473,652 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Obtaining the Python Control Flow Graph

Hi,

I have been looking for a good way to convert python code into a
control flow graph.

I know of Python functions that will convert an expression into an
abstract syntax tree (i.e. ast = parser.expr('(x +5)*5') then t =
ast.totuple() then t), but I am not sure how to obtain a CFG.

I've gone through the compiler and it has code that converts the AST
into a CFG (described here:
http://www.python.org/doc/peps/pep-0...g-to-bytecode).
Basically, PyAST_Compile() in Python/compile.c coverts the AST to a CFG
and outputs final bytecode from the CFG by calling two functions:
PySymtable_Buil d() in Python/symtable.c and compiler_mod() in
Python/compile.c. PySymtable_Buil d() will build a symtable and
compiler_mod() will create the CFG.

PyPy also offers a way to obtain a control flow graph:
http://codespeak.net/pypy/dist/pypy/...the-flow-model

I was wondering if anyone had any advice on the best way to obtain a
control flow graph. I need the control flow graph because I am trying
figure out if there is a way to bound the integer ranges and list
lengths at compile time.

Thank you for your help

Apr 3 '06 #1
2 4993
Hi!

br******@gmail. com wrote:
I have been looking for a good way to convert python code into a
control flow graph.

I know of Python functions that will convert an expression into an
abstract syntax tree (i.e. ast = parser.expr('(x +5)*5') then t =
ast.totuple() then t), but I am not sure how to obtain a CFG.

I've gone through the compiler and it has code that converts the AST
into a CFG (described here:
http://www.python.org/doc/peps/pep-0...g-to-bytecode).
Basically, PyAST_Compile() in Python/compile.c coverts the AST to a CFG
and outputs final bytecode from the CFG by calling two functions:
PySymtable_Buil d() in Python/symtable.c and compiler_mod() in
Python/compile.c. PySymtable_Buil d() will build a symtable and
compiler_mod() will create the CFG.

PyPy also offers a way to obtain a control flow graph:
http://codespeak.net/pypy/dist/pypy/...the-flow-model
(Disclaimer: I am a PyPy developer) This works quite well in most cases
but not in all, e.g. generators are not supported. It has other
problems: The result will be (due to the used approach) in SSA form,
which might be good or bad, depending on what you want. I don't know the
CPython compiler well enough to say whether it is easy to get a CFG out
of it. I know of no other easy method to get a CFG graph from Python code.
I was wondering if anyone had any advice on the best way to obtain a
control flow graph. I need the control flow graph because I am trying
figure out if there is a way to bound the integer ranges and list
lengths at compile time.


This might be quite hard, for generic Python code. You might possibly
(depending again on what your exact plans are) also look into the type
inference part of PyPy:

http://codespeak.net/pypy/dist/pypy/...nnotation-pass

Feel free to also contact the PyPy mailing list (py******@codes peak.net)
if you have PyPy-specific questions.

Cheers,

Carl Friedrich Bolz

Apr 3 '06 #2
br******@gmail. com wrote:

I was wondering if anyone had any advice on the best way to obtain a
control flow graph. I need the control flow graph because I am trying
figure out if there is a way to bound the integer ranges and list
lengths at compile time.


Although I haven't actually been generating control flow graphs as
such, the analysis distribution [1] produces both HTML summaries and C
source code for Python programs of a certain level of sophistication,
and I'm currently trying to finish off a new release which exposes
different strategies for deducing types and specialising functions. You
probably want to read Mark Dufour's ShedSkin paper [2] to get an idea
about how hard this kind of stuff is, and a dose of defeatism might
also be necessary given that your problem may not be solvable in
general. ;-)

Paul

[1] http://www.boddie.org.uk/python/analysis.html
[2] http://kascade.org/optimizing_python.pdf

Apr 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2150
by: Samir Patel | last post by:
For last couple of weeks, I was doing lot of research on various simulation libraries. Only one I find in python is http://simpy.sourceforge.net. It is very powerful, but it seems that it does not have any active development. I am sure there are more than that. Where are they? If one wants to create a new simulation program, what are different packages to use: simulation event list - priority queue?
42
4075
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time. I'm not interested on which language is better in *general*, just for my purpose. My area of research is in CAD algorithms, and I'm sensing the need to resort to something more expedient than C++, bash scripting, or sed scripting.
0
1036
by: Rick Hilburger | last post by:
My new non-profit web site is www.getcet.org It's about automatic flow chart generation. I have a program that automatically draws a flow chart next to Python code so that you can see and understand the flow among the lines of Python code. I use a slightly different syntax for flow control than Python does, but the rest is regular Python. I have numerous images to illustrate this. I think it would be a new way to have code that is...
3
3054
by: Biff | last post by:
Hello, I am bringing up an iFrame with a calendar control in it in coordination with a text box that holds a date field. In my code behind class I add a method call to the text box's OnFocus event that calls the javascript method that shows and positions the iFrame (arguments are the text control, and the desired x and y coordinates of the iFrame). Right now I use hardcoded fixed values for the calendar location - it was expedient and...
267
10675
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
0
1323
by: jacksparrowminion | last post by:
Hi I want to do a simple control flow graph tool that generates a graph for the user i .e. the circles and arrows that make up a control flow graph used in program testing. Do you have any idea how it can be done it .NET using C# ? i tried the GDI+ but didnt get to understand how to dynamically draw shapes according to user input. Also tride playing around with picture boxes but cannot find the exact solution.
0
2348
by: mylog | last post by:
I have downloaded a GLEE(Graph Layout Execution Engine) and written the following code to display a windows form from a web page but i am encountering with a small problem on displaying the graph into the display and I am getting the following error when I run the web page.Can anybody solve this problem. I would be very grateful. Thanks Error: System.InvalidOperationException: Showing a modal dialog box or form when the application is not...
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8703
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.