blob: 070c2bee7f9dee0b5704e4a1c5a17e915a22af02 [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
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010038_NUM_TYPES = (int, float, complex)
39
Georg Brandl0c77a822008-06-10 16:37:50 +000040def literal_eval(node_or_string):
41 """
42 Safely evaluate an expression node or a string containing a Python
43 expression. The string or node provided may only consist of the following
Éric Araujo2a83cc62011-04-17 19:10:27 +020044 Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
45 sets, booleans, and None.
Georg Brandl0c77a822008-06-10 16:37:50 +000046 """
Georg Brandl0c77a822008-06-10 16:37:50 +000047 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):
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010052 if isinstance(node, Constant):
53 return node.value
54 elif isinstance(node, (Str, Bytes)):
Georg Brandl0c77a822008-06-10 16:37:50 +000055 return node.s
56 elif isinstance(node, Num):
57 return node.n
58 elif isinstance(node, Tuple):
59 return tuple(map(_convert, node.elts))
60 elif isinstance(node, List):
61 return list(map(_convert, node.elts))
Georg Brandl492f3fc2010-07-11 09:41:21 +000062 elif isinstance(node, Set):
63 return set(map(_convert, node.elts))
Georg Brandl0c77a822008-06-10 16:37:50 +000064 elif isinstance(node, Dict):
65 return dict((_convert(k), _convert(v)) for k, v
66 in zip(node.keys, node.values))
Benjamin Peterson442f2092012-12-06 17:41:04 -050067 elif isinstance(node, NameConstant):
68 return node.value
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010069 elif isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
Raymond Hettingerbc959732010-10-08 00:47:45 +000070 operand = _convert(node.operand)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010071 if isinstance(operand, _NUM_TYPES):
72 if isinstance(node.op, UAdd):
73 return + operand
74 else:
75 return - operand
76 elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
Raymond Hettingerbc959732010-10-08 00:47:45 +000077 left = _convert(node.left)
78 right = _convert(node.right)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010079 if isinstance(left, _NUM_TYPES) and isinstance(right, _NUM_TYPES):
80 if isinstance(node.op, Add):
81 return left + right
82 else:
83 return left - right
Raymond Hettingerbc959732010-10-08 00:47:45 +000084 raise ValueError('malformed node or string: ' + repr(node))
Georg Brandl0c77a822008-06-10 16:37:50 +000085 return _convert(node_or_string)
86
87
88def dump(node, annotate_fields=True, include_attributes=False):
89 """
90 Return a formatted dump of the tree in *node*. This is mainly useful for
91 debugging purposes. The returned string will show the names and the values
92 for fields. This makes the code impossible to evaluate, so if evaluation is
93 wanted *annotate_fields* must be set to False. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +000094 numbers and column offsets are not dumped by default. If this is wanted,
Georg Brandl0c77a822008-06-10 16:37:50 +000095 *include_attributes* can be set to True.
96 """
97 def _format(node):
98 if isinstance(node, AST):
99 fields = [(a, _format(b)) for a, b in iter_fields(node)]
100 rv = '%s(%s' % (node.__class__.__name__, ', '.join(
101 ('%s=%s' % field for field in fields)
102 if annotate_fields else
103 (b for a, b in fields)
104 ))
105 if include_attributes and node._attributes:
106 rv += fields and ', ' or ' '
107 rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
108 for a in node._attributes)
109 return rv + ')'
110 elif isinstance(node, list):
111 return '[%s]' % ', '.join(_format(x) for x in node)
112 return repr(node)
113 if not isinstance(node, AST):
114 raise TypeError('expected AST, got %r' % node.__class__.__name__)
115 return _format(node)
116
117
118def copy_location(new_node, old_node):
119 """
120 Copy source location (`lineno` and `col_offset` attributes) from
121 *old_node* to *new_node* if possible, and return *new_node*.
122 """
123 for attr in 'lineno', 'col_offset':
124 if attr in old_node._attributes and attr in new_node._attributes \
125 and hasattr(old_node, attr):
126 setattr(new_node, attr, getattr(old_node, attr))
127 return new_node
128
129
130def fix_missing_locations(node):
131 """
132 When you compile a node tree with compile(), the compiler expects lineno and
133 col_offset attributes for every node that supports them. This is rather
134 tedious to fill in for generated nodes, so this helper adds these attributes
135 recursively where not already set, by setting them to the values of the
136 parent node. It works recursively starting at *node*.
137 """
138 def _fix(node, lineno, col_offset):
139 if 'lineno' in node._attributes:
140 if not hasattr(node, 'lineno'):
141 node.lineno = lineno
142 else:
143 lineno = node.lineno
144 if 'col_offset' in node._attributes:
145 if not hasattr(node, 'col_offset'):
146 node.col_offset = col_offset
147 else:
148 col_offset = node.col_offset
149 for child in iter_child_nodes(node):
150 _fix(child, lineno, col_offset)
151 _fix(node, 1, 0)
152 return node
153
154
155def increment_lineno(node, n=1):
156 """
157 Increment the line number of each node in the tree starting at *node* by *n*.
158 This is useful to "move code" to a different location in a file.
159 """
Georg Brandl0c77a822008-06-10 16:37:50 +0000160 for child in walk(node):
161 if 'lineno' in child._attributes:
162 child.lineno = getattr(child, 'lineno', 0) + n
163 return node
164
165
166def iter_fields(node):
167 """
168 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
169 that is present on *node*.
170 """
171 for field in node._fields:
172 try:
173 yield field, getattr(node, field)
174 except AttributeError:
175 pass
176
177
178def iter_child_nodes(node):
179 """
180 Yield all direct child nodes of *node*, that is, all fields that are nodes
181 and all items of fields that are lists of nodes.
182 """
183 for name, field in iter_fields(node):
184 if isinstance(field, AST):
185 yield field
186 elif isinstance(field, list):
187 for item in field:
188 if isinstance(item, AST):
189 yield item
190
191
192def get_docstring(node, clean=True):
193 """
194 Return the docstring for the given node or None if no docstring can
195 be found. If the node provided does not have docstrings a TypeError
196 will be raised.
Matthias Bussonnier41cea702017-02-23 22:44:19 -0800197
198 If *clean* is `True`, all tabs are expanded to spaces and any whitespace
199 that can be uniformly removed from the second line onwards is removed.
Georg Brandl0c77a822008-06-10 16:37:50 +0000200 """
Yury Selivanov2f07a662015-07-23 08:54:35 +0300201 if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
Georg Brandl0c77a822008-06-10 16:37:50 +0000202 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
INADA Naokicb41b272017-02-23 00:31:59 +0900203 text = node.docstring
Matthias Bussonnier41cea702017-02-23 22:44:19 -0800204 if clean and text:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100205 import inspect
206 text = inspect.cleandoc(text)
207 return text
Georg Brandl0c77a822008-06-10 16:37:50 +0000208
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):
Georg Brandl0c77a822008-06-10 16:37:50 +0000299 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