Recompile AST?

Bengt Richter bokr at oz.net
Thu Nov 10 18:37:50 EST 2005


On 10 Nov 2005 13:21:56 -0800, chrisspen at gmail.com wrote:

>Is it possible to recompile the AST generated by compiler.parse, back
>into code or an executable code object? My aim here is to allow a
>script to manipulate Python code as elements within a list. However, it
>doesn't look like the module has any native methods designed to do
>this.
>
It is possible, but if you want code from a particular python source
string that you are giving to compiler.parse, why not use compile?
Or do you want to modify the AST and then compile? This can be done too,
but modifying the AST can be tricky.

Based on the starting point providided by
    http://groups.google.com/group/comp.lang.python/browse_thread/thread/5fa80186d9f067f4/7a2351b221063a8c

I've done a few experiments, e.g.,
    http://groups.google.com/group/comp.lang.python/msg/9a3f473ae5c23751

I've also posted sporadic musings about the possibilities for AST-transforming
custom import functions to do optimizations and macros and special forms etc.,
but no one seemed much interested (or maybe they quietly went off to do something
of their own ;-)

I've seen some references to new AST stuff in the latest version, but I haven't
got the new version. I really must clean up my disk for space ;-/

Regards,
Bengt Richter



More information about the Python-list mailing list