Tracing the execution of a Python program line/statement by line programmatically

旧巷老猫 提交于 2019-12-02 07:09:01

Look at the sys.settrace function. This allows you to specify a tracing function which is executed for every line of code. It was, I think, specifically implemented to create pdb so it's at the right level of abstraction you seem to want. Implementing what you want will not be trivial but I think it's the best place to start.

For an production program that uses this, look at coverage.py.

Accepting Noufal's answer since it put me on the right path, but this helped as well

http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html

The April's fools module goto is a working example of tracing implemented in Python, so although it's doubtlessly slower (than the C implementation in coverage.py), it's rather easy to understand. http://entrian.com/goto/

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