Getting exec p python argument string or accessing evaluation stack

In my python debugger, I have a way to reassign a line to a file name so that when you go through the exec'd function inside the debugger, you can list the lines with strong keystrokes or view them inside an editor such as Emacs through realgud .

Thus, I would like to be able to retrieve the string in the exec statement when CPython stops when evaluating this.

I already have a mechanism that can look back in the call frame to find out if there was a callerEXEC_STMT , and I can look back one instruction to see if there was a previous instruction DUP_TOP. Therefore, I would be free to go home if I could just figure out how to read the stack entry during the call, and this gives a line estimate. There is probably a way to get into C to get this, but my knowledge of CPython's internal functions is missing and would rather not. If there is a package there, maybe I can enable it optionally.

CPython already provides access to function arguments and local variables, but of course, since it is an inline function, it is not written as a function parameter.

If there are other thoughts on how to do the same, that would be fine too. I feel that a less good solution would be to try to overload or replace somehow exec, as debuggers may appear at the end of the game.

I understand that CPython2 and CPython3 may be slightly different, but start with it.

+1
source share
2 answers

I think I have now found a way.

Inside the debugger, I go up the call stack one level to get to the operator exec. Then I can use uncompyle6 to get the syntax tree of the source code. (In uncompyle6, a change may be required to make this easier.)

- exec_stmt → expr... , . , - , "foo" + var1.

, , , .

. , ? ;-)

, , , .

, -, byterun ( - Python C), .

0

Thonny IDE [sub] stepping. . SO. Python .

0

Source: https://habr.com/ru/post/1678470/


All Articles