Recompile AST?

C

chrisspen

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.

Chris
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[[email protected]]
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.

Pynits does something like this for pretty-printing Python statements,
from within Vim. I did not touch this project for a while, yet I still
intend to revisit it in a few weeks or months (for an actual need). You
may peek at:

http://fp-etc.progiciels-bpi.ca/pynits.html
 
B

Bengt Richter

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...read/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
 
P

Paul Boddie

Bengt said:
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 ;-)

For an example of AST transformations, take a look at the
libxml2macro.py script in the libxml2dom distribution [1]: it contains
code which transforms ASTs based on simple rules (changing method calls
to function calls) as well as the magic sequence of instructions to
generate .pyc files from ASTs. Originally, the analysis distribution
[2] also compiled modified ASTs to Python bytecode, but such activities
didn't really fit in with the goals of that particular project.

Paul

[1] http://www.python.org/pypi/libxml2dom
[2] http://www.python.org/pypi/analysis
 
B

Bengt Richter

Bengt said:
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 ;-)

For an example of AST transformations, take a look at the
libxml2macro.py script in the libxml2dom distribution [1]: it contains
code which transforms ASTs based on simple rules (changing method calls
to function calls) as well as the magic sequence of instructions to
generate .pyc files from ASTs. Originally, the analysis distribution
[2] also compiled modified ASTs to Python bytecode, but such activities
didn't really fit in with the goals of that particular project.

Paul

[1] http://www.python.org/pypi/libxml2dom
[2] http://www.python.org/pypi/analysis
Thanks for the links. I downloaded the tgzs but it will be a while
before I get into anything ;-)

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,351
Members
48,036
Latest member
nickwillsonn

Latest Threads

Top