What is the easiest way to generate a Control Flow-Graph for a method in Python?

时光毁灭记忆、已成空白 提交于 2019-12-21 03:37:14

问题


I am writing a program that tries to compare two methods. I would like to generate Control flow graphs (CFG) for all matched methods and use either a topological sort to compare the two graphs.


回答1:


RPython, the translation toolchain behind PyPy, offers a way of grabbing the flow graph (in the pypy/rpython/flowspace directory of the PyPy project) for type inference.

This works quite well in most cases but generators are not supported. The result will be in SSA form, which might be good or bad, depending on what you want.




回答2:


http://pycallgraph.slowchop.com/ looks like what you need.

Python trace module also have option --trackcalls that can be an entrypoint for call tracing machinery in stdlib.



来源:https://stackoverflow.com/questions/6204914/what-is-the-easiest-way-to-generate-a-control-flow-graph-for-a-method-in-python

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