In my python debugger I have a way of remapping a string to a filename so that when you are stepping through an exec'd function inside the debugger you can list lines pygmentized, or view them along inside an editor like Emacs via realgud.
So I'd like to be able to extract the string in an exec statement when CPython is stopped inside evaluating that.
I already have a mechanism that can look back in the call frame to see if the caller was an EXEC_STMT
and I can look back one instruction to see if the previous instruction was say DUP_TOP
. So I'd be home free if I could just figure out a way to read the stack entry at the time of the call and that gives the string evaluated. There is probably a way to drop into C to get this, but my knowledge of CPython internals lacking, and would prefer not to do this. If there's a package out there, maybe I could include that optionally.
CPython already provides access to function arguments, and local variables but of course since this is a built-in function this isn't recorded as a function parameter.
If there are other thoughts at how to do the same thing, that'd be okay too. I feel a less good solution would be to somehow try to overload or replace exec
since debuggers can be brought in late in the game.
I understand that CPython2 and CPython3 may be a little bit different here, but to start off either would do.