Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting namespaces right when parsing/executing Python ASTs

Thread Tools

Getting namespaces right when parsing/executing Python ASTs

Gordon Fraser
Posts: n/a

I'm trying to parse Python code to an AST, apply some changes to the AST
and then compile and run the AST, but I'm running into problems when
trying to evaluate/execute the resulting code object. It seems that the
global namespace differs depending on where I call parse and eval/exec.

The following code parses a file, compiles and then evaluates the AST.
If I call Python directly on this code, then it works:

import sys, parser
ast = parser.suite(open(sys.argv[1]).read())
code = ast.compile()

....and it also works this way with Python2.6:

ast = compile(open(sys.argv[1]).read(), "<AST>",
code = compile(ast, "<AST", "exec")

However, if I include that snippet in a different scope (some function
or class), then the namespace that the code object will have differs -
it seems the symbols defined in the AST are not included when executing
the code. For example:

import sys,parser

def main():
ast = parser.suite(open(sys.argv[1]).read())
code = ast.compile()

if __name__ == "__main__":

In particular this is a problem if I'm parsing a module with several
functions - none of these functions actually ends up in the scope of the
code object (same behavior with Python2.6 and the PyCF_ONLY_AST

The function "exec" takes parameters for globals and locals, but I have
no idea where to get these dictionaries from the "parser" module. My
guess is that I am misunderstanding something about how Python treats
namespaces. Can anyone help me here?


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
data structure for ASTs in Python-written parsers eliben Python 0 02-14-2009 06:47 AM
Getting namespaces from rake Chris Kilmer Ruby 0 10-26-2007 02:56 PM
"Right" use of namespaces lionheart C++ 4 08-14-2006 01:24 PM
Namespaces, or getting function declarations to work António Marques Javascript 14 05-29-2006 10:04 PM
pass the right form input to the right control Tom ASP .Net 0 12-11-2003 03:07 AM