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