blob: 4b2063bd78f453453f55593e277aacf5e5910f7d [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
31def parse(expr, filename='<unknown>', mode='exec'):
32 """
33 Parse an expression into an AST node.
34 Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST).
35 """
36 return compile(expr, filename, mode, PyCF_ONLY_AST)
37
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
43 Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
44 and None.
45 """
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 """
162 if 'lineno' in node._attributes:
163 node.lineno = getattr(node, 'lineno', 0) + n
164 for child in walk(node):
165 if 'lineno' in child._attributes:
166 child.lineno = getattr(child, 'lineno', 0) + n
167 return node
168
169
170def iter_fields(node):
171 """
172 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
173 that is present on *node*.
174 """
175 for field in node._fields:
176 try:
177 yield field, getattr(node, field)
178 except AttributeError:
179 pass
180
181
182def iter_child_nodes(node):
183 """
184 Yield all direct child nodes of *node*, that is, all fields that are nodes
185 and all items of fields that are lists of nodes.
186 """
187 for name, field in iter_fields(node):
188 if isinstance(field, AST):
189 yield field
190 elif isinstance(field, list):
191 for item in field:
192 if isinstance(item, AST):
193 yield item
194
195
196def get_docstring(node, clean=True):
197 """
198 Return the docstring for the given node or None if no docstring can
199 be found. If the node provided does not have docstrings a TypeError
200 will be raised.
201 """
202 if not isinstance(node, (FunctionDef, ClassDef, Module)):
203 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
204 if node.body and isinstance(node.body[0], Expr) and \
205 isinstance(node.body[0].value, Str):
206 if clean:
207 import inspect
208 return inspect.cleandoc(node.body[0].value.s)
209 return node.body[0].value.s
210
211
212def walk(node):
213 """
214 Recursively yield all child nodes of *node*, in no specified order. This is
215 useful if you only want to modify nodes in place and don't care about the
216 context.
217 """
218 from collections import deque
219 todo = deque([node])
220 while todo:
221 node = todo.popleft()
222 todo.extend(iter_child_nodes(node))
223 yield node
224
225
226class NodeVisitor(object):
227 """
228 A node visitor base class that walks the abstract syntax tree and calls a
229 visitor function for every node found. This function may return a value
230 which is forwarded by the `visit` method.
231
232 This class is meant to be subclassed, with the subclass adding visitor
233 methods.
234
235 Per default the visitor functions for the nodes are ``'visit_'`` +
236 class name of the node. So a `TryFinally` node visit function would
237 be `visit_TryFinally`. This behavior can be changed by overriding
238 the `visit` method. If no visitor function exists for a node
239 (return value `None`) the `generic_visit` visitor is used instead.
240
241 Don't use the `NodeVisitor` if you want to apply changes to nodes during
242 traversing. For this a special visitor exists (`NodeTransformer`) that
243 allows modifications.
244 """
245
246 def visit(self, node):
247 """Visit a node."""
248 method = 'visit_' + node.__class__.__name__
249 visitor = getattr(self, method, self.generic_visit)
250 return visitor(node)
251
252 def generic_visit(self, node):
253 """Called if no explicit visitor function exists for a node."""
254 for field, value in iter_fields(node):
255 if isinstance(value, list):
256 for item in value:
257 if isinstance(item, AST):
258 self.visit(item)
259 elif isinstance(value, AST):
260 self.visit(value)
261
262
263class NodeTransformer(NodeVisitor):
264 """
265 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
266 allows modification of nodes.
267
268 The `NodeTransformer` will walk the AST and use the return value of the
269 visitor methods to replace or remove the old node. If the return value of
270 the visitor method is ``None``, the node will be removed from its location,
271 otherwise it is replaced with the return value. The return value may be the
272 original node in which case no replacement takes place.
273
274 Here is an example transformer that rewrites all occurrences of name lookups
275 (``foo``) to ``data['foo']``::
276
277 class RewriteName(NodeTransformer):
278
279 def visit_Name(self, node):
280 return copy_location(Subscript(
281 value=Name(id='data', ctx=Load()),
282 slice=Index(value=Str(s=node.id)),
283 ctx=node.ctx
284 ), node)
285
286 Keep in mind that if the node you're operating on has child nodes you must
287 either transform the child nodes yourself or call the :meth:`generic_visit`
288 method for the node first.
289
290 For nodes that were part of a collection of statements (that applies to all
291 statement nodes), the visitor may also return a list of nodes rather than
292 just a single node.
293
294 Usually you use the transformer like this::
295
296 node = YourTransformer().visit(node)
297 """
298
299 def generic_visit(self, node):
300 for field, old_value in iter_fields(node):
301 old_value = getattr(node, field, None)
302 if isinstance(old_value, list):
303 new_values = []
304 for value in old_value:
305 if isinstance(value, AST):
306 value = self.visit(value)
307 if value is None:
308 continue
309 elif not isinstance(value, AST):
310 new_values.extend(value)
311 continue
312 new_values.append(value)
313 old_value[:] = new_values
314 elif isinstance(old_value, AST):
315 new_node = self.visit(old_value)
316 if new_node is None:
317 delattr(node, field)
318 else:
319 setattr(node, field, new_node)
320 return node