Ñò
§ÚêLc @ s¼ d Z d d k Td d k l Z d d d „ Z d „ Z e e d „ Z d „ Z d
„ Z d d „ Z
d
„ Z d „ Z e d „ Z
d „ Z d e f d „ ƒ YZ d e f d „ ƒ YZ d S( sH
ast
~~~
The `ast` module helps Python applications to process trees of the Python
abstract syntax grammar. The abstract syntax itself might change with
each Python release; this module helps to find out programmatically what
the current grammar looks like and allows modifications of it.
An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as
a flag to the `compile()` builtin function or by using the `parse()`
function from this module. The result will be a tree of objects whose
classes all inherit from `ast.AST`.
A modified abstract syntax tree can be compiled into a Python code object
using the built-in `compile()` function.
Additionally various helper functions are provided that make working with
the trees simpler. The main intention of the helper functions and this
module in general is to provide an easy to use interface for libraries
that work tightly with the python syntax (template engines for example).
:copyright: Copyright 2008 by Armin Ronacher.
:license: Python License.
iÿÿÿÿ( t *( t __version__s t execc C s t | | | t ƒ S( so
Parse an expression into an AST node.
Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST).
( t compilet
PyCF_ONLY_AST( t exprt filenamet mode( ( s /usr/lib64/python2.6/ast.pyt parse s c sz h d d 6t d 6t d 6‰ t | t ƒ o t | d d ƒ} n t | t ƒ o
| i } n ‡ ‡ f d † ‰ ˆ | ƒ S( sú
Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and None.
t Nonet Truet FalseR t evalc sø t | t ƒ o | i St | t ƒ o | i St | t ƒ o t t ˆ | i ƒ ƒ St | t ƒ o t
t ˆ | i ƒ ƒ St | t ƒ o* t ‡ f d † t
| i | i ƒ Dƒ ƒ St | t ƒ o | i ˆ j o ˆ | i Sn t d ƒ ‚ d S( Nc 3 s1 x* | ]# \ } } ˆ | ƒ ˆ | ƒ f Vq Wd S( N( ( t .0t kt v( t _convert( s /usr/lib64/python2.6/ast.pys > s s malformed string( t
isinstancet Strt st Numt nt Tuplet tuplet mapt eltst Listt listt Dictt dictt zipt keyst valuest Namet idt
ValueError( t node( t _safe_namesR ( s /usr/lib64/python2.6/ast.pyR 4 s N( R R
R R t
basestringR t
Expressiont body( t node_or_string( ( R% R s /usr/lib64/python2.6/ast.pyt literal_eval( s
c sI ‡ ‡ ‡ f d † ‰ t | t ƒ p t d | i i ƒ ‚ n ˆ | ƒ S( s»
Return a formatted dump of the tree in *node*. This is mainly useful for
debugging purposes. The returned string will show the names and the values
for fields. This makes the code impossible to evaluate, so if evaluation is
wanted *annotate_fields* must be set to False. Attributes such as line
numbers and column offsets are not dumped by default. If this is wanted,
*include_attributes* can be set to True.
c s% t ˆ t ƒ oÙ g } t ˆ ƒ D] \ } } | | ˆ | ƒ f q! ~ } d ˆ i i d i ˆ o d „ | Dƒ n d „ | Dƒ ƒ f } ˆ oO ˆ i oE | | o d p d 7} | d i ‡ ‡ f d † ˆ i Dƒ ƒ 7} n | d St ˆ t ƒ o" d d i ‡ f d † ˆ Dƒ ƒ St ˆ ƒ S(
Ns %s(%ss , c s s x | ] } d | Vq Wd S( s %s=%sN( ( R
t field( ( s /usr/lib64/python2.6/ast.pys T s c s s x | ] \ } } | Vq Wd S( N( ( R
t at b( ( s /usr/lib64/python2.6/ast.pys V s t c 3 s2 x+ | ]$ } d | ˆ t ˆ | ƒ ƒ f Vq Wd S( s %s=%sN( t getattr( R
R, ( R$ t _format( s /usr/lib64/python2.6/ast.pys Z s t )s [%s]c 3 s x | ] } ˆ | ƒ Vq Wd S( N( ( R
t x( R0 ( s /usr/lib64/python2.6/ast.pys ^ s ( R t ASTt iter_fieldst __class__t __name__t joint _attributesR t repr( R$ t _[1]R, R- t fieldst rv( t include_attributest annotate_fieldsR0 ( R$ s /usr/lib64/python2.6/ast.pyR0 P s 9 "s expected AST, got %r( R R3 t TypeErrorR5 R6 ( R$ R> R= ( ( R0 R= R> s /usr/lib64/python2.6/ast.pyt dumpG s c C sb x[ d D]S } | | i j o= | | i j o- t | | ƒ o t | | t | | ƒ ƒ q q W| S( s‹
Copy source location (`lineno` and `col_offset` attributes) from
*old_node* to *new_node* if possible, and return *new_node*.
t linenot
col_offset( s linenos
col_offset( R8 t hasattrt setattrR/ ( t new_nodet old_nodet attr( ( s /usr/lib64/python2.6/ast.pyt
copy_locatione s !c s# ‡ f d † ‰ ˆ | d d ƒ | S( s{
When you compile a node tree with compile(), the compiler expects lineno and
col_offset attributes for every node that supports them. This is rather
tedious to fill in for generated nodes, so this helper adds these attributes
recursively where not already set, by setting them to the values of the
parent node. It works recursively starting at *node*.
c sŸ d | i j o* t | d ƒ p
| | _ q: | i } n d | i j o* t | d ƒ p
| | _ qt | i } n x$ t | ƒ D] } ˆ | | | ƒ q Wd S( NRA RB ( R8 RC RA RB t iter_child_nodes( R$ RA RB t child( t _fix( s /usr/lib64/python2.6/ast.pyRK y s
i i ( ( R$ ( ( RK s /usr/lib64/python2.6/ast.pyt fix_missing_locationsq s
i c C su d | i j o t | d d ƒ | | _ n xA t | ƒ D]3 } d | i j o t | d d ƒ | | _ q: q: W| S( sœ
Increment the line number of each node in the tree starting at *node* by *n*.
This is useful to "move code" to a different location in a file.
RA i ( R8 R/ RA t walk( R$ R RJ ( ( s /usr/lib64/python2.6/ast.pyt increment_linenoŠ s
!c c sF x? | i D]4 } y | t | | ƒ f VWq
t j
o q
Xq
Wd S( ss
Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
that is present on *node*.
N( t _fieldsR/ t AttributeError( R$ R+ ( ( s /usr/lib64/python2.6/ast.pyR4 — s
c c sx xq t | ƒ D]c \ } } t | t ƒ o | Vq
t | t ƒ o. x+ | D] } t | t ƒ o | VqI qI Wq
q
Wd S( s‰
Yield all direct child nodes of *node*, that is, all fields that are nodes
and all items of fields that are lists of nodes.
N( R4 R R3 R ( R$ t nameR+ t item( ( s /usr/lib64/python2.6/ast.pyRI £ s
c C s² t | t t t f ƒ p t d | i i ƒ ‚ n | i oq t | i d t ƒ oZ t | i d i t
ƒ o@ | o' d d k } | i | i d i i
ƒ S| i d i i
Sd S( sª
Return the docstring for the given node or None if no docstring can
be found. If the node provided does not have docstrings a TypeError
will be raised.
s %r can't have docstringsi iÿÿÿÿN( R t FunctionDeft ClassDeft ModuleR? R5 R6 R( t Exprt valueR t inspectt cleandocR ( R$ t cleanRX ( ( s /usr/lib64/python2.6/ast.pyt
get_docstring± s !c c sV d d k l } | | g ƒ } x0 | o( | i ƒ } | i t | ƒ ƒ | Vq" Wd S( s±
Recursively yield all child nodes of *node*, in no specified order. This is
useful if you only want to modify nodes in place and don't care about the
context.
iÿÿÿÿ( t dequeN( t collectionsR\ t popleftt extendRI ( R$ R\ t todo( ( s /usr/lib64/python2.6/ast.pyRM Á s t NodeVisitorc B s e Z d Z d „ Z d „ Z RS( s<
A node visitor base class that walks the abstract syntax tree and calls a
visitor function for every node found. This function may return a value
which is forwarded by the `visit` method.
This class is meant to be subclassed, with the subclass adding visitor
methods.
Per default the visitor functions for the nodes are ``'visit_'`` +
class name of the node. So a `TryFinally` node visit function would
be `visit_TryFinally`. This behavior can be changed by overriding
the `visit` method. If no visitor function exists for a node
(return value `None`) the `generic_visit` visitor is used instead.
Don't use the `NodeVisitor` if you want to apply changes to nodes during
traversing. For this a special visitor exists (`NodeTransformer`) that
allows modifications.
c C s/ d | i i } t | | | i ƒ } | | ƒ S( s
Visit a node.t visit_( R5 R6 R/ t
generic_visit( t selfR$ t methodt visitor( ( s /usr/lib64/python2.6/ast.pyt visitã s c C sˆ x t | ƒ D]s \ } } t | t ƒ o6 xT | D]' } t | t ƒ o | i | ƒ q0 q0 Wq
t | t ƒ o | i | ƒ q
q
Wd S( s9 Called if no explicit visitor function exists for a node.N( R4 R R R3 Rg ( Rd R$ R+ RW RR ( ( s /usr/lib64/python2.6/ast.pyRc é s
( R6 t
__module__t __doc__Rg Rc ( ( ( s /usr/lib64/python2.6/ast.pyRa Ï s t NodeTransformerc B s e Z d Z d „ Z RS( s\
A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
allows modification of nodes.
The `NodeTransformer` will walk the AST and use the return value of the
visitor methods to replace or remove the old node. If the return value of
the visitor method is ``None``, the node will be removed from its location,
otherwise it is replaced with the return value. The return value may be the
original node in which case no replacement takes place.
Here is an example transformer that rewrites all occurrences of name lookups
(``foo``) to ``data['foo']``::
class RewriteName(NodeTransformer):
def visit_Name(self, node):
return copy_location(Subscript(
value=Name(id='data', ctx=Load()),
slice=Index(value=Str(s=node.id)),
ctx=node.ctx
), node)
Keep in mind that if the node you're operating on has child nodes you must
either transform the child nodes yourself or call the :meth:`generic_visit`
method for the node first.
For nodes that were part of a collection of statements (that applies to all
statement nodes), the visitor may also return a list of nodes rather than
just a single node.
Usually you use the transformer like this::
node = YourTransformer().visit(node)
c C s xt | ƒ D] \ } } t | | d ƒ } t | t ƒ oŠ g } xv | D]n } t | t ƒ oK | i | ƒ } | d j o qH q© t | t ƒ p | i | ƒ qH q© n | i | ƒ qH W| | (q
t | t ƒ oA | i | ƒ } | d j o t | | ƒ qt
| | | ƒ q
q
W| S( N( R4 R/ R R R R3 Rg R_ t appendt delattrRD ( Rd R$ R+ t old_valuet
new_valuesRW RE ( ( s /usr/lib64/python2.6/ast.pyRc s,
( R6 Rh Ri Rc ( ( ( s /usr/lib64/python2.6/ast.pyRj ô s "N( Ri t _astR R R* R
R R@ RH RL RN R4 RI R[ RM t objectRa Rj ( ( ( s /usr/lib64/python2.6/ast.pyt s
%