[Python-Dev] Module Suggestion: ast

Thomas Lee tom at vector-seven.com
Wed Apr 30 01:22:27 CEST 2008

Just a thought, but it would be great if this could be implemented over 
the top of a C layer that operates on real AST nodes (rather than the 
PyObject representation of those nodes). I'm working on stuff to perform 
code optimization at the AST level (see the tlee-ast-optimize branch), 
and the functionality you're describing may wind up being very useful to me.

I've got more to say on the topic, but I'm at work right now. Just 
something to keep in mind.


Armin Ronacher wrote:
> Hi all,
> I would like to propose a new module for the stdlib for Python 2.6
> and higher:  "ast".  The motivation for this module is the pending
> deprecation for compiler.ast which is widely used (debugging,
> template engines, code coverage etc.).  _ast is a very solid module
> and is without a doubt easier to maintain then compiler.ast which
> was written in Python, it's lacking some features such as pretty
> printing the AST or traversing it.
> The idea of "ast" would be adding high level functionality for
> easier working with the AST.  It currently provides these features:
> -   pretty printing AST objects
> -   a parse function as easier alias for compile() + flag
> -   operator-node -> operator symbol mappings (eg: _ast.Add -> '+')
> -   methods to modify lineno / col_offset (incrementing or copying
>     the data over from existing nodes)
> -   getting the fields of nodes as dicts
> -   iterating over all child nodes
> -   a function to get the docstring or an AST node
> -   a node walker that yields all child-nodes recursively
> -   a `NodeVistor` and `NodeTransformer`
> Additionally there is a `literate_eval` function in that module that
> can safely evaluate python literals in a string.
> Module and unittests are located in the Pocoo Sandbox HG repository:
>   http://dev.pocoo.org/hg/sandbox/file/tip/ast/ast.py
>   http://dev.pocoo.org/hg/sandbox/file/tip/ast/test_ast.py
> A slightly modified version of the ast.py module for Python 2.5
> compatibility is currently in use for the Mako template engine to
> achieve support for Google's AppEngine.
> An example module for the NodeVisitor is in the repository which
> converts a Python AST back into Python source code:
>   http://dev.pocoo.org/hg/sandbox/file/tip/ast/codegen.py
> Regards,
> Armin
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/tom%40vector-seven.com

More information about the Python-Dev mailing list