Home | History | Annotate | Download | only in python2.7

Lines Matching refs:node

34     Parse the source into an AST node.
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
52 def _convert(node):
53 if isinstance(node, Str):
54 return node.s
55 elif isinstance(node, Num):
56 return node.n
57 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):
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]
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)):
73 left = node.left.n
74 right = node.right.n
75 if isinstance(node.op, Add):
83 def dump(node, annotate_fields=True, include_attributes=False):
85 Return a formatted dump of the tree in *node*. This is mainly useful for
92 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(
100 if include_attributes and node._attributes:
102 rv += ', '.join('%s=%s' % (a, _format(getattr(node, a)))
103 for a in node._attributes)
105 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)
125 def fix_missing_locations(node):
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
131 parent node. It works recursively starting at *node*.
133 def _fix(node, lineno, col_offset):
134 if 'lineno' in node._attributes:
135 if not hasattr(node, 'lineno'):
136 node.lineno = lineno
138 lineno = node.lineno
139 if 'col_offset' in node._attributes:
140 if not hasattr(node, 'col_offset'):
141 node.col_offset = col_offset
143 col_offset = node.col_offset
144 for child in iter_child_nodes(node):
146 _fix(node, 1, 0)
147 return node
150 def increment_lineno(node, n=1):
152 Increment the line number of each node in the tree starting at *node* by *n*.
155 for child in walk(node):
158 return node
161 def iter_fields(node):
163 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
164 that is present on *node*.
166 for field in node._fields:
168 yield field, getattr(node, field)
173 def iter_child_nodes(node):
175 Yield all direct child nodes of *node*, that is, all fields that are nodes
178 for name, field in iter_fields(node):
187 def get_docstring(node, clean=True):
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
193 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):
199 return inspect.cleandoc(node.body[0].value.s)
200 return node.body[0].value.s
203 def walk(node):
205 Recursively yield all descendant nodes in the tree starting at *node*
206 (including *node* itself), in no specified order. This is useful if you
210 todo = deque([node])
212 node = todo.popleft()
213 todo.extend(iter_child_nodes(node))
214 yield node
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
227 class name of the node. So a `TryFinally` node visit function would
229 the `visit` method. If no visitor function exists for a node
237 def visit(self, node):
238 """Visit a node."""
239 method = 'visit_' + node.__class__.__name__
241 return visitor(node)
243 def generic_visit(self, node):
244 """Called if no explicit visitor function exists for a node."""
245 for field, value in iter_fields(node):
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,
263 original node in which case no replacement takes place.
270 def visit_Name(self, node):
273 slice=Index(value=Str(s=node.id)),
274 ctx=node.ctx
275 ), node)
277 Keep in mind that if the node you're operating on has child nodes you must
279 method for the node first.
283 just a single node.
287 node = YourTransformer().visit(node)
290 def generic_visit(self, node):
291 for field, old_value in iter_fields(node):
292 old_value = getattr(node, field, None)
308 delattr(node, field)
310 setattr(node, field, new_node)
311 return node