blob: d9b22127ca90aaf144ef613a797f62c9e3768312 [file] [log] [blame]
Éric Araujod478a462011-08-19 02:01:14 +02001:mod:`ast` --- Abstract Syntax Trees
2====================================
Georg Brandl8509db52008-06-10 07:45:28 +00003
4.. module:: ast
5 :synopsis: Abstract Syntax Tree classes and manipulation.
6
7.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
8.. sectionauthor:: Georg Brandl <georg@python.org>
9
10.. versionadded:: 2.5
11 The low-level ``_ast`` module containing only the node classes.
12
13.. versionadded:: 2.6
14 The high-level ``ast`` module containing all helpers.
15
16
17The :mod:`ast` module helps Python applications to process trees of the Python
18abstract syntax grammar. The abstract syntax itself might change with each
19Python release; this module helps to find out programmatically what the current
20grammar looks like.
21
Benjamin Petersond923f982008-11-08 16:55:33 +000022An abstract syntax tree can be generated by passing :data:`ast.PyCF_ONLY_AST` as
Georg Brandld7d4fd72009-07-26 14:37:28 +000023a flag to the :func:`compile` built-in function, or using the :func:`parse`
Georg Brandl8509db52008-06-10 07:45:28 +000024helper provided in this module. The result will be a tree of objects whose
Benjamin Petersond923f982008-11-08 16:55:33 +000025classes all inherit from :class:`ast.AST`. An abstract syntax tree can be
26compiled into a Python code object using the built-in :func:`compile` function.
Georg Brandl8509db52008-06-10 07:45:28 +000027
Georg Brandl8509db52008-06-10 07:45:28 +000028
Raymond Hettingere679a372010-11-05 23:58:42 +000029.. seealso::
30
31 Latest version of the `ast module Python source code
32 <http://svn.python.org/view/python/branches/release27-maint/Lib/ast.py?view=markup>`_
33
Georg Brandl8509db52008-06-10 07:45:28 +000034Node classes
35------------
36
37.. class:: AST
38
39 This is the base of all AST node classes. The actual node classes are
40 derived from the :file:`Parser/Python.asdl` file, which is reproduced
41 :ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C
42 module and re-exported in :mod:`ast`.
43
44 There is one class defined for each left-hand side symbol in the abstract
45 grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
46 there is one class defined for each constructor on the right-hand side; these
47 classes inherit from the classes for the left-hand side trees. For example,
48 :class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
49 with alternatives (aka "sums"), the left-hand side class is abstract: only
50 instances of specific constructor nodes are ever created.
51
52 .. attribute:: _fields
53
54 Each concrete class has an attribute :attr:`_fields` which gives the names
55 of all child nodes.
56
57 Each instance of a concrete class has one attribute for each child node,
58 of the type as defined in the grammar. For example, :class:`ast.BinOp`
59 instances have an attribute :attr:`left` of type :class:`ast.expr`.
60
61 If these attributes are marked as optional in the grammar (using a
62 question mark), the value might be ``None``. If the attributes can have
63 zero-or-more values (marked with an asterisk), the values are represented
64 as Python lists. All possible attributes must be present and have valid
65 values when compiling an AST with :func:`compile`.
66
67 .. attribute:: lineno
68 col_offset
69
70 Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
71 :attr:`lineno` and :attr:`col_offset` attributes. The :attr:`lineno` is
72 the line number of source text (1-indexed so the first line is line 1) and
73 the :attr:`col_offset` is the UTF-8 byte offset of the first token that
74 generated the node. The UTF-8 offset is recorded because the parser uses
75 UTF-8 internally.
76
77 The constructor of a class :class:`ast.T` parses its arguments as follows:
78
79 * If there are positional arguments, there must be as many as there are items
80 in :attr:`T._fields`; they will be assigned as attributes of these names.
81 * If there are keyword arguments, they will set the attributes of the same
82 names to the given values.
83
84 For example, to create and populate an :class:`ast.UnaryOp` node, you could
85 use ::
86
87 node = ast.UnaryOp()
88 node.op = ast.USub()
89 node.operand = ast.Num()
90 node.operand.n = 5
91 node.operand.lineno = 0
92 node.operand.col_offset = 0
93 node.lineno = 0
94 node.col_offset = 0
95
96 or the more compact ::
97
98 node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
99 lineno=0, col_offset=0)
100
Armin Ronacher482f3122008-06-10 20:52:19 +0000101 .. versionadded:: 2.6
102 The constructor as explained above was added. In Python 2.5 nodes had
103 to be created by calling the class constructor without arguments and
104 setting the attributes afterwards.
105
Georg Brandl8509db52008-06-10 07:45:28 +0000106
107.. _abstract-grammar:
108
109Abstract Grammar
110----------------
111
112The module defines a string constant ``__version__`` which is the decimal
113Subversion revision number of the file shown below.
114
115The abstract grammar is currently defined as follows:
116
117.. literalinclude:: ../../Parser/Python.asdl
118
119
120:mod:`ast` Helpers
121------------------
122
123.. versionadded:: 2.6
124
125Apart from the node classes, :mod:`ast` module defines these utility functions
126and classes for traversing abstract syntax trees:
127
Terry Reedy51581de2011-01-24 21:48:11 +0000128.. function:: parse(source, filename='<unknown>', mode='exec')
Georg Brandl8509db52008-06-10 07:45:28 +0000129
Terry Reedy51581de2011-01-24 21:48:11 +0000130 Parse the source into an AST node. Equivalent to ``compile(source,
Benjamin Petersond923f982008-11-08 16:55:33 +0000131 filename, mode, ast.PyCF_ONLY_AST)``.
Georg Brandl8509db52008-06-10 07:45:28 +0000132
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000133
Georg Brandl8509db52008-06-10 07:45:28 +0000134.. function:: literal_eval(node_or_string)
135
136 Safely evaluate an expression node or a string containing a Python
137 expression. The string or node provided may only consist of the following
138 Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
139 and ``None``.
140
141 This can be used for safely evaluating strings containing Python expressions
142 from untrusted sources without the need to parse the values oneself.
143
144
Armin Ronacher3079be52008-06-10 20:37:02 +0000145.. function:: get_docstring(node, clean=True)
Georg Brandl8509db52008-06-10 07:45:28 +0000146
147 Return the docstring of the given *node* (which must be a
148 :class:`FunctionDef`, :class:`ClassDef` or :class:`Module` node), or ``None``
149 if it has no docstring. If *clean* is true, clean up the docstring's
150 indentation with :func:`inspect.cleandoc`.
151
152
153.. function:: fix_missing_locations(node)
154
155 When you compile a node tree with :func:`compile`, the compiler expects
156 :attr:`lineno` and :attr:`col_offset` attributes for every node that supports
157 them. This is rather tedious to fill in for generated nodes, so this helper
158 adds these attributes recursively where not already set, by setting them to
159 the values of the parent node. It works recursively starting at *node*.
160
161
162.. function:: increment_lineno(node, n=1)
163
164 Increment the line number of each node in the tree starting at *node* by *n*.
165 This is useful to "move code" to a different location in a file.
166
167
168.. function:: copy_location(new_node, old_node)
169
170 Copy source location (:attr:`lineno` and :attr:`col_offset`) from *old_node*
171 to *new_node* if possible, and return *new_node*.
172
173
174.. function:: iter_fields(node)
175
176 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
177 that is present on *node*.
178
179
180.. function:: iter_child_nodes(node)
181
182 Yield all direct child nodes of *node*, that is, all fields that are nodes
183 and all items of fields that are lists of nodes.
184
185
186.. function:: walk(node)
187
Georg Brandl6adb9792011-01-09 07:53:14 +0000188 Recursively yield all descendant nodes in the tree starting at *node*
189 (including *node* itself), in no specified order. This is useful if you only
190 want to modify nodes in place and don't care about the context.
Georg Brandl8509db52008-06-10 07:45:28 +0000191
192
193.. class:: NodeVisitor()
194
195 A node visitor base class that walks the abstract syntax tree and calls a
196 visitor function for every node found. This function may return a value
Georg Brandle92818f2009-01-03 20:47:01 +0000197 which is forwarded by the :meth:`visit` method.
Georg Brandl8509db52008-06-10 07:45:28 +0000198
199 This class is meant to be subclassed, with the subclass adding visitor
200 methods.
201
202 .. method:: visit(node)
203
204 Visit a node. The default implementation calls the method called
205 :samp:`self.visit_{classname}` where *classname* is the name of the node
206 class, or :meth:`generic_visit` if that method doesn't exist.
207
208 .. method:: generic_visit(node)
209
210 This visitor calls :meth:`visit` on all children of the node.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000211
Georg Brandl8509db52008-06-10 07:45:28 +0000212 Note that child nodes of nodes that have a custom visitor method won't be
213 visited unless the visitor calls :meth:`generic_visit` or visits them
214 itself.
215
216 Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
217 during traversal. For this a special visitor exists
218 (:class:`NodeTransformer`) that allows modifications.
219
220
221.. class:: NodeTransformer()
222
223 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
224 allows modification of nodes.
225
Georg Brandle92818f2009-01-03 20:47:01 +0000226 The :class:`NodeTransformer` will walk the AST and use the return value of
227 the visitor methods to replace or remove the old node. If the return value
228 of the visitor method is ``None``, the node will be removed from its
229 location, otherwise it is replaced with the return value. The return value
230 may be the original node in which case no replacement takes place.
Georg Brandl8509db52008-06-10 07:45:28 +0000231
232 Here is an example transformer that rewrites all occurrences of name lookups
233 (``foo``) to ``data['foo']``::
234
235 class RewriteName(NodeTransformer):
236
237 def visit_Name(self, node):
238 return copy_location(Subscript(
239 value=Name(id='data', ctx=Load()),
240 slice=Index(value=Str(s=node.id)),
241 ctx=node.ctx
242 ), node)
243
244 Keep in mind that if the node you're operating on has child nodes you must
245 either transform the child nodes yourself or call the :meth:`generic_visit`
246 method for the node first.
247
248 For nodes that were part of a collection of statements (that applies to all
249 statement nodes), the visitor may also return a list of nodes rather than
250 just a single node.
251
252 Usually you use the transformer like this::
253
254 node = YourTransformer().visit(node)
255
256
257.. function:: dump(node, annotate_fields=True, include_attributes=False)
258
259 Return a formatted dump of the tree in *node*. This is mainly useful for
260 debugging purposes. The returned string will show the names and the values
261 for fields. This makes the code impossible to evaluate, so if evaluation is
262 wanted *annotate_fields* must be set to False. Attributes such as line
Georg Brandl924b8232008-06-21 14:26:19 +0000263 numbers and column offsets are not dumped by default. If this is wanted,
Georg Brandl8509db52008-06-10 07:45:28 +0000264 *include_attributes* can be set to ``True``.