blob: fb5adac8fd9ae8b6a990797fbf20e2647e5456ea [file] [log] [blame]
Georg Brandl0c77a822008-06-10 16:37:50 +00001"""
2 ast
3 ~~~
4
5 The `ast` module helps Python applications to process trees of the Python
6 abstract syntax grammar. The abstract syntax itself might change with
7 each Python release; this module helps to find out programmatically what
8 the current grammar looks like and allows modifications of it.
9
10 An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as
11 a flag to the `compile()` builtin function or by using the `parse()`
12 function from this module. The result will be a tree of objects whose
13 classes all inherit from `ast.AST`.
14
15 A modified abstract syntax tree can be compiled into a Python code object
16 using the built-in `compile()` function.
17
18 Additionally various helper functions are provided that make working with
19 the trees simpler. The main intention of the helper functions and this
20 module in general is to provide an easy to use interface for libraries
21 that work tightly with the python syntax (template engines for example).
22
23
24 :copyright: Copyright 2008 by Armin Ronacher.
25 :license: Python License.
26"""
27from _ast import *
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +000028from _ast import __version__
Georg Brandl0c77a822008-06-10 16:37:50 +000029
30
Terry Reedyfeac6242011-01-24 21:36:03 +000031def parse(source, filename='<unknown>', mode='exec'):
Georg Brandl0c77a822008-06-10 16:37:50 +000032 """
Terry Reedyfeac6242011-01-24 21:36:03 +000033 Parse the source into an AST node.
34 Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
Georg Brandl0c77a822008-06-10 16:37:50 +000035 """
Terry Reedyfeac6242011-01-24 21:36:03 +000036 return compile(source, filename, mode, PyCF_ONLY_AST)
Georg Brandl0c77a822008-06-10 16:37:50 +000037
38
39def literal_eval(node_or_string):
40 """
41 Safely evaluate an expression node or a string containing a Python
42 expression. The string or node provided may only consist of the following
Éric Araujo2a83cc62011-04-17 19:10:27 +020043 Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
44 sets, booleans, and None.
Georg Brandl0c77a822008-06-10 16:37:50 +000045 """
46 _safe_names = {'None': None, 'True': True, 'False': False}
47 if isinstance(node_or_string, str):
48 node_or_string = parse(node_or_string, mode='eval')
49 if isinstance(node_or_string, Expression):
50 node_or_string = node_or_string.body
51 def _convert(node):
Benjamin Peterson5ef96e52010-07-11 23:06:06 +000052 if isinstance(node, (Str, Bytes)):
Georg Brandl0c77a822008-06-10 16:37:50 +000053 return node.s
54 elif isinstance(node, Num):
55 return node.n
56 elif isinstance(node, Tuple):
57 return tuple(map(_convert, node.elts))
58 elif isinstance(node, List):
59 return list(map(_convert, node.elts))
Georg Brandl492f3fc2010-07-11 09:41:21 +000060 elif isinstance(node, Set):
61 return set(map(_convert, node.elts))
Georg Brandl0c77a822008-06-10 16:37:50 +000062 elif isinstance(node, Dict):
63 return dict((_convert(k), _convert(v)) for k, v
64 in zip(node.keys, node.values))
65 elif isinstance(node, Name):
66 if node.id in _safe_names:
67 return _safe_names[node.id]
Raymond Hettingerbc959732010-10-08 00:47:45 +000068 elif isinstance(node, UnaryOp) and \
69 isinstance(node.op, (UAdd, USub)) and \
70 isinstance(node.operand, (Num, UnaryOp, BinOp)):
71 operand = _convert(node.operand)
72 if isinstance(node.op, UAdd):
73 return + operand
74 else:
75 return - operand
Benjamin Peterson058e31e2009-01-16 03:54:08 +000076 elif isinstance(node, BinOp) and \
77 isinstance(node.op, (Add, Sub)) and \
Raymond Hettingerbc959732010-10-08 00:47:45 +000078 isinstance(node.right, (Num, UnaryOp, BinOp)) and \
79 isinstance(node.left, (Num, UnaryOp, BinOp)):
80 left = _convert(node.left)
81 right = _convert(node.right)
Benjamin Peterson058e31e2009-01-16 03:54:08 +000082 if isinstance(node.op, Add):
83 return left + right
84 else:
85 return left - right
Raymond Hettingerbc959732010-10-08 00:47:45 +000086 raise ValueError('malformed node or string: ' + repr(node))
Georg Brandl0c77a822008-06-10 16:37:50 +000087 return _convert(node_or_string)
88
89
90def dump(node, annotate_fields=True, include_attributes=False):
91 """
92 Return a formatted dump of the tree in *node*. This is mainly useful for
93 debugging purposes. The returned string will show the names and the values
94 for fields. This makes the code impossible to evaluate, so if evaluation is
95 wanted *annotate_fields* must be set to False. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +000096 numbers and column offsets are not dumped by default. If this is wanted,
Georg Brandl0c77a822008-06-10 16:37:50 +000097 *include_attributes* can be set to True.
98 """
99 def _format(node):
100 if isinstance(node, AST):
101 fields = [(a, _format(b)) for a, b in iter_fields(node)]
102 rv = '%s(%s' % (node.__class__.__name__, ', '.join(
103 ('%s=%s' % field for field in fields)
104 if annotate_fields else
105 (b for a, b in fields)
106 ))
107 if include_attributes and node._attributes:
108 rv += fields and ', ' or ' '
109 rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
110 for a in node._attributes)
111 return rv + ')'
112 elif isinstance(node, list):
113 return '[%s]' % ', '.join(_format(x) for x in node)
114 return repr(node)
115 if not isinstance(node, AST):
116 raise TypeError('expected AST, got %r' % node.__class__.__name__)
117 return _format(node)
118
119
120def copy_location(new_node, old_node):
121 """
122 Copy source location (`lineno` and `col_offset` attributes) from
123 *old_node* to *new_node* if possible, and return *new_node*.
124 """
125 for attr in 'lineno', 'col_offset':
126 if attr in old_node._attributes and attr in new_node._attributes \
127 and hasattr(old_node, attr):
128 setattr(new_node, attr, getattr(old_node, attr))
129 return new_node
130
131
132def fix_missing_locations(node):
133 """
134 When you compile a node tree with compile(), the compiler expects lineno and
135 col_offset attributes for every node that supports them. This is rather
136 tedious to fill in for generated nodes, so this helper adds these attributes
137 recursively where not already set, by setting them to the values of the
138 parent node. It works recursively starting at *node*.
139 """
140 def _fix(node, lineno, col_offset):
141 if 'lineno' in node._attributes:
142 if not hasattr(node, 'lineno'):
143 node.lineno = lineno
144 else:
145 lineno = node.lineno
146 if 'col_offset' in node._attributes:
147 if not hasattr(node, 'col_offset'):
148 node.col_offset = col_offset
149 else:
150 col_offset = node.col_offset
151 for child in iter_child_nodes(node):
152 _fix(child, lineno, col_offset)
153 _fix(node, 1, 0)
154 return node
155
156
157def increment_lineno(node, n=1):
158 """
159 Increment the line number of each node in the tree starting at *node* by *n*.
160 This is useful to "move code" to a different location in a file.
161 """
Georg Brandl0c77a822008-06-10 16:37:50 +0000162 for child in walk(node):
163 if 'lineno' in child._attributes:
164 child.lineno = getattr(child, 'lineno', 0) + n
165 return node
166
167
168def iter_fields(node):
169 """
170 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
171 that is present on *node*.
172 """
173 for field in node._fields:
174 try:
175 yield field, getattr(node, field)
176 except AttributeError:
177 pass
178
179
180def 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 for name, field in iter_fields(node):
186 if isinstance(field, AST):
187 yield field
188 elif isinstance(field, list):
189 for item in field:
190 if isinstance(item, AST):
191 yield item
192
193
194def get_docstring(node, clean=True):
195 """
196 Return the docstring for the given node or None if no docstring can
197 be found. If the node provided does not have docstrings a TypeError
198 will be raised.
199 """
200 if not isinstance(node, (FunctionDef, ClassDef, Module)):
201 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
202 if node.body and isinstance(node.body[0], Expr) and \
203 isinstance(node.body[0].value, Str):
204 if clean:
205 import inspect
206 return inspect.cleandoc(node.body[0].value.s)
207 return node.body[0].value.s
208
209
210def walk(node):
211 """
Georg Brandl619e7ba2011-01-09 07:38:51 +0000212 Recursively yield all descendant nodes in the tree starting at *node*
213 (including *node* itself), in no specified order. This is useful if you
214 only want to modify nodes in place and don't care about the context.
Georg Brandl0c77a822008-06-10 16:37:50 +0000215 """
216 from collections import deque
217 todo = deque([node])
218 while todo:
219 node = todo.popleft()
220 todo.extend(iter_child_nodes(node))
221 yield node
222
223
224class NodeVisitor(object):
225 """
226 A node visitor base class that walks the abstract syntax tree and calls a
227 visitor function for every node found. This function may return a value
228 which is forwarded by the `visit` method.
229
230 This class is meant to be subclassed, with the subclass adding visitor
231 methods.
232
233 Per default the visitor functions for the nodes are ``'visit_'`` +
234 class name of the node. So a `TryFinally` node visit function would
235 be `visit_TryFinally`. This behavior can be changed by overriding
236 the `visit` method. If no visitor function exists for a node
237 (return value `None`) the `generic_visit` visitor is used instead.
238
239 Don't use the `NodeVisitor` if you want to apply changes to nodes during
240 traversing. For this a special visitor exists (`NodeTransformer`) that
241 allows modifications.
242 """
243
244 def visit(self, node):
245 """Visit a node."""
246 method = 'visit_' + node.__class__.__name__
247 visitor = getattr(self, method, self.generic_visit)
248 return visitor(node)
249
250 def generic_visit(self, node):
251 """Called if no explicit visitor function exists for a node."""
252 for field, value in iter_fields(node):
253 if isinstance(value, list):
254 for item in value:
255 if isinstance(item, AST):
256 self.visit(item)
257 elif isinstance(value, AST):
258 self.visit(value)
259
260
261class NodeTransformer(NodeVisitor):
262 """
263 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
264 allows modification of nodes.
265
266 The `NodeTransformer` will walk the AST and use the return value of the
267 visitor methods to replace or remove the old node. If the return value of
268 the visitor method is ``None``, the node will be removed from its location,
269 otherwise it is replaced with the return value. The return value may be the
270 original node in which case no replacement takes place.
271
272 Here is an example transformer that rewrites all occurrences of name lookups
273 (``foo``) to ``data['foo']``::
274
275 class RewriteName(NodeTransformer):
276
277 def visit_Name(self, node):
278 return copy_location(Subscript(
279 value=Name(id='data', ctx=Load()),
280 slice=Index(value=Str(s=node.id)),
281 ctx=node.ctx
282 ), node)
283
284 Keep in mind that if the node you're operating on has child nodes you must
285 either transform the child nodes yourself or call the :meth:`generic_visit`
286 method for the node first.
287
288 For nodes that were part of a collection of statements (that applies to all
289 statement nodes), the visitor may also return a list of nodes rather than
290 just a single node.
291
292 Usually you use the transformer like this::
293
294 node = YourTransformer().visit(node)
295 """
296
297 def generic_visit(self, node):
298 for field, old_value in iter_fields(node):
299 old_value = getattr(node, field, None)
300 if isinstance(old_value, list):
301 new_values = []
302 for value in old_value:
303 if isinstance(value, AST):
304 value = self.visit(value)
305 if value is None:
306 continue
307 elif not isinstance(value, AST):
308 new_values.extend(value)
309 continue
310 new_values.append(value)
311 old_value[:] = new_values
312 elif isinstance(old_value, AST):
313 new_node = self.visit(old_value)
314 if new_node is None:
315 delattr(node, field)
316 else:
317 setattr(node, field, new_node)
318 return node