admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | # -*- 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 Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 22 | that work tightly with the python syntax (template engines for example). |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 23 | |
| 24 | |
| 25 | :copyright: Copyright 2008 by Armin Ronacher. |
| 26 | :license: Python License. |
| 27 | """ |
| 28 | from _ast import * |
| 29 | from _ast import __version__ |
| 30 | |
| 31 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 32 | def parse( source, filename='<unknown>', mode='exec' ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | """ |
| 34 | Parse the source into an AST node. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 35 | Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 36 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 37 | return compile( source, filename, mode, PyCF_ONLY_AST ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 38 | |
| 39 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 40 | def literal_eval( node_or_string ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 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 |
| 44 | Python literal structures: strings, numbers, tuples, lists, dicts, booleans, |
| 45 | and None. |
| 46 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 47 | _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 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 51 | node_or_string = node_or_string.body |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 52 | |
| 53 | def _convert( node ): |
| 54 | if isinstance( node, Str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 55 | return node.s |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 56 | elif isinstance( node, Num ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | return node.n |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 58 | elif isinstance( node, Tuple ): |
| 59 | return tuple( map( _convert, node.elts ) ) |
| 60 | elif isinstance( node, List ): |
| 61 | return list( map( _convert, node.elts ) ) |
| 62 | elif isinstance( node, Dict ): |
| 63 | return dict( ( _convert( k ), _convert( v ) ) for k, v in zip( node.keys, node.values ) ) |
| 64 | elif isinstance( node, Name ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 65 | if node.id in _safe_names: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 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 ) ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | left = node.left.n |
| 74 | right = node.right.n |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 75 | if isinstance( node.op, Add ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 76 | return left + right |
| 77 | else: |
| 78 | return left - right |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 79 | raise ValueError( 'malformed string' ) |
| 80 | return _convert( node_or_string ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | |
| 82 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 83 | def dump( node, annotate_fields=True, include_attributes=False ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | """ |
| 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 Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 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( |
| 96 | ( '%s=%s' % field for field in fields ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | if annotate_fields else |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 98 | ( b for a, b in fields ) |
| 99 | ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 100 | if include_attributes and node._attributes: |
| 101 | rv += fields and ', ' or ' ' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 102 | rv += ', '.join( '%s=%s' % ( a, _format( getattr( node, a ) ) ) for a in node._attributes ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 103 | return rv + ')' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 104 | elif isinstance( node, list ): |
| 105 | return '[%s]' % ', '.join( _format( x ) for x in node ) |
| 106 | return repr( node ) |
| 107 | if not isinstance( node, AST ): |
| 108 | raise TypeError( 'expected AST, got %r' % node.__class__.__name__ ) |
| 109 | return _format( node ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 110 | |
| 111 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 112 | def copy_location( new_node, old_node ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 113 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 114 | Copy source location (`lineno` and `col_offset` attributes) from |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 115 | *old_node* to *new_node* if possible, and return *new_node*. |
| 116 | """ |
| 117 | for attr in 'lineno', 'col_offset': |
| 118 | if attr in old_node._attributes and attr in new_node._attributes \ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 119 | and hasattr( old_node, attr ): |
| 120 | setattr( new_node, attr, getattr( old_node, attr ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 121 | return new_node |
| 122 | |
| 123 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 124 | def fix_missing_locations( node ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 125 | """ |
| 126 | When you compile a node tree with compile(), the compiler expects lineno and |
| 127 | col_offset attributes for every node that supports them. This is rather |
| 128 | tedious to fill in for generated nodes, so this helper adds these attributes |
| 129 | recursively where not already set, by setting them to the values of the |
| 130 | parent node. It works recursively starting at *node*. |
| 131 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 132 | def _fix( node, lineno, col_offset ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 133 | if 'lineno' in node._attributes: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 134 | if not hasattr( node, 'lineno' ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 135 | node.lineno = lineno |
| 136 | else: |
| 137 | lineno = node.lineno |
| 138 | if 'col_offset' in node._attributes: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 139 | if not hasattr( node, 'col_offset' ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 140 | node.col_offset = col_offset |
| 141 | else: |
| 142 | col_offset = node.col_offset |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 143 | for child in iter_child_nodes( node ): |
| 144 | _fix( child, lineno, col_offset ) |
| 145 | _fix( node, 1, 0 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 146 | return node |
| 147 | |
| 148 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 149 | def increment_lineno( node, n=1 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | """ |
| 151 | Increment the line number of each node in the tree starting at *node* by *n*. |
| 152 | This is useful to "move code" to a different location in a file. |
| 153 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 154 | for child in walk( node ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 155 | if 'lineno' in child._attributes: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 156 | child.lineno = getattr( child, 'lineno', 0 ) + n |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 157 | return node |
| 158 | |
| 159 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 160 | def iter_fields( node ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 161 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 162 | Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields`` |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 163 | that is present on *node*. |
| 164 | """ |
| 165 | for field in node._fields: |
| 166 | try: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 167 | yield field, getattr( node, field ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 168 | except AttributeError: |
| 169 | pass |
| 170 | |
| 171 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 172 | def iter_child_nodes( node ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 173 | """ |
| 174 | Yield all direct child nodes of *node*, that is, all fields that are nodes |
| 175 | and all items of fields that are lists of nodes. |
| 176 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 177 | for name, field in iter_fields( node ): |
| 178 | if isinstance( field, AST ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 179 | yield field |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 180 | elif isinstance( field, list ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | for item in field: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 182 | if isinstance( item, AST ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | yield item |
| 184 | |
| 185 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 186 | def get_docstring( node, clean=True ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 187 | """ |
| 188 | Return the docstring for the given node or None if no docstring can |
| 189 | be found. If the node provided does not have docstrings a TypeError |
| 190 | will be raised. |
| 191 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 192 | if not isinstance( node, ( FunctionDef, ClassDef, Module ) ): |
| 193 | raise TypeError( "%r can't have docstrings" % node.__class__.__name__ ) |
| 194 | if node.body and isinstance( node.body[ 0 ], Expr ) and \ |
| 195 | isinstance( node.body[ 0 ].value, Str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | if clean: |
| 197 | import inspect |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 198 | return inspect.cleandoc( node.body[ 0 ].value.s ) |
| 199 | return node.body[ 0 ].value.s |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 200 | |
| 201 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 202 | def walk( node ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 203 | """ |
| 204 | Recursively yield all descendant nodes in the tree starting at *node* |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 205 | (including *node* itself), in no specified order. This is useful if you |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 206 | only want to modify nodes in place and don't care about the context. |
| 207 | """ |
| 208 | from collections import deque |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 209 | todo = deque( [ node ] ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | while todo: |
| 211 | node = todo.popleft() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 212 | todo.extend( iter_child_nodes( node ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 213 | yield node |
| 214 | |
| 215 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 216 | class NodeVisitor( object ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 217 | """ |
| 218 | A node visitor base class that walks the abstract syntax tree and calls a |
| 219 | visitor function for every node found. This function may return a value |
| 220 | which is forwarded by the `visit` method. |
| 221 | |
| 222 | This class is meant to be subclassed, with the subclass adding visitor |
| 223 | methods. |
| 224 | |
| 225 | Per default the visitor functions for the nodes are ``'visit_'`` + |
| 226 | class name of the node. So a `TryFinally` node visit function would |
| 227 | be `visit_TryFinally`. This behavior can be changed by overriding |
| 228 | the `visit` method. If no visitor function exists for a node |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 229 | (return value `None`) the `generic_visit` visitor is used instead. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 230 | |
| 231 | Don't use the `NodeVisitor` if you want to apply changes to nodes during |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 232 | traversing. For this a special visitor exists (`NodeTransformer`) that |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 233 | allows modifications. |
| 234 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 235 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 236 | def visit( self, node ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 237 | """Visit a node.""" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 238 | method = 'visit_' + node.__class__.__name__ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 239 | visitor = getattr( self, method, self.generic_visit ) |
| 240 | return visitor( node ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 241 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 242 | def generic_visit( self, node ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 243 | """Called if no explicit visitor function exists for a node.""" |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 244 | for field, value in iter_fields( node ): |
| 245 | if isinstance( value, list ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 246 | for item in value: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 247 | if isinstance( item, AST ): |
| 248 | self.visit( item ) |
| 249 | elif isinstance( value, AST ): |
| 250 | self.visit( value ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 251 | |
| 252 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 253 | class NodeTransformer( NodeVisitor ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 254 | """ |
| 255 | A :class:`NodeVisitor` subclass that walks the abstract syntax tree and |
| 256 | allows modification of nodes. |
| 257 | |
| 258 | The `NodeTransformer` will walk the AST and use the return value of the |
| 259 | visitor methods to replace or remove the old node. If the return value of |
| 260 | the visitor method is ``None``, the node will be removed from its location, |
| 261 | otherwise it is replaced with the return value. The return value may be the |
| 262 | original node in which case no replacement takes place. |
| 263 | |
| 264 | Here is an example transformer that rewrites all occurrences of name lookups |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 265 | (``foo``) to ``data['foo']``:: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 266 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 267 | class RewriteName(NodeTransformer): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 268 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 269 | def visit_Name(self, node): |
| 270 | return copy_location(Subscript( |
| 271 | value=Name(id='data', ctx=Load()), |
| 272 | slice=Index(value=Str(s=node.id)), |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 273 | ctx=node.ctx |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 274 | ), node) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 275 | |
| 276 | Keep in mind that if the node you're operating on has child nodes you must |
| 277 | either transform the child nodes yourself or call the :meth:`generic_visit` |
| 278 | method for the node first. |
| 279 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 280 | For nodes that were part of a collection of statements (that applies to all |
| 281 | statement nodes), the visitor may also return a list of nodes rather than |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 282 | just a single node. |
| 283 | |
| 284 | Usually you use the transformer like this:: |
| 285 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 286 | node = YourTransformer().visit(node) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 287 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 288 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 289 | def generic_visit( self, node ): |
| 290 | for field, old_value in iter_fields( node ): |
| 291 | old_value = getattr( node, field, None ) |
| 292 | if isinstance( old_value, list ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 293 | new_values = [] |
| 294 | for value in old_value: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 295 | if isinstance( value, AST ): |
| 296 | value = self.visit( value ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 297 | if value is None: |
| 298 | continue |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 299 | elif not isinstance( value, AST ): |
| 300 | new_values.extend( value ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 301 | continue |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 302 | new_values.append( value ) |
| 303 | old_value[ : ] = new_values |
| 304 | elif isinstance( old_value, AST ): |
| 305 | new_node = self.visit( old_value ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 306 | if new_node is None: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 307 | delattr( node, field ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 308 | else: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 309 | setattr( node, field, new_node ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 310 | return node |