blob: 03b8a1b16b7a8c43304701a3a80049e85488091c [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 """
Georg Brandl0c77a822008-06-10 16:37:50 +000045 if isinstance(node_or_string, str):
46 node_or_string = parse(node_or_string, mode='eval')
47 if isinstance(node_or_string, Expression):
48 node_or_string = node_or_string.body
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +020049 def _convert_num(node):
50 if isinstance(node, Constant):
Serhiy Storchaka3f228112018-09-27 17:42:37 +030051 if type(node.value) in (int, float, complex):
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +020052 return node.value
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +020053 raise ValueError('malformed node or string: ' + repr(node))
54 def _convert_signed_num(node):
55 if isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
56 operand = _convert_num(node.operand)
57 if isinstance(node.op, UAdd):
58 return + operand
59 else:
60 return - operand
61 return _convert_num(node)
Georg Brandl0c77a822008-06-10 16:37:50 +000062 def _convert(node):
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010063 if isinstance(node, Constant):
64 return node.value
Georg Brandl0c77a822008-06-10 16:37:50 +000065 elif isinstance(node, Tuple):
66 return tuple(map(_convert, node.elts))
67 elif isinstance(node, List):
68 return list(map(_convert, node.elts))
Georg Brandl492f3fc2010-07-11 09:41:21 +000069 elif isinstance(node, Set):
70 return set(map(_convert, node.elts))
Georg Brandl0c77a822008-06-10 16:37:50 +000071 elif isinstance(node, Dict):
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +020072 return dict(zip(map(_convert, node.keys),
73 map(_convert, node.values)))
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010074 elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +020075 left = _convert_signed_num(node.left)
76 right = _convert_num(node.right)
77 if isinstance(left, (int, float)) and isinstance(right, complex):
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010078 if isinstance(node.op, Add):
79 return left + right
80 else:
81 return left - right
Serhiy Storchakad8ac4d12018-01-04 11:15:39 +020082 return _convert_signed_num(node)
Georg Brandl0c77a822008-06-10 16:37:50 +000083 return _convert(node_or_string)
84
85
86def dump(node, annotate_fields=True, include_attributes=False):
87 """
88 Return a formatted dump of the tree in *node*. This is mainly useful for
89 debugging purposes. The returned string will show the names and the values
90 for fields. This makes the code impossible to evaluate, so if evaluation is
91 wanted *annotate_fields* must be set to False. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +000092 numbers and column offsets are not dumped by default. If this is wanted,
Georg Brandl0c77a822008-06-10 16:37:50 +000093 *include_attributes* can be set to True.
94 """
95 def _format(node):
96 if isinstance(node, AST):
97 fields = [(a, _format(b)) for a, b in iter_fields(node)]
98 rv = '%s(%s' % (node.__class__.__name__, ', '.join(
99 ('%s=%s' % field for field in fields)
100 if annotate_fields else
101 (b for a, b in fields)
102 ))
103 if include_attributes and node._attributes:
104 rv += fields and ', ' or ' '
105 rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
106 for a in node._attributes)
107 return rv + ')'
108 elif isinstance(node, list):
109 return '[%s]' % ', '.join(_format(x) for x in node)
110 return repr(node)
111 if not isinstance(node, AST):
112 raise TypeError('expected AST, got %r' % node.__class__.__name__)
113 return _format(node)
114
115
116def copy_location(new_node, old_node):
117 """
118 Copy source location (`lineno` and `col_offset` attributes) from
119 *old_node* to *new_node* if possible, and return *new_node*.
120 """
121 for attr in 'lineno', 'col_offset':
122 if attr in old_node._attributes and attr in new_node._attributes \
123 and hasattr(old_node, attr):
124 setattr(new_node, attr, getattr(old_node, attr))
125 return new_node
126
127
128def fix_missing_locations(node):
129 """
130 When you compile a node tree with compile(), the compiler expects lineno and
131 col_offset attributes for every node that supports them. This is rather
132 tedious to fill in for generated nodes, so this helper adds these attributes
133 recursively where not already set, by setting them to the values of the
134 parent node. It works recursively starting at *node*.
135 """
136 def _fix(node, lineno, col_offset):
137 if 'lineno' in node._attributes:
138 if not hasattr(node, 'lineno'):
139 node.lineno = lineno
140 else:
141 lineno = node.lineno
142 if 'col_offset' in node._attributes:
143 if not hasattr(node, 'col_offset'):
144 node.col_offset = col_offset
145 else:
146 col_offset = node.col_offset
147 for child in iter_child_nodes(node):
148 _fix(child, lineno, col_offset)
149 _fix(node, 1, 0)
150 return node
151
152
153def increment_lineno(node, n=1):
154 """
155 Increment the line number of each node in the tree starting at *node* by *n*.
156 This is useful to "move code" to a different location in a file.
157 """
Georg Brandl0c77a822008-06-10 16:37:50 +0000158 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.
Matthias Bussonnier41cea702017-02-23 22:44:19 -0800195
196 If *clean* is `True`, all tabs are expanded to spaces and any whitespace
197 that can be uniformly removed from the second line onwards is removed.
Georg Brandl0c77a822008-06-10 16:37:50 +0000198 """
Yury Selivanov2f07a662015-07-23 08:54:35 +0300199 if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
Georg Brandl0c77a822008-06-10 16:37:50 +0000200 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
Serhiy Storchaka08f127a2018-06-15 11:05:15 +0300201 if not(node.body and isinstance(node.body[0], Expr)):
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300202 return None
203 node = node.body[0].value
204 if isinstance(node, Str):
205 text = node.s
206 elif isinstance(node, Constant) and isinstance(node.value, str):
207 text = node.value
208 else:
209 return None
Serhiy Storchaka08f127a2018-06-15 11:05:15 +0300210 if clean:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100211 import inspect
212 text = inspect.cleandoc(text)
213 return text
Georg Brandl0c77a822008-06-10 16:37:50 +0000214
215
216def walk(node):
217 """
Georg Brandl619e7ba2011-01-09 07:38:51 +0000218 Recursively yield all descendant nodes in the tree starting at *node*
219 (including *node* itself), in no specified order. This is useful if you
220 only want to modify nodes in place and don't care about the context.
Georg Brandl0c77a822008-06-10 16:37:50 +0000221 """
222 from collections import deque
223 todo = deque([node])
224 while todo:
225 node = todo.popleft()
226 todo.extend(iter_child_nodes(node))
227 yield node
228
229
230class NodeVisitor(object):
231 """
232 A node visitor base class that walks the abstract syntax tree and calls a
233 visitor function for every node found. This function may return a value
234 which is forwarded by the `visit` method.
235
236 This class is meant to be subclassed, with the subclass adding visitor
237 methods.
238
239 Per default the visitor functions for the nodes are ``'visit_'`` +
240 class name of the node. So a `TryFinally` node visit function would
241 be `visit_TryFinally`. This behavior can be changed by overriding
242 the `visit` method. If no visitor function exists for a node
243 (return value `None`) the `generic_visit` visitor is used instead.
244
245 Don't use the `NodeVisitor` if you want to apply changes to nodes during
246 traversing. For this a special visitor exists (`NodeTransformer`) that
247 allows modifications.
248 """
249
250 def visit(self, node):
251 """Visit a node."""
252 method = 'visit_' + node.__class__.__name__
253 visitor = getattr(self, method, self.generic_visit)
254 return visitor(node)
255
256 def generic_visit(self, node):
257 """Called if no explicit visitor function exists for a node."""
258 for field, value in iter_fields(node):
259 if isinstance(value, list):
260 for item in value:
261 if isinstance(item, AST):
262 self.visit(item)
263 elif isinstance(value, AST):
264 self.visit(value)
265
266
267class NodeTransformer(NodeVisitor):
268 """
269 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
270 allows modification of nodes.
271
272 The `NodeTransformer` will walk the AST and use the return value of the
273 visitor methods to replace or remove the old node. If the return value of
274 the visitor method is ``None``, the node will be removed from its location,
275 otherwise it is replaced with the return value. The return value may be the
276 original node in which case no replacement takes place.
277
278 Here is an example transformer that rewrites all occurrences of name lookups
279 (``foo``) to ``data['foo']``::
280
281 class RewriteName(NodeTransformer):
282
283 def visit_Name(self, node):
284 return copy_location(Subscript(
285 value=Name(id='data', ctx=Load()),
286 slice=Index(value=Str(s=node.id)),
287 ctx=node.ctx
288 ), node)
289
290 Keep in mind that if the node you're operating on has child nodes you must
291 either transform the child nodes yourself or call the :meth:`generic_visit`
292 method for the node first.
293
294 For nodes that were part of a collection of statements (that applies to all
295 statement nodes), the visitor may also return a list of nodes rather than
296 just a single node.
297
298 Usually you use the transformer like this::
299
300 node = YourTransformer().visit(node)
301 """
302
303 def generic_visit(self, node):
304 for field, old_value in iter_fields(node):
Georg Brandl0c77a822008-06-10 16:37:50 +0000305 if isinstance(old_value, list):
306 new_values = []
307 for value in old_value:
308 if isinstance(value, AST):
309 value = self.visit(value)
310 if value is None:
311 continue
312 elif not isinstance(value, AST):
313 new_values.extend(value)
314 continue
315 new_values.append(value)
316 old_value[:] = new_values
317 elif isinstance(old_value, AST):
318 new_node = self.visit(old_value)
319 if new_node is None:
320 delattr(node, field)
321 else:
322 setattr(node, field, new_node)
323 return node
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300324
325
326# The following code is for backward compatibility.
327# It will be removed in future.
328
329def _getter(self):
330 return self.value
331
332def _setter(self, value):
333 self.value = value
334
335Constant.n = property(_getter, _setter)
336Constant.s = property(_getter, _setter)
337
338class _ABC(type):
339
340 def __instancecheck__(cls, inst):
341 if not isinstance(inst, Constant):
342 return False
343 if cls in _const_types:
344 try:
345 value = inst.value
346 except AttributeError:
347 return False
348 else:
Anthony Sottile74176222019-01-18 11:30:28 -0800349 return (
350 isinstance(value, _const_types[cls]) and
351 not isinstance(value, _const_types_not.get(cls, ()))
352 )
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300353 return type.__instancecheck__(cls, inst)
354
355def _new(cls, *args, **kwargs):
356 if cls in _const_types:
357 return Constant(*args, **kwargs)
358 return Constant.__new__(cls, *args, **kwargs)
359
360class Num(Constant, metaclass=_ABC):
361 _fields = ('n',)
362 __new__ = _new
363
364class Str(Constant, metaclass=_ABC):
365 _fields = ('s',)
366 __new__ = _new
367
368class Bytes(Constant, metaclass=_ABC):
369 _fields = ('s',)
370 __new__ = _new
371
372class NameConstant(Constant, metaclass=_ABC):
373 __new__ = _new
374
375class Ellipsis(Constant, metaclass=_ABC):
376 _fields = ()
377
378 def __new__(cls, *args, **kwargs):
379 if cls is Ellipsis:
380 return Constant(..., *args, **kwargs)
381 return Constant.__new__(cls, *args, **kwargs)
382
383_const_types = {
384 Num: (int, float, complex),
385 Str: (str,),
386 Bytes: (bytes,),
387 NameConstant: (type(None), bool),
388 Ellipsis: (type(...),),
389}
Anthony Sottile74176222019-01-18 11:30:28 -0800390_const_types_not = {
391 Num: (bool,),
392}