ucb-sejits / ctree

A C-family AST implementation designed to be an IR for DSL compilers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Demo code ast_node.to_dot() not found error.

mkmojo opened this issue · comments

Dear ctree team:

When I was testing the sample code following this guide Using IPython for AST Visualization.

I observed some behavior that I cannot figure out the cause to.

The Error

There are two code snippet in the tutorial.
Code snippet A:

import ctree

def f(a):
    for x in range(10):
        a[x] += x

tree1 = ctree.get_ast(f)
ctree.ipython_show_ast(tree1)

Code snippet B:

from ctree.transformations import PyBasicConversions

t = PyBasicConversions()
tree2 = t.visit(tree1)
ctree.ipython_show_ast(tree2)

Here is how to reproduce my error:

  1. create a new ipython notebook
  2. run code snippet A
  3. get ast_node.to_dot() error

The exact error message I have is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-73912badbc60> in <module>()
      6 
      7 tree1 = ctree.get_ast(f)
----> 8 ctree.ipython_show_ast(tree1)

/Library/Python/2.7/site-packages/ctree/__init__.pyc in ipython_show_ast(tree)
    114     then renders that into a png file
    115     """
--> 116     return DotManager.dot_ast_to_image(tree)
    117 
    118 

/Library/Python/2.7/site-packages/ctree/visual/dot_manager.pyc in dot_ast_to_image(ast_node)
     15     @staticmethod
     16     def dot_ast_to_image(ast_node):
---> 17         dot_text = ast_node.to_dot()
     18         return DotManager.dot_text_to_image(dot_text)
     19 

AttributeError: 'Module' object has no attribute 'to_dot'

The interesting thing is that if you simply run code snippet B before code snippet A, the error goes away.

Is this error the expected behavior or I am missing something?
Could you please kindly point out what I did wrong here?

Package List

Acquired by pip freeze

Flask==0.10.1
GraphLab-Create==1.1
Jinja2==2.7.3
Mako==1.0.0
MarkupSafe==0.23
Pygments==2.0.1
Sphinx==1.2.3
Twisted==13.2.0
Werkzeug==0.9.6
altgraph==0.10.2
backports.ssl-match-hostname==3.4.0.2
bdist-mpkg==0.5.0
bonjour-py==0.3
boto==2.33.0
certifi==14.05.14
ctree==0.95a9
decorator==3.4.0
diff-match-patch==20110725.1
docutils==0.12
gnureadline==6.3.3
ipython==2.3.1
itsdangerous==0.24
librato-metrics==0.4.9
llvmpy==0.12.5-3-g7af2f71
macholib==1.5.1
matplotlib==1.4.2
mixpanel-py==3.1.1
mock==1.0.1
modulegraph==0.10.4
nose==1.3.4
numpy==1.9.1
openbabel==1.8.1
prettytable==0.7.2
py2app==0.7.3
pyOpenSSL==0.13.1
pycl==0.1a2
pygraphviz==1.2
pyobjc-core==2.5.1
pyobjc-framework-Accounts==2.5.1
pyobjc-framework-AddressBook==2.5.1
pyobjc-framework-AppleScriptKit==2.5.1
pyobjc-framework-AppleScriptObjC==2.5.1
pyobjc-framework-Automator==2.5.1
pyobjc-framework-CFNetwork==2.5.1
pyobjc-framework-Cocoa==2.5.1
pyobjc-framework-Collaboration==2.5.1
pyobjc-framework-CoreData==2.5.1
pyobjc-framework-CoreLocation==2.5.1
pyobjc-framework-CoreText==2.5.1
pyobjc-framework-DictionaryServices==2.5.1
pyobjc-framework-EventKit==2.5.1
pyobjc-framework-ExceptionHandling==2.5.1
pyobjc-framework-FSEvents==2.5.1
pyobjc-framework-InputMethodKit==2.5.1
pyobjc-framework-InstallerPlugins==2.5.1
pyobjc-framework-InstantMessage==2.5.1
pyobjc-framework-LatentSemanticMapping==2.5.1
pyobjc-framework-LaunchServices==2.5.1
pyobjc-framework-Message==2.5.1
pyobjc-framework-OpenDirectory==2.5.1
pyobjc-framework-PreferencePanes==2.5.1
pyobjc-framework-PubSub==2.5.1
pyobjc-framework-QTKit==2.5.1
pyobjc-framework-Quartz==2.5.1
pyobjc-framework-ScreenSaver==2.5.1
pyobjc-framework-ScriptingBridge==2.5.1
pyobjc-framework-SearchKit==2.5.1
pyobjc-framework-ServiceManagement==2.5.1
pyobjc-framework-Social==2.5.1
pyobjc-framework-SyncServices==2.5.1
pyobjc-framework-SystemConfiguration==2.5.1
pyobjc-framework-WebKit==2.5.1
pyparsing==2.0.3
pyserial==2.7
python-dateutil==2.2
pytz==2014.9
pyzmq==14.4.1
requests==2.3.0
scipy==0.13.0b1
six==1.8.0
tornado==3.2.1
vboxapi==1.0
wsgiref==0.1.2
xattr==0.6.4
zope.interface==4.1.1

I think I kinda know the issue.
The to_dot() method is defined in the ctree/nodes.py
So if we just import ctree it is not enough
Instead, what we should do to fix code snippet A is import the nodes module as well.
Therefore the following code will work:

import ctree.nodes #<-- change from import ctree
def f(a):
    for x in range(10):
        a[x] += x

tree1 = ctree.get_ast(f)
ctree.ipython_show_ast(tree1)

This is a good catch, what actually happens is ctree.nodes imports ctree.dotgen which is responsible for patching the ast.AST subclasses (built-in Python AST nodes) with the a to_dot method. Seems that importing a dot viewer should ensure that this code is called so that the dotgen methods work properly which should be a simple fix.

I have a pending fix for this. I will try to get it in by the end of the day