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?
I found interesting project on github: pyrasite
Inject code into running Python processes http://pyrasite.com
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?).
imp.reloada 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.