blob: b5e08cbd53f68c218bc51958e16f1c081f46c16c [file] [log] [blame]
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00001:mod:`ast` --- Abstract Syntax Trees
2====================================
Georg Brandl0c77a822008-06-10 16:37:50 +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
Raymond Hettinger10480942011-01-10 03:26:08 +000010**Source code:** :source:`Lib/ast.py`
Georg Brandl0c77a822008-06-10 16:37:50 +000011
12The :mod:`ast` module helps Python applications to process trees of the Python
13abstract syntax grammar. The abstract syntax itself might change with each
14Python release; this module helps to find out programmatically what the current
15grammar looks like.
16
Benjamin Petersonec9199b2008-11-08 17:05:00 +000017An abstract syntax tree can be generated by passing :data:`ast.PyCF_ONLY_AST` as
Georg Brandl22b34312009-07-26 14:54:51 +000018a flag to the :func:`compile` built-in function, or using the :func:`parse`
Georg Brandl0c77a822008-06-10 16:37:50 +000019helper provided in this module. The result will be a tree of objects whose
Benjamin Petersonec9199b2008-11-08 17:05:00 +000020classes all inherit from :class:`ast.AST`. An abstract syntax tree can be
21compiled into a Python code object using the built-in :func:`compile` function.
Georg Brandl0c77a822008-06-10 16:37:50 +000022
Georg Brandl0c77a822008-06-10 16:37:50 +000023
24Node classes
25------------
26
27.. class:: AST
28
29 This is the base of all AST node classes. The actual node classes are
30 derived from the :file:`Parser/Python.asdl` file, which is reproduced
31 :ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C
32 module and re-exported in :mod:`ast`.
33
34 There is one class defined for each left-hand side symbol in the abstract
35 grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
36 there is one class defined for each constructor on the right-hand side; these
37 classes inherit from the classes for the left-hand side trees. For example,
38 :class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
39 with alternatives (aka "sums"), the left-hand side class is abstract: only
40 instances of specific constructor nodes are ever created.
41
42 .. attribute:: _fields
43
44 Each concrete class has an attribute :attr:`_fields` which gives the names
45 of all child nodes.
46
47 Each instance of a concrete class has one attribute for each child node,
48 of the type as defined in the grammar. For example, :class:`ast.BinOp`
49 instances have an attribute :attr:`left` of type :class:`ast.expr`.
50
51 If these attributes are marked as optional in the grammar (using a
52 question mark), the value might be ``None``. If the attributes can have
53 zero-or-more values (marked with an asterisk), the values are represented
54 as Python lists. All possible attributes must be present and have valid
55 values when compiling an AST with :func:`compile`.
56
57 .. attribute:: lineno
58 col_offset
59
60 Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
61 :attr:`lineno` and :attr:`col_offset` attributes. The :attr:`lineno` is
62 the line number of source text (1-indexed so the first line is line 1) and
63 the :attr:`col_offset` is the UTF-8 byte offset of the first token that
64 generated the node. The UTF-8 offset is recorded because the parser uses
65 UTF-8 internally.
66
67 The constructor of a class :class:`ast.T` parses its arguments as follows:
68
69 * If there are positional arguments, there must be as many as there are items
70 in :attr:`T._fields`; they will be assigned as attributes of these names.
71 * If there are keyword arguments, they will set the attributes of the same
72 names to the given values.
73
74 For example, to create and populate an :class:`ast.UnaryOp` node, you could
75 use ::
76
77 node = ast.UnaryOp()
78 node.op = ast.USub()
79 node.operand = ast.Num()
80 node.operand.n = 5
81 node.operand.lineno = 0
82 node.operand.col_offset = 0
83 node.lineno = 0
84 node.col_offset = 0
85
86 or the more compact ::
87
88 node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
89 lineno=0, col_offset=0)
90
91
92.. _abstract-grammar:
93
94Abstract Grammar
95----------------
96
97The module defines a string constant ``__version__`` which is the decimal
98Subversion revision number of the file shown below.
99
100The abstract grammar is currently defined as follows:
101
102.. literalinclude:: ../../Parser/Python.asdl
103
104
105:mod:`ast` Helpers
106------------------
107
Georg Brandl0c77a822008-06-10 16:37:50 +0000108Apart from the node classes, :mod:`ast` module defines these utility functions
109and classes for traversing abstract syntax trees:
110
111.. function:: parse(expr, filename='<unknown>', mode='exec')
112
113 Parse an expression into an AST node. Equivalent to ``compile(expr,
Benjamin Petersonec9199b2008-11-08 17:05:00 +0000114 filename, mode, ast.PyCF_ONLY_AST)``.
Georg Brandl0c77a822008-06-10 16:37:50 +0000115
Georg Brandl48310cd2009-01-03 21:18:54 +0000116
Georg Brandl0c77a822008-06-10 16:37:50 +0000117.. function:: literal_eval(node_or_string)
118
119 Safely evaluate an expression node or a string containing a Python
120 expression. The string or node provided may only consist of the following
Georg Brandl85f21772010-07-13 06:38:10 +0000121 Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
122 sets, booleans, and ``None``.
Georg Brandl0c77a822008-06-10 16:37:50 +0000123
124 This can be used for safely evaluating strings containing Python expressions
125 from untrusted sources without the need to parse the values oneself.
126
Georg Brandl492f3fc2010-07-11 09:41:21 +0000127 .. versionchanged:: 3.2
Georg Brandl85f21772010-07-13 06:38:10 +0000128 Now allows bytes and set literals.
Georg Brandl492f3fc2010-07-11 09:41:21 +0000129
Georg Brandl0c77a822008-06-10 16:37:50 +0000130
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +0000131.. function:: get_docstring(node, clean=True)
Georg Brandl0c77a822008-06-10 16:37:50 +0000132
133 Return the docstring of the given *node* (which must be a
134 :class:`FunctionDef`, :class:`ClassDef` or :class:`Module` node), or ``None``
135 if it has no docstring. If *clean* is true, clean up the docstring's
136 indentation with :func:`inspect.cleandoc`.
137
138
139.. function:: fix_missing_locations(node)
140
141 When you compile a node tree with :func:`compile`, the compiler expects
142 :attr:`lineno` and :attr:`col_offset` attributes for every node that supports
143 them. This is rather tedious to fill in for generated nodes, so this helper
144 adds these attributes recursively where not already set, by setting them to
145 the values of the parent node. It works recursively starting at *node*.
146
147
148.. function:: increment_lineno(node, n=1)
149
150 Increment the line number of each node in the tree starting at *node* by *n*.
151 This is useful to "move code" to a different location in a file.
152
153
154.. function:: copy_location(new_node, old_node)
155
156 Copy source location (:attr:`lineno` and :attr:`col_offset`) from *old_node*
157 to *new_node* if possible, and return *new_node*.
158
159
160.. function:: iter_fields(node)
161
162 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
163 that is present on *node*.
164
165
166.. function:: iter_child_nodes(node)
167
168 Yield all direct child nodes of *node*, that is, all fields that are nodes
169 and all items of fields that are lists of nodes.
170
171
172.. function:: walk(node)
173
Georg Brandl619e7ba2011-01-09 07:38:51 +0000174 Recursively yield all descendant nodes in the tree starting at *node*
175 (including *node* itself), in no specified order. This is useful if you only
176 want to modify nodes in place and don't care about the context.
Georg Brandl0c77a822008-06-10 16:37:50 +0000177
178
179.. class:: NodeVisitor()
180
181 A node visitor base class that walks the abstract syntax tree and calls a
182 visitor function for every node found. This function may return a value
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000183 which is forwarded by the :meth:`visit` method.
Georg Brandl0c77a822008-06-10 16:37:50 +0000184
185 This class is meant to be subclassed, with the subclass adding visitor
186 methods.
187
188 .. method:: visit(node)
189
190 Visit a node. The default implementation calls the method called
191 :samp:`self.visit_{classname}` where *classname* is the name of the node
192 class, or :meth:`generic_visit` if that method doesn't exist.
193
194 .. method:: generic_visit(node)
195
196 This visitor calls :meth:`visit` on all children of the node.
Georg Brandl48310cd2009-01-03 21:18:54 +0000197
Georg Brandl0c77a822008-06-10 16:37:50 +0000198 Note that child nodes of nodes that have a custom visitor method won't be
199 visited unless the visitor calls :meth:`generic_visit` or visits them
200 itself.
201
202 Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
203 during traversal. For this a special visitor exists
204 (:class:`NodeTransformer`) that allows modifications.
205
206
207.. class:: NodeTransformer()
208
209 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
210 allows modification of nodes.
211
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000212 The :class:`NodeTransformer` will walk the AST and use the return value of
213 the visitor methods to replace or remove the old node. If the return value
214 of the visitor method is ``None``, the node will be removed from its
215 location, otherwise it is replaced with the return value. The return value
216 may be the original node in which case no replacement takes place.
Georg Brandl0c77a822008-06-10 16:37:50 +0000217
218 Here is an example transformer that rewrites all occurrences of name lookups
219 (``foo``) to ``data['foo']``::
220
221 class RewriteName(NodeTransformer):
222
223 def visit_Name(self, node):
224 return copy_location(Subscript(
225 value=Name(id='data', ctx=Load()),
226 slice=Index(value=Str(s=node.id)),
227 ctx=node.ctx
228 ), node)
229
230 Keep in mind that if the node you're operating on has child nodes you must
231 either transform the child nodes yourself or call the :meth:`generic_visit`
232 method for the node first.
233
234 For nodes that were part of a collection of statements (that applies to all
235 statement nodes), the visitor may also return a list of nodes rather than
236 just a single node.
237
238 Usually you use the transformer like this::
239
240 node = YourTransformer().visit(node)
241
242
243.. function:: dump(node, annotate_fields=True, include_attributes=False)
244
245 Return a formatted dump of the tree in *node*. This is mainly useful for
246 debugging purposes. The returned string will show the names and the values
247 for fields. This makes the code impossible to evaluate, so if evaluation is
248 wanted *annotate_fields* must be set to False. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000249 numbers and column offsets are not dumped by default. If this is wanted,
Georg Brandl0c77a822008-06-10 16:37:50 +0000250 *include_attributes* can be set to ``True``.