blob: 7ea9b8ac6b2893a29ce261cfab3c52d348bc39eb [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Chetan Gaonker3ff8eae2016-04-12 14:50:26 -070017# -*- coding: utf-8 -*-
18"""
19 ast
20 ~~~
21
22 The `ast` module helps Python applications to process trees of the Python
23 abstract syntax grammar. The abstract syntax itself might change with
24 each Python release; this module helps to find out programmatically what
25 the current grammar looks like and allows modifications of it.
26
27 An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as
28 a flag to the `compile()` builtin function or by using the `parse()`
29 function from this module. The result will be a tree of objects whose
30 classes all inherit from `ast.AST`.
31
32 A modified abstract syntax tree can be compiled into a Python code object
33 using the built-in `compile()` function.
34
35 Additionally various helper functions are provided that make working with
36 the trees simpler. The main intention of the helper functions and this
37 module in general is to provide an easy to use interface for libraries
38 that work tightly with the python syntax (template engines for example).
39
40
41 :copyright: Copyright 2008 by Armin Ronacher.
42 :license: Python License.
43"""
44from _ast import *
45from _ast import __version__
46
47
48def parse(source, filename='<unknown>', mode='exec'):
49 """
50 Parse the source into an AST node.
51 Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
52 """
53 return compile(source, filename, mode, PyCF_ONLY_AST)
54
55
56def literal_eval(node_or_string):
57 """
58 Safely evaluate an expression node or a string containing a Python
59 expression. The string or node provided may only consist of the following
60 Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
61 and None.
62 """
63 _safe_names = {'None': None, 'True': True, 'False': False}
64 if isinstance(node_or_string, basestring):
65 node_or_string = parse(node_or_string, mode='eval')
66 if isinstance(node_or_string, Expression):
67 node_or_string = node_or_string.body
68 def _convert(node):
69 if isinstance(node, Str):
70 return node.s
71 elif isinstance(node, Num):
72 return node.n
73 elif isinstance(node, Tuple):
74 return tuple(map(_convert, node.elts))
75 elif isinstance(node, List):
76 return list(map(_convert, node.elts))
77 elif isinstance(node, Dict):
78 return dict((_convert(k), _convert(v)) for k, v
79 in zip(node.keys, node.values))
80 elif isinstance(node, Name):
81 if node.id in _safe_names:
82 return _safe_names[node.id]
83 elif isinstance(node, BinOp) and \
84 isinstance(node.op, (Add, Sub)) and \
85 isinstance(node.right, Num) and \
86 isinstance(node.right.n, complex) and \
87 isinstance(node.left, Num) and \
88 isinstance(node.left.n, (int, long, float)):
89 left = node.left.n
90 right = node.right.n
91 if isinstance(node.op, Add):
92 return left + right
93 else:
94 return left - right
95 raise ValueError('malformed string')
96 return _convert(node_or_string)
97
98
99def dump(node, annotate_fields=True, include_attributes=False):
100 """
101 Return a formatted dump of the tree in *node*. This is mainly useful for
102 debugging purposes. The returned string will show the names and the values
103 for fields. This makes the code impossible to evaluate, so if evaluation is
104 wanted *annotate_fields* must be set to False. Attributes such as line
105 numbers and column offsets are not dumped by default. If this is wanted,
106 *include_attributes* can be set to True.
107 """
108 def _format(node):
109 if isinstance(node, AST):
110 fields = [(a, _format(b)) for a, b in iter_fields(node)]
111 rv = '%s(%s' % (node.__class__.__name__, ', '.join(
112 ('%s=%s' % field for field in fields)
113 if annotate_fields else
114 (b for a, b in fields)
115 ))
116 if include_attributes and node._attributes:
117 rv += fields and ', ' or ' '
118 rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
119 for a in node._attributes)
120 return rv + ')'
121 elif isinstance(node, list):
122 return '[%s]' % ', '.join(_format(x) for x in node)
123 return repr(node)
124 if not isinstance(node, AST):
125 raise TypeError('expected AST, got %r' % node.__class__.__name__)
126 return _format(node)
127
128
129def copy_location(new_node, old_node):
130 """
131 Copy source location (`lineno` and `col_offset` attributes) from
132 *old_node* to *new_node* if possible, and return *new_node*.
133 """
134 for attr in 'lineno', 'col_offset':
135 if attr in old_node._attributes and attr in new_node._attributes \
136 and hasattr(old_node, attr):
137 setattr(new_node, attr, getattr(old_node, attr))
138 return new_node
139
140
141def fix_missing_locations(node):
142 """
143 When you compile a node tree with compile(), the compiler expects lineno and
144 col_offset attributes for every node that supports them. This is rather
145 tedious to fill in for generated nodes, so this helper adds these attributes
146 recursively where not already set, by setting them to the values of the
147 parent node. It works recursively starting at *node*.
148 """
149 def _fix(node, lineno, col_offset):
150 if 'lineno' in node._attributes:
151 if not hasattr(node, 'lineno'):
152 node.lineno = lineno
153 else:
154 lineno = node.lineno
155 if 'col_offset' in node._attributes:
156 if not hasattr(node, 'col_offset'):
157 node.col_offset = col_offset
158 else:
159 col_offset = node.col_offset
160 for child in iter_child_nodes(node):
161 _fix(child, lineno, col_offset)
162 _fix(node, 1, 0)
163 return node
164
165
166def increment_lineno(node, n=1):
167 """
168 Increment the line number of each node in the tree starting at *node* by *n*.
169 This is useful to "move code" to a different location in a file.
170 """
171 for child in walk(node):
172 if 'lineno' in child._attributes:
173 child.lineno = getattr(child, 'lineno', 0) + n
174 return node
175
176
177def iter_fields(node):
178 """
179 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
180 that is present on *node*.
181 """
182 for field in node._fields:
183 try:
184 yield field, getattr(node, field)
185 except AttributeError:
186 pass
187
188
189def iter_child_nodes(node):
190 """
191 Yield all direct child nodes of *node*, that is, all fields that are nodes
192 and all items of fields that are lists of nodes.
193 """
194 for name, field in iter_fields(node):
195 if isinstance(field, AST):
196 yield field
197 elif isinstance(field, list):
198 for item in field:
199 if isinstance(item, AST):
200 yield item
201
202
203def get_docstring(node, clean=True):
204 """
205 Return the docstring for the given node or None if no docstring can
206 be found. If the node provided does not have docstrings a TypeError
207 will be raised.
208 """
209 if not isinstance(node, (FunctionDef, ClassDef, Module)):
210 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
211 if node.body and isinstance(node.body[0], Expr) and \
212 isinstance(node.body[0].value, Str):
213 if clean:
214 import inspect
215 return inspect.cleandoc(node.body[0].value.s)
216 return node.body[0].value.s
217
218
219def walk(node):
220 """
221 Recursively yield all descendant nodes in the tree starting at *node*
222 (including *node* itself), in no specified order. This is useful if you
223 only want to modify nodes in place and don't care about the context.
224 """
225 from collections import deque
226 todo = deque([node])
227 while todo:
228 node = todo.popleft()
229 todo.extend(iter_child_nodes(node))
230 yield node
231
232
233class NodeVisitor(object):
234 """
235 A node visitor base class that walks the abstract syntax tree and calls a
236 visitor function for every node found. This function may return a value
237 which is forwarded by the `visit` method.
238
239 This class is meant to be subclassed, with the subclass adding visitor
240 methods.
241
242 Per default the visitor functions for the nodes are ``'visit_'`` +
243 class name of the node. So a `TryFinally` node visit function would
244 be `visit_TryFinally`. This behavior can be changed by overriding
245 the `visit` method. If no visitor function exists for a node
246 (return value `None`) the `generic_visit` visitor is used instead.
247
248 Don't use the `NodeVisitor` if you want to apply changes to nodes during
249 traversing. For this a special visitor exists (`NodeTransformer`) that
250 allows modifications.
251 """
252
253 def visit(self, node):
254 """Visit a node."""
255 method = 'visit_' + node.__class__.__name__
256 visitor = getattr(self, method, self.generic_visit)
257 return visitor(node)
258
259 def generic_visit(self, node):
260 """Called if no explicit visitor function exists for a node."""
261 for field, value in iter_fields(node):
262 if isinstance(value, list):
263 for item in value:
264 if isinstance(item, AST):
265 self.visit(item)
266 elif isinstance(value, AST):
267 self.visit(value)
268
269
270class NodeTransformer(NodeVisitor):
271 """
272 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
273 allows modification of nodes.
274
275 The `NodeTransformer` will walk the AST and use the return value of the
276 visitor methods to replace or remove the old node. If the return value of
277 the visitor method is ``None``, the node will be removed from its location,
278 otherwise it is replaced with the return value. The return value may be the
279 original node in which case no replacement takes place.
280
281 Here is an example transformer that rewrites all occurrences of name lookups
282 (``foo``) to ``data['foo']``::
283
284 class RewriteName(NodeTransformer):
285
286 def visit_Name(self, node):
287 return copy_location(Subscript(
288 value=Name(id='data', ctx=Load()),
289 slice=Index(value=Str(s=node.id)),
290 ctx=node.ctx
291 ), node)
292
293 Keep in mind that if the node you're operating on has child nodes you must
294 either transform the child nodes yourself or call the :meth:`generic_visit`
295 method for the node first.
296
297 For nodes that were part of a collection of statements (that applies to all
298 statement nodes), the visitor may also return a list of nodes rather than
299 just a single node.
300
301 Usually you use the transformer like this::
302
303 node = YourTransformer().visit(node)
304 """
305
306 def generic_visit(self, node):
307 for field, old_value in iter_fields(node):
308 old_value = getattr(node, field, None)
309 if isinstance(old_value, list):
310 new_values = []
311 for value in old_value:
312 if isinstance(value, AST):
313 value = self.visit(value)
314 if value is None:
315 continue
316 elif not isinstance(value, AST):
317 new_values.extend(value)
318 continue
319 new_values.append(value)
320 old_value[:] = new_values
321 elif isinstance(old_value, AST):
322 new_node = self.visit(old_value)
323 if new_node is None:
324 delattr(node, field)
325 else:
326 setattr(node, field, new_node)
327 return node