Sorry for the kind of general question. More details about what I want:
I want the user to be able to write some Python code and execute it. Once there is an exception which is not handled, I want the debugger to pause the execution, show information about the current state/environment/stack/exception and make it possible to edit the code.
I only want to have the special code block editable where the exception occurred and nothing else (for now). I.e. if it occurred inside a for
loop, I only want to have the code block inside the for
loop editable. It should be the latest/most recent code block which is in the user editor scope (and not inside some other libs or the Python libs). Under this conditions, it is always clear what code block to edit.
I already tried to investigate a bit how to do this, though I feel a bit lost.
The Python traceback doesn't give me directly the code block, just the function and the code line. I could calculate that back but that seems a bit hacky to me. Better (and more natural) would be if I could somehow get a reference to the code block in the AST of the code.
To get the AST (and operate on it, i.e. manipulate/edit), I probably will use the compiler
(which is deprecated?) and/or the parser
module. Or the ast
module. Not sure though how I could recompile special nodes / code blocks in the AST. Or if I only can recompile whole functions.