How to retrieve a Control Flow Graph for python code?

≡放荡痞女 提交于 2019-12-09 19:23:08

问题


I would like to dump the Control Flow Graph of a given python code, similar to the option given by gcc compiler option: -fdump-tree-cfg for c code.

I succeeded getting the AST (Abstract Syntax Trees) of a python code, but it seams quite complex and hassle to get the Control Flow Graph from AST phase.

Is there an easier way to retrieve directly the Control Flow Graph of a python code? any suggestions?

oh by the way I'm using python3.5

Thank you all!

P.S I really don't know what kind of interpreter I'm using under the hood, As far as I know it's CPython (not sure), I don't think it's PyPy(Rpython). Any suggestion how can I verify it?


回答1:


See my SO answer on how to build a control flow graph, using an AST.

The original question asked about CFGs for Java, but the approach is actually pretty generic, and the same approach would work for producing a CFG for Python.

I wouldn't have called this "quite complex"; the basic idea is pretty simple.



来源:https://stackoverflow.com/questions/39719729/how-to-retrieve-a-control-flow-graph-for-python-code

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