如何检索python代码的控制流图? [英] How to retrieve a Control Flow Graph for python code?

查看:47
本文介绍了如何检索python代码的控制流图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转储给定 python 代码的控制流图,类似于 gcc 编译器选项给出的选项:-fdump-tree-cfg for c 代码.

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.

我成功获得了python代码的AST(抽象语法树),但是从 AST 阶段获取控制流图看起来相当复杂和麻烦.

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.

有没有更简单的方法可以直接检索python代码的控制流图?有什么建议吗?

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

哦,顺便说一下,我使用的是 python3.5

oh by the way I'm using python3.5

谢谢大家!

附言我真的不知道我在使用什么样的解释器,据我所知它是 CPython(不确定),我不认为它是 PyPy(Rpython).任何建议我如何验证它?

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?

推荐答案

请参阅我的 SO 关于如何构建控制流图的答案,使用 AST.

最初的问题是关于 Java 的 CFG,但该方法实际上非常通用,同样的方法也适用于为 Python 生成 CFG.

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.

这篇关于如何检索python代码的控制流图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆