getting the C python exec argument string or accessing the evaluation stack

一个人想着一个人 提交于 2019-12-04 19:24:26

The open-source Thonny IDE has [sub]expression evaluation stepping. See the author's answer to the SO question Tracing Python expression evaluation step by step.

I think I've now found a way.

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

The tree at the point of call will have something like exec_stmt -> expr .... That expression will have the text of the expression which is not necessarily the value of the expression. The expression could be a constant string value, but it could be something complex like "foo" + var1.

So then the debugger can evaluate that string in the context of the debugger which knows how to evaluate expressions up the call stack.

This still has a problem of the reevaluating the expression may have side effects. But that's bad programming practice, right? ;-)

So instead what I do is just decompile the code from the bytecode if the source isn't there. This has a disadvantage in that the line numbers mentioned in the bytecode don't always line up with those in the bytecode. For that the method of recreating the string above is better.

In closing, I hope to give some idea why writing a really good debugger is hard and why the vast number of debuggers have a number of limitations on even simple things like getting the source text at the point you are currently stopped.

A totally different approach would be to stop early and switch to an sub-interpreter like byterun (or some suitably modified Python C module) which would have access to a stack.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!