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

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

问题描述

我想转储给定python代码的控制流图
,类似于gcc编译器选项:-fdump-tree-cfg用于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

谢谢大家!

PS
我真的不知道我正在使用什么样的解释器,
据我所知它是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?

推荐答案

请参阅我的如何使用AST构建控制流程图的答案

最初的问题是关于CFG的问题Java,但是这种方法实际上非常通用,并且相同的方法也可以为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天全站免登陆