blob: aab61d763409e9ee8f8623c1a4703e8ba50c7a59 [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]
Benjamin Peterson058e31e2009-01-16 03:54:08 +000068 elif isinstance(node, BinOp) and \
69 isinstance(node.op, (Add, Sub)) and \
70 isinstance(node.right, Num) and \
71 isinstance(node.right.n, complex) and \
72 isinstance(node.left, Num) and \
73 isinstance(node.left.n, (int, float)):
74 left = node.left.n
75 right = node.right.n
76 if isinstance(node.op, Add):
77 return left + right
78 else:
79 return left - right
Georg Brandl0c77a822008-06-10 16:37:50 +000080 raise ValueError('malformed string')
81 return _convert(node_or_string)
82
83
84def dump(node, annotate_fields=True, include_attributes=False):
85 """
86 Return a formatted dump of the tree in *node*. This is mainly useful for
87 debugging purposes. The returned string will show the names and the values
88 for fields. This makes the code impossible to evaluate, so if evaluation is
89 wanted *annotate_fields* must be set to False. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +000090 numbers and column offsets are not dumped by default. If this is wanted,
Georg Brandl0c77a822008-06-10 16:37:50 +000091 *include_attributes* can be set to True.
92 """
93 def _format(node):
94 if isinstance(node, AST):
95 fields = [(a, _format(b)) for a, b in iter_fields(node)]
96 rv = '%s(%s' % (node.__class__.__name__, ', '.join(
97 ('%s=%s' % field for field in fields)
98 if annotate_fields else
99 (b for a, b in fields)
100 ))
101 if include_attributes and node._attributes:
102 rv += fields and ', ' or ' '
103 rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
104 for a in node._attributes)
105 return rv + ')'
106 elif isinstance(node, list):
107 return '[%s]' % ', '.join(_format(x) for x in node)
108 return repr(node)
109 if not isinstance(node, AST):
110 raise TypeError('expected AST, got %r' % node.__class__.__name__)
111 return _format(node)
112
113
114def copy_location(new_node, old_node):
115 """
116 Copy source location (`lineno` and `col_offset` attributes) from
117 *old_node* to *new_node* if possible, and return *new_node*.
118 """
119 for attr in 'lineno', 'col_offset':
120 if attr in old_node._attributes and attr in new_node._attributes \
121 and hasattr(old_node, attr):
122 setattr(new_node, attr, getattr(old_node, attr))
123 return new_node
124
125
126def fix_missing_locations(node):
127 """
128 When you compile a node tree with compile(), the compiler expects lineno and
129 col_offset attributes for every node that supports them. This is rather
130 tedious to fill in for generated nodes, so this helper adds these attributes
131 recursively where not already set, by setting them to the values of the
132 parent node. It works recursively starting at *node*.
133 """
134 def _fix(node, lineno, col_offset):
135 if 'lineno' in node._attributes:
136 if not hasattr(node, 'lineno'):
137 node.lineno = lineno
138 else:
139 lineno = node.lineno
140 if 'col_offset' in node._attributes:
141 if not hasattr(node, 'col_offset'):
142 node.col_offset = col_offset
143 else:
144 col_offset = node.col_offset
145 for child in iter_child_nodes(node):
146 _fix(child, lineno, col_offset)
147 _fix(node, 1, 0)
148 return node
149
150
151def increment_lineno(node, n=1):
152 """
153 Increment the line number of each node in the tree starting at *node* by *n*.
154 This is useful to "move code" to a different location in a file.
155 """
156 if 'lineno' in node._attributes:
157 node.lineno = getattr(node, 'lineno', 0) + n
158 for child in walk(node):
159 if 'lineno' in child._attributes:
160 child.lineno = getattr(child, 'lineno', 0) + n
161 return node
162
163
164def iter_fields(node):
165 """
166 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
167 that is present on *node*.
168 """
169 for field in node._fields:
170 try:
171 yield field, getattr(node, field)
172 except AttributeError:
173 pass
174
175
176def iter_child_nodes(node):
177 """
178 Yield all direct child nodes of *node*, that is, all fields that are nodes
179 and all items of fields that are lists of nodes.
180 """
181 for name, field in iter_fields(node):
182 if isinstance(field, AST):
183 yield field
184 elif isinstance(field, list):
185 for item in field:
186 if isinstance(item, AST):
187 yield item
188
189
190def get_docstring(node, clean=True):
191 """
192 Return the docstring for the given node or None if no docstring can
193 be found. If the node provided does not have docstrings a TypeError
194 will be raised.
195 """
196 if not isinstance(node, (FunctionDef, ClassDef, Module)):
197 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
198 if node.body and isinstance(node.body[0], Expr) and \
199 isinstance(node.body[0].value, Str):
200 if clean:
201 import inspect
202 return inspect.cleandoc(node.body[0].value.s)
203 return node.body[0].value.s
204
205
206def walk(node):
207 """
208 Recursively yield all child nodes of *node*, in no specified order. This is
209 useful if you only want to modify nodes in place and don't care about the
210 context.
211 """
212 from collections import deque
213 todo = deque([node])
214 while todo:
215 node = todo.popleft()
216 todo.extend(iter_child_nodes(node))
217 yield node
218
219
220class NodeVisitor(object):
221 """
222 A node visitor base class that walks the abstract syntax tree and calls a
223 visitor function for every node found. This function may return a value
224 which is forwarded by the `visit` method.
225
226 This class is meant to be subclassed, with the subclass adding visitor
227 methods.
228
229 Per default the visitor functions for the nodes are ``'visit_'`` +
230 class name of the node. So a `TryFinally` node visit function would
231 be `visit_TryFinally`. This behavior can be changed by overriding
232 the `visit` method. If no visitor function exists for a node
233 (return value `None`) the `generic_visit` visitor is used instead.
234
235 Don't use the `NodeVisitor` if you want to apply changes to nodes during
236 traversing. For this a special visitor exists (`NodeTransformer`) that
237 allows modifications.
238 """
239
240 def visit(self, node):
241 """Visit a node."""
242 method = 'visit_' + node.__class__.__name__
243 visitor = getattr(self, method, self.generic_visit)
244 return visitor(node)
245
246 def generic_visit(self, node):
247 """Called if no explicit visitor function exists for a node."""
248 for field, value in iter_fields(node):
249 if isinstance(value, list):
250 for item in value:
251 if isinstance(item, AST):
252 self.visit(item)
253 elif isinstance(value, AST):
254 self.visit(value)
255
256
257class NodeTransformer(NodeVisitor):
258 """
259 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
260 allows modification of nodes.
261
262 The `NodeTransformer` will walk the AST and use the return value of the
263 visitor methods to replace or remove the old node. If the return value of
264 the visitor method is ``None``, the node will be removed from its location,
265 otherwise it is replaced with the return value. The return value may be the
266 original node in which case no replacement takes place.
267
268 Here is an example transformer that rewrites all occurrences of name lookups
269 (``foo``) to ``data['foo']``::
270
271 class RewriteName(NodeTransformer):
272
273 def visit_Name(self, node):
274 return copy_location(Subscript(
275 value=Name(id='data', ctx=Load()),
276 slice=Index(value=Str(s=node.id)),
277 ctx=node.ctx
278 ), node)
279
280 Keep in mind that if the node you're operating on has child nodes you must
281 either transform the child nodes yourself or call the :meth:`generic_visit`
282 method for the node first.
283
284 For nodes that were part of a collection of statements (that applies to all
285 statement nodes), the visitor may also return a list of nodes rather than
286 just a single node.
287
288 Usually you use the transformer like this::
289
290 node = YourTransformer().visit(node)
291 """
292
293 def generic_visit(self, node):
294 for field, old_value in iter_fields(node):
295 old_value = getattr(node, field, None)
296 if isinstance(old_value, list):
297 new_values = []
298 for value in old_value:
299 if isinstance(value, AST):
300 value = self.visit(value)
301 if value is None:
302 continue
303 elif not isinstance(value, AST):
304 new_values.extend(value)
305 continue
306 new_values.append(value)
307 old_value[:] = new_values
308 elif isinstance(old_value, AST):
309 new_node = self.visit(old_value)
310 if new_node is None:
311 delattr(node, field)
312 else:
313 setattr(node, field, new_node)
314 return node