blob: fd5dfdba676d97d6b45ddab30fcf8e7169faea0c [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001# -*- coding: utf-8 -*-
2"""
3 ast
4 ~~~
5
6 The `ast` module helps Python applications to process trees of the Python
7 abstract syntax grammar. The abstract syntax itself might change with
8 each Python release; this module helps to find out programmatically what
9 the current grammar looks like and allows modifications of it.
10
11 An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as
12 a flag to the `compile()` builtin function or by using the `parse()`
13 function from this module. The result will be a tree of objects whose
14 classes all inherit from `ast.AST`.
15
16 A modified abstract syntax tree can be compiled into a Python code object
17 using the built-in `compile()` function.
18
19 Additionally various helper functions are provided that make working with
20 the trees simpler. The main intention of the helper functions and this
21 module in general is to provide an easy to use interface for libraries
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000022 that work tightly with the python syntax (template engines for example).
adminbae64d82013-08-01 10:50:15 -070023
24
25 :copyright: Copyright 2008 by Armin Ronacher.
26 :license: Python License.
27"""
28from _ast import *
29from _ast import __version__
30
31
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000032def parse(source, filename='<unknown>', mode='exec'):
adminbae64d82013-08-01 10:50:15 -070033 """
34 Parse the source into an AST node.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000035 Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
adminbae64d82013-08-01 10:50:15 -070036 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000037 return compile(source, filename, mode, PyCF_ONLY_AST)
adminbae64d82013-08-01 10:50:15 -070038
39
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000040def literal_eval(node_or_string):
adminbae64d82013-08-01 10:50:15 -070041 """
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
44 Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
45 and None.
46 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000047 _safe_names = {'None': None, 'True': True, 'False': False}
48 if isinstance(node_or_string, basestring):
49 node_or_string = parse(node_or_string, mode='eval')
50 if isinstance(node_or_string, Expression):
adminbae64d82013-08-01 10:50:15 -070051 node_or_string = node_or_string.body
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000052 def _convert(node):
53 if isinstance(node, Str):
adminbae64d82013-08-01 10:50:15 -070054 return node.s
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000055 elif isinstance(node, Num):
adminbae64d82013-08-01 10:50:15 -070056 return node.n
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000057 elif isinstance(node, Tuple):
58 return tuple(map(_convert, node.elts))
59 elif isinstance(node, List):
60 return list(map(_convert, node.elts))
61 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):
adminbae64d82013-08-01 10:50:15 -070065 if node.id in _safe_names:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000066 return _safe_names[node.id]
67 elif isinstance(node, BinOp) and \
68 isinstance(node.op, (Add, Sub)) and \
69 isinstance(node.right, Num) and \
70 isinstance(node.right.n, complex) and \
71 isinstance(node.left, Num) and \
72 isinstance(node.left.n, (int, long, float)):
adminbae64d82013-08-01 10:50:15 -070073 left = node.left.n
74 right = node.right.n
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000075 if isinstance(node.op, Add):
adminbae64d82013-08-01 10:50:15 -070076 return left + right
77 else:
78 return left - right
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000079 raise ValueError('malformed string')
80 return _convert(node_or_string)
adminbae64d82013-08-01 10:50:15 -070081
82
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000083def dump(node, annotate_fields=True, include_attributes=False):
adminbae64d82013-08-01 10:50:15 -070084 """
85 Return a formatted dump of the tree in *node*. This is mainly useful for
86 debugging purposes. The returned string will show the names and the values
87 for fields. This makes the code impossible to evaluate, so if evaluation is
88 wanted *annotate_fields* must be set to False. Attributes such as line
89 numbers and column offsets are not dumped by default. If this is wanted,
90 *include_attributes* can be set to True.
91 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000092 def _format(node):
93 if isinstance(node, AST):
94 fields = [(a, _format(b)) for a, b in iter_fields(node)]
95 rv = '%s(%s' % (node.__class__.__name__, ', '.join(
96 ('%s=%s' % field for field in fields)
adminbae64d82013-08-01 10:50:15 -070097 if annotate_fields else
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000098 (b for a, b in fields)
99 ))
adminbae64d82013-08-01 10:50:15 -0700100 if include_attributes and node._attributes:
101 rv += fields and ', ' or ' '
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000102 rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
103 for a in node._attributes)
adminbae64d82013-08-01 10:50:15 -0700104 return rv + ')'
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000105 elif isinstance(node, list):
106 return '[%s]' % ', '.join(_format(x) for x in node)
107 return repr(node)
108 if not isinstance(node, AST):
109 raise TypeError('expected AST, got %r' % node.__class__.__name__)
110 return _format(node)
adminbae64d82013-08-01 10:50:15 -0700111
112
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000113def copy_location(new_node, old_node):
adminbae64d82013-08-01 10:50:15 -0700114 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000115 Copy source location (`lineno` and `col_offset` attributes) from
adminbae64d82013-08-01 10:50:15 -0700116 *old_node* to *new_node* if possible, and return *new_node*.
117 """
118 for attr in 'lineno', 'col_offset':
119 if attr in old_node._attributes and attr in new_node._attributes \
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000120 and hasattr(old_node, attr):
121 setattr(new_node, attr, getattr(old_node, attr))
adminbae64d82013-08-01 10:50:15 -0700122 return new_node
123
124
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000125def fix_missing_locations(node):
adminbae64d82013-08-01 10:50:15 -0700126 """
127 When you compile a node tree with compile(), the compiler expects lineno and
128 col_offset attributes for every node that supports them. This is rather
129 tedious to fill in for generated nodes, so this helper adds these attributes
130 recursively where not already set, by setting them to the values of the
131 parent node. It works recursively starting at *node*.
132 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000133 def _fix(node, lineno, col_offset):
adminbae64d82013-08-01 10:50:15 -0700134 if 'lineno' in node._attributes:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000135 if not hasattr(node, 'lineno'):
adminbae64d82013-08-01 10:50:15 -0700136 node.lineno = lineno
137 else:
138 lineno = node.lineno
139 if 'col_offset' in node._attributes:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000140 if not hasattr(node, 'col_offset'):
adminbae64d82013-08-01 10:50:15 -0700141 node.col_offset = col_offset
142 else:
143 col_offset = node.col_offset
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000144 for child in iter_child_nodes(node):
145 _fix(child, lineno, col_offset)
146 _fix(node, 1, 0)
adminbae64d82013-08-01 10:50:15 -0700147 return node
148
149
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000150def increment_lineno(node, n=1):
adminbae64d82013-08-01 10:50:15 -0700151 """
152 Increment the line number of each node in the tree starting at *node* by *n*.
153 This is useful to "move code" to a different location in a file.
154 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000155 for child in walk(node):
adminbae64d82013-08-01 10:50:15 -0700156 if 'lineno' in child._attributes:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000157 child.lineno = getattr(child, 'lineno', 0) + n
adminbae64d82013-08-01 10:50:15 -0700158 return node
159
160
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000161def iter_fields(node):
adminbae64d82013-08-01 10:50:15 -0700162 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000163 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
adminbae64d82013-08-01 10:50:15 -0700164 that is present on *node*.
165 """
166 for field in node._fields:
167 try:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000168 yield field, getattr(node, field)
adminbae64d82013-08-01 10:50:15 -0700169 except AttributeError:
170 pass
171
172
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000173def iter_child_nodes(node):
adminbae64d82013-08-01 10:50:15 -0700174 """
175 Yield all direct child nodes of *node*, that is, all fields that are nodes
176 and all items of fields that are lists of nodes.
177 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000178 for name, field in iter_fields(node):
179 if isinstance(field, AST):
adminbae64d82013-08-01 10:50:15 -0700180 yield field
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000181 elif isinstance(field, list):
adminbae64d82013-08-01 10:50:15 -0700182 for item in field:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000183 if isinstance(item, AST):
adminbae64d82013-08-01 10:50:15 -0700184 yield item
185
186
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000187def get_docstring(node, clean=True):
adminbae64d82013-08-01 10:50:15 -0700188 """
189 Return the docstring for the given node or None if no docstring can
190 be found. If the node provided does not have docstrings a TypeError
191 will be raised.
192 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000193 if not isinstance(node, (FunctionDef, ClassDef, Module)):
194 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
195 if node.body and isinstance(node.body[0], Expr) and \
196 isinstance(node.body[0].value, Str):
adminbae64d82013-08-01 10:50:15 -0700197 if clean:
198 import inspect
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000199 return inspect.cleandoc(node.body[0].value.s)
200 return node.body[0].value.s
adminbae64d82013-08-01 10:50:15 -0700201
202
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000203def walk(node):
adminbae64d82013-08-01 10:50:15 -0700204 """
205 Recursively yield all descendant nodes in the tree starting at *node*
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000206 (including *node* itself), in no specified order. This is useful if you
adminbae64d82013-08-01 10:50:15 -0700207 only want to modify nodes in place and don't care about the context.
208 """
209 from collections import deque
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000210 todo = deque([node])
adminbae64d82013-08-01 10:50:15 -0700211 while todo:
212 node = todo.popleft()
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000213 todo.extend(iter_child_nodes(node))
adminbae64d82013-08-01 10:50:15 -0700214 yield node
215
216
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000217class NodeVisitor(object):
adminbae64d82013-08-01 10:50:15 -0700218 """
219 A node visitor base class that walks the abstract syntax tree and calls a
220 visitor function for every node found. This function may return a value
221 which is forwarded by the `visit` method.
222
223 This class is meant to be subclassed, with the subclass adding visitor
224 methods.
225
226 Per default the visitor functions for the nodes are ``'visit_'`` +
227 class name of the node. So a `TryFinally` node visit function would
228 be `visit_TryFinally`. This behavior can be changed by overriding
229 the `visit` method. If no visitor function exists for a node
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000230 (return value `None`) the `generic_visit` visitor is used instead.
adminbae64d82013-08-01 10:50:15 -0700231
232 Don't use the `NodeVisitor` if you want to apply changes to nodes during
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000233 traversing. For this a special visitor exists (`NodeTransformer`) that
adminbae64d82013-08-01 10:50:15 -0700234 allows modifications.
235 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000236
237 def visit(self, node):
238 """Visit a node."""
adminbae64d82013-08-01 10:50:15 -0700239 method = 'visit_' + node.__class__.__name__
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000240 visitor = getattr(self, method, self.generic_visit)
241 return visitor(node)
adminbae64d82013-08-01 10:50:15 -0700242
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000243 def generic_visit(self, node):
244 """Called if no explicit visitor function exists for a node."""
245 for field, value in iter_fields(node):
246 if isinstance(value, list):
adminbae64d82013-08-01 10:50:15 -0700247 for item in value:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000248 if isinstance(item, AST):
249 self.visit(item)
250 elif isinstance(value, AST):
251 self.visit(value)
adminbae64d82013-08-01 10:50:15 -0700252
253
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000254class NodeTransformer(NodeVisitor):
adminbae64d82013-08-01 10:50:15 -0700255 """
256 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
257 allows modification of nodes.
258
259 The `NodeTransformer` will walk the AST and use the return value of the
260 visitor methods to replace or remove the old node. If the return value of
261 the visitor method is ``None``, the node will be removed from its location,
262 otherwise it is replaced with the return value. The return value may be the
263 original node in which case no replacement takes place.
264
265 Here is an example transformer that rewrites all occurrences of name lookups
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000266 (``foo``) to ``data['foo']``::
adminbae64d82013-08-01 10:50:15 -0700267
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000268 class RewriteName(NodeTransformer):
adminbae64d82013-08-01 10:50:15 -0700269
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000270 def visit_Name(self, node):
271 return copy_location(Subscript(
272 value=Name(id='data', ctx=Load()),
273 slice=Index(value=Str(s=node.id)),
adminbae64d82013-08-01 10:50:15 -0700274 ctx=node.ctx
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000275 ), node)
adminbae64d82013-08-01 10:50:15 -0700276
277 Keep in mind that if the node you're operating on has child nodes you must
278 either transform the child nodes yourself or call the :meth:`generic_visit`
279 method for the node first.
280
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000281 For nodes that were part of a collection of statements (that applies to all
282 statement nodes), the visitor may also return a list of nodes rather than
adminbae64d82013-08-01 10:50:15 -0700283 just a single node.
284
285 Usually you use the transformer like this::
286
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000287 node = YourTransformer().visit(node)
adminbae64d82013-08-01 10:50:15 -0700288 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000289
290 def generic_visit(self, node):
291 for field, old_value in iter_fields(node):
292 old_value = getattr(node, field, None)
293 if isinstance(old_value, list):
adminbae64d82013-08-01 10:50:15 -0700294 new_values = []
295 for value in old_value:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000296 if isinstance(value, AST):
297 value = self.visit(value)
adminbae64d82013-08-01 10:50:15 -0700298 if value is None:
299 continue
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000300 elif not isinstance(value, AST):
301 new_values.extend(value)
adminbae64d82013-08-01 10:50:15 -0700302 continue
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000303 new_values.append(value)
304 old_value[:] = new_values
305 elif isinstance(old_value, AST):
306 new_node = self.visit(old_value)
adminbae64d82013-08-01 10:50:15 -0700307 if new_node is None:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000308 delattr(node, field)
adminbae64d82013-08-01 10:50:15 -0700309 else:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000310 setattr(node, field, new_node)
adminbae64d82013-08-01 10:50:15 -0700311 return node