4

Let's say I am stepping through code with Eclipse's PyDev.

Is there a way to add some new code below the breakpoint... modify the code on the fly / at runtime?

2
  • Well, you could come up with some code that allows you to, e.g., imp.reload a module. If you're writing code that's mostly stateless responses (like a web framework), and only want the next request to get the new code, that's easy (in fact, many frameworks, like Flask, do that automatically). If you're dealing with stateful code, you have to keep a weakdict of your state objects to pickle-unpickle everything. If you want to affect the current function stack, you need a function to rebuild the frames. It's all doable (at least in CPython/PyPy)… but not at all easy, and probably a bad idea. Commented Sep 3, 2014 at 0:35
  • It might be possible to re-import a module, but that would be a much more limited mechanism than what the OP is asking for. Beaten by 4 seconds! Commented Sep 3, 2014 at 0:35

2 Answers 2

1

I found interesting project on github: pyrasite

Inject code into running Python processes http://pyrasite.com

Sign up to request clarification or add additional context in comments.

Comments

0

In PDB, you can use p to evaluate and print expressions and ! to execute atements:

p expression

Evaluate the expression in the current context and print its value.

[!]statement

Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a global command on the same line, e.g.:

(Pdb) global list_options; list_options = ['-l']
(Pdb)

Docs: https://docs.python.org/2/library/pdb.html

As for how to do this in PyDev, I don't use PyDev so I'm not quite sure; but this functionality should be available to any BDB-based debuggers (which I believe PyDev is also based on BDB?).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.