Recompile AST?

Leif K-Brooks eurleif at ecritters.biz
Thu Nov 10 19:50:58 EST 2005


chrisspen at gmail.com wrote:
> Is it possible to recompile the AST generated by compiler.parse, back
> into code or an executable code object?

Into a bytecode object:

 >>> from compiler.pycodegen import ModuleCodeGenerator
 >>> from compiler.misc import set_filename
 >>> from compiler import parse
 >>>
 >>> tree = parse('foo = 42')
 >>> set_filename('<foo>', tree)
 >>> code = ModuleCodeGenerator(tree).getCode()
 >>> exec code
 >>> foo
 42

Into Python source code: <http://ecritters.biz/asttosource.py>.



More information about the Python-list mailing list