Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/gast/gast.py: 30%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import sys as _sys
2import ast as _ast
3from ast import boolop, cmpop, excepthandler, expr, expr_context, operator
4from ast import slice, stmt, unaryop, mod, AST
5from ast import iter_child_nodes, walk
7try:
8 from ast import TypeIgnore
9except ImportError:
10 class TypeIgnore(AST):
11 pass
13try:
14 from ast import pattern
15except ImportError:
16 class pattern(AST):
17 pass
20try:
21 from ast import type_param
22except ImportError:
23 class type_param(AST):
24 pass
27def _make_node(Name, Fields, Attributes, Bases):
29 # This constructor is used a lot during conversion from ast to gast,
30 # then as the primary way to build ast nodes. So we tried to optimized it
31 # for speed and not for readability.
32 def create_node(self, *args, **kwargs):
33 if len(args) > len(Fields):
34 raise TypeError(
35 "{} constructor takes at most {} positional arguments".
36 format(Name, len(Fields)))
38 # it's faster to iterate rather than zipping or enumerate
39 for i in range(len(args)):
40 setattr(self, Fields[i], args[i])
41 if kwargs: # cold branch
42 self.__dict__.update(kwargs)
44 setattr(_sys.modules[__name__],
45 Name,
46 type(Name,
47 Bases,
48 {'__init__': create_node,
49 '_fields': Fields,
50 '_field_types': {},
51 '_attributes': Attributes}))
53def _fill_field_types(Name, FieldTypes):
54 node = getattr(_sys.modules[__name__], Name)
55 assert len(node._fields) == len(FieldTypes), Name
56 node._field_types.update(zip(node._fields, FieldTypes))
58_nodes = (
59 # mod
60 ('Module', (('body', 'type_ignores'), (), (mod,))),
61 ('Interactive', (('body',), (), (mod,))),
62 ('Expression', (('body',), (), (mod,))),
63 ('FunctionType', (('argtypes', 'returns'), (), (mod,))),
64 ('Suite', (('body',), (), (mod,))),
66 # stmt
67 ('FunctionDef', (('name', 'args', 'body', 'decorator_list', 'returns',
68 'type_comment', 'type_params'),
69 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
70 (stmt,))),
71 ('AsyncFunctionDef', (('name', 'args', 'body', 'decorator_list', 'returns',
72 'type_comment', 'type_params',),
73 ('lineno', 'col_offset',
74 'end_lineno', 'end_col_offset',),
75 (stmt,))),
76 ('ClassDef', (('name', 'bases', 'keywords', 'body', 'decorator_list',
77 'type_params',),
78 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
79 (stmt,))),
80 ('Return', (('value',),
81 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
82 (stmt,))),
83 ('Delete', (('targets',),
84 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
85 (stmt,))),
86 ('Assign', (('targets', 'value', 'type_comment'),
87 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
88 (stmt,))),
89 ('TypeAlias', (('name', 'type_params', 'value'),
90 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
91 (stmt,))),
92 ('AugAssign', (('target', 'op', 'value',),
93 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
94 (stmt,))),
95 ('AnnAssign', (('target', 'annotation', 'value', 'simple',),
96 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
97 (stmt,))),
98 ('Print', (('dest', 'values', 'nl',),
99 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
100 (stmt,))),
101 ('For', (('target', 'iter', 'body', 'orelse', 'type_comment'),
102 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
103 (stmt,))),
104 ('AsyncFor', (('target', 'iter', 'body', 'orelse', 'type_comment'),
105 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
106 (stmt,))),
107 ('While', (('test', 'body', 'orelse',),
108 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
109 (stmt,))),
110 ('If', (('test', 'body', 'orelse',),
111 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
112 (stmt,))),
113 ('With', (('items', 'body', 'type_comment'),
114 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
115 (stmt,))),
116 ('AsyncWith', (('items', 'body', 'type_comment'),
117 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
118 (stmt,))),
119 ('Match', (('subject', 'cases'),
120 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
121 (stmt,))),
122 ('Raise', (('exc', 'cause',),
123 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
124 (stmt,))),
125 ('Try', (('body', 'handlers', 'orelse', 'finalbody',),
126 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
127 (stmt,))),
128 ('TryStar', (('body', 'handlers', 'orelse', 'finalbody',),
129 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
130 (stmt,))),
131 ('Assert', (('test', 'msg',),
132 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
133 (stmt,))),
134 ('Import', (('names',),
135 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
136 (stmt,))),
137 ('ImportFrom', (('module', 'names', 'level',),
138 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
139 (stmt,))),
140 ('Exec', (('body', 'globals', 'locals',),
141 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
142 (stmt,))),
143 ('Global', (('names',),
144 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
145 (stmt,))),
146 ('Nonlocal', (('names',),
147 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
148 (stmt,))),
149 ('Expr', (('value',),
150 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
151 (stmt,))),
152 ('Pass', ((), ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
153 (stmt,))),
154 ('Break', ((), ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
155 (stmt,))),
156 ('Continue', ((),
157 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
158 (stmt,))),
160 # expr
162 ('BoolOp', (('op', 'values',),
163 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
164 (expr,))),
165 ('NamedExpr', (('target', 'value',),
166 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
167 (expr,))),
168 ('BinOp', (('left', 'op', 'right',),
169 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
170 (expr,))),
171 ('UnaryOp', (('op', 'operand',),
172 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
173 (expr,))),
174 ('Lambda', (('args', 'body',),
175 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
176 (expr,))),
177 ('IfExp', (('test', 'body', 'orelse',),
178 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
179 (expr,))),
180 ('Dict', (('keys', 'values',),
181 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
182 (expr,))),
183 ('Set', (('elts',),
184 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
185 (expr,))),
186 ('ListComp', (('elt', 'generators',),
187 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
188 (expr,))),
189 ('SetComp', (('elt', 'generators',),
190 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
191 (expr,))),
192 ('DictComp', (('key', 'value', 'generators',),
193 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
194 (expr,))),
195 ('GeneratorExp', (('elt', 'generators',),
196 ('lineno', 'col_offset',
197 'end_lineno', 'end_col_offset',),
198 (expr,))),
199 ('Await', (('value',),
200 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
201 (expr,))),
202 ('Yield', (('value',),
203 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
204 (expr,))),
205 ('YieldFrom', (('value',),
206 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
207 (expr,))),
208 ('Compare', (('left', 'ops', 'comparators',),
209 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
210 (expr,))),
211 ('Call', (('func', 'args', 'keywords',),
212 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
213 (expr,))),
214 ('Repr', (('value',),
215 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
216 (expr,))),
217 ('FormattedValue', (('value', 'conversion', 'format_spec',),
218 ('lineno', 'col_offset',
219 'end_lineno', 'end_col_offset',),
220 (expr,))),
221 ('Interpolation', (('value', 'str', 'conversion', 'format_spec',),
222 ('lineno', 'col_offset',
223 'end_lineno', 'end_col_offset',),
224 (expr,))),
225 ('JoinedStr', (('values',),
226 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
227 (expr,))),
228 ('TemplateStr', (('values',),
229 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
230 (expr,))),
231 ('Constant', (('value', 'kind'),
232 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
233 (expr,))),
234 ('Attribute', (('value', 'attr', 'ctx',),
235 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
236 (expr,))),
237 ('Subscript', (('value', 'slice', 'ctx',),
238 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
239 (expr,))),
240 ('Starred', (('value', 'ctx',),
241 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
242 (expr,))),
243 ('Name', (('id', 'ctx', 'annotation', 'type_comment'),
244 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
245 (expr,))),
246 ('List', (('elts', 'ctx',),
247 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
248 (expr,))),
249 ('Tuple', (('elts', 'ctx',),
250 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
251 (expr,))),
253 # expr_context
254 ('Load', ((), (), (expr_context,))),
255 ('Store', ((), (), (expr_context,))),
256 ('Del', ((), (), (expr_context,))),
257 ('AugLoad', ((), (), (expr_context,))),
258 ('AugStore', ((), (), (expr_context,))),
259 ('Param', ((), (), (expr_context,))),
261 # slice
262 ('Slice', (('lower', 'upper', 'step'),
263 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
264 (slice,))),
266 # boolop
267 ('And', ((), (), (boolop,))),
268 ('Or', ((), (), (boolop,))),
270 # operator
271 ('Add', ((), (), (operator,))),
272 ('Sub', ((), (), (operator,))),
273 ('Mult', ((), (), (operator,))),
274 ('MatMult', ((), (), (operator,))),
275 ('Div', ((), (), (operator,))),
276 ('Mod', ((), (), (operator,))),
277 ('Pow', ((), (), (operator,))),
278 ('LShift', ((), (), (operator,))),
279 ('RShift', ((), (), (operator,))),
280 ('BitOr', ((), (), (operator,))),
281 ('BitXor', ((), (), (operator,))),
282 ('BitAnd', ((), (), (operator,))),
283 ('FloorDiv', ((), (), (operator,))),
285 # unaryop
286 ('Invert', ((), (), (unaryop, AST,))),
287 ('Not', ((), (), (unaryop, AST,))),
288 ('UAdd', ((), (), (unaryop, AST,))),
289 ('USub', ((), (), (unaryop, AST,))),
291 # cmpop
292 ('Eq', ((), (), (cmpop,))),
293 ('NotEq', ((), (), (cmpop,))),
294 ('Lt', ((), (), (cmpop,))),
295 ('LtE', ((), (), (cmpop,))),
296 ('Gt', ((), (), (cmpop,))),
297 ('GtE', ((), (), (cmpop,))),
298 ('Is', ((), (), (cmpop,))),
299 ('IsNot', ((), (), (cmpop,))),
300 ('In', ((), (), (cmpop,))),
301 ('NotIn', ((), (), (cmpop,))),
303 # comprehension
304 ('comprehension', (('target', 'iter', 'ifs', 'is_async'), (), (AST,))),
306 # excepthandler
307 ('ExceptHandler', (('type', 'name', 'body'),
308 ('lineno', 'col_offset',
309 'end_lineno', 'end_col_offset'),
310 (excepthandler,))),
312 # arguments
313 ('arguments', (('args', 'posonlyargs', 'vararg', 'kwonlyargs',
314 'kw_defaults', 'kwarg', 'defaults'), (), (AST,))),
316 # keyword
317 ('keyword', (('arg', 'value'),
318 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset'),
319 (AST,))),
321 # alias
322 ('alias', (('name', 'asname'),
323 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset'),
324 (AST,))),
326 # withitem
327 ('withitem', (('context_expr', 'optional_vars'), (), (AST,))),
329 # match_case
330 ('match_case', (('pattern', 'guard', 'body'), (), (AST,))),
332 # pattern
333 ('MatchValue', (('value',),
334 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset'),
335 (pattern,))),
336 ('MatchSingleton', (('value',),
337 ('lineno', 'col_offset',
338 'end_lineno', 'end_col_offset'),
339 (pattern,))),
340 ('MatchSequence', (('patterns',),
341 ('lineno', 'col_offset',
342 'end_lineno', 'end_col_offset'),
343 (pattern,))),
344 ('MatchMapping', (('keys', 'patterns', 'rest'),
345 ('lineno', 'col_offset',
346 'end_lineno', 'end_col_offset'),
347 (pattern,))),
348 ('MatchClass', (('cls', 'patterns', 'kwd_attrs', 'kwd_patterns'),
349 ('lineno', 'col_offset',
350 'end_lineno', 'end_col_offset'),
351 (pattern,))),
352 ('MatchStar', (('name',),
353 ('lineno', 'col_offset',
354 'end_lineno', 'end_col_offset'),
355 (pattern,))),
356 ('MatchAs', (('pattern', 'name'),
357 ('lineno', 'col_offset',
358 'end_lineno', 'end_col_offset'),
359 (pattern,))),
360 ('MatchOr', (('patterns',),
361 ('lineno', 'col_offset',
362 'end_lineno', 'end_col_offset'),
363 (pattern,))),
365 # type_ignore
366 ('type_ignore', ((), ('lineno', 'tag'), (TypeIgnore,))),
368 # type_param
369 ('TypeVar', (('name', 'bound',),
370 ('lineno', 'col_offset',
371 'end_lineno', 'end_col_offset'),
372 (type_param,))),
373 ('ParamSpec', (('name',),
374 ('lineno', 'col_offset',
375 'end_lineno', 'end_col_offset'),
376 (type_param,))),
377 ('TypeVarTuple', (('name',),
378 ('lineno', 'col_offset',
379 'end_lineno', 'end_col_offset'),
380 (type_param,))),
381 )
383for _name, _descr in _nodes:
384 _make_node(_name, *_descr)
386# As an exception to gast rule that states that all nodes are identical for all
387# python version, we don't fill the field type for python with a version lower
388# than 3.10. Those version lack type support to be compatible with the more
389# modern representation anyway. The _field_types still exists though, but it's
390# always empty.
391if _sys.version_info >= (3, 10):
393 _node_types = (
394 # mod
395 ('Module', (list[stmt], list[type_ignore])),
396 ('Interactive', (list[stmt],)),
397 ('Expression', (expr,)),
398 ('FunctionType', ('argtypes', 'returns'),),
399 ('Suite', (list[stmt],),),
401 # stmt
402 ('FunctionDef', (str, arguments, list[stmt], list[expr], expr | None, str | None, list[type_param]),),
403 ('AsyncFunctionDef', (str, arguments, list[stmt], list[expr], expr | None, str | None, list[type_param]),),
404 ('ClassDef', (str, list[expr], list[keyword], list[stmt], list[expr], list[type_param])),
405 ('Return', (expr | None,)),
406 ('Delete', (list[expr],)),
407 ('Assign', (list[expr], expr, str | None),),
408 ('TypeAlias', (expr, list[type_param], expr),),
409 ('AugAssign', (expr, operator, expr), ),
410 ('AnnAssign', (expr, expr, expr | None, int), ),
411 ('Print', (expr | None, list[expr], bool), ),
412 ('For', (expr, expr, list[stmt], list[stmt], str | None), ),
413 ('AsyncFor', (expr, expr, list[stmt], list[stmt], str | None), ),
414 ('While', (expr, list[stmt], list[stmt]), ),
415 ('If', (expr, list[stmt], list[stmt]), ),
416 ('With', (list[withitem], list[stmt], str | None), ),
417 ('AsyncWith', (list[withitem], list[stmt], str | None), ),
418 ('Match', (expr, match_case), ),
419 ('Raise', (expr | None, expr | None), ),
420 ('Try', (list[stmt], list[excepthandler], list[stmt], list[stmt]), ),
421 ('TryStar', (list[stmt], list[excepthandler], list[stmt], list[stmt]), ),
422 ('Assert', (expr, expr | None), ),
423 ('Import', (list[alias],), ),
424 ('ImportFrom', (str|None, list[alias], int | None), ),
425 ('Exec', (expr, expr | None, expr | None), ),
426 ('Global', (list[str],), ),
427 ('Nonlocal', (list[str],), ),
428 ('Expr', (expr,), ),
430 # expr
432 ('BoolOp', (boolop, list[expr]), ),
433 ('NamedExpr', (expr, expr), ),
434 ('BinOp', (expr, operator, expr), ),
435 ('UnaryOp', (unaryop, expr), ),
436 ('Lambda', (arguments, expr), ),
437 ('IfExp', (expr, expr, expr), ),
438 ('Dict', (list[expr], list[expr]), ),
439 ('Set', (list[expr],), ),
440 ('ListComp', (expr, list[comprehension]), ),
441 ('SetComp', (expr, list[comprehension]), ),
442 ('DictComp', (expr, expr, list[comprehension]), ),
443 ('GeneratorExp', (expr, list[comprehension]), ),
444 ('Await', (expr,), ),
445 ('Yield', (expr | None,), ),
446 ('YieldFrom', (expr,), ),
447 ('Compare', (expr, list[cmpop], list[expr]), ),
448 ('Call', (expr, list[expr], list[keyword]), ),
449 ('Repr', (expr,), ),
450 ('FormattedValue', (expr, int, expr | None), ),
451 ('Interpolation', (expr, str, int, expr | None), ),
452 ('JoinedStr', (list[expr],), ),
453 ('TemplateStr', (list[expr],), ),
454 ('Constant', (object, str | None), ),
455 ('Attribute', (expr, str, expr_context), ),
456 ('Subscript', (expr, expr, expr_context), ),
457 ('Starred', (expr, expr_context), ),
458 ('Name', (str, expr_context, expr, str | None), ),
459 ('List', (list[expr], expr_context), ),
460 ('Tuple', (list[expr], expr_context), ),
461 ('Slice', (expr | None, expr | None, expr | None), ),
463 # comprehension
464 ('comprehension', (expr, expr, list[expr], int), ),
466 # excepthandler
467 ('ExceptHandler', (expr | None, str | None, list[stmt]), ),
469 # arguments
470 ('arguments', (list[expr], list[expr], expr | None, list[expr], list[expr], expr | None, list[expr]), ),
472 # keyword
473 ('keyword', (str | None, expr), ),
475 # alias
476 ('alias', (str, str | None), ),
478 # withitem
479 ('withitem', (expr, expr | None), ),
481 # match_case
482 ('match_case', (pattern, expr, list[stmt]), ),
484 # pattern
485 ('MatchValue', (expr,), ),
486 ('MatchSingleton', (object,), ),
487 ('MatchSequence', (list[pattern],), ),
488 ('MatchMapping', (list[expr], list[pattern], str | None), ),
489 ('MatchClass', (expr, list[pattern], list[str], list[pattern]), ),
490 ('MatchStar', (str | None,), ),
491 ('MatchAs', (pattern | None, str | None), ),
492 ('MatchOr', (list[pattern],), ),
494 # type_param
495 ('TypeVar', (str, expr | None), ),
496 ('ParamSpec', (str,), ),
497 ('TypeVarTuple', (str,), ),
498 )
500 for _name, _types in _node_types:
501 _fill_field_types(_name, _types)
503if _sys.version_info.major == 2:
504 from .ast2 import ast_to_gast, gast_to_ast
505if _sys.version_info.major == 3:
506 from .ast3 import ast_to_gast, gast_to_ast
509def parse(*args, **kwargs):
510 return ast_to_gast(_ast.parse(*args, **kwargs))
513def unparse(gast_obj):
514 from .unparser import unparse
515 return unparse(gast_obj)
518def literal_eval(node_or_string):
519 if isinstance(node_or_string, AST):
520 node_or_string = gast_to_ast(node_or_string)
521 return _ast.literal_eval(node_or_string)
524def get_docstring(node, clean=True):
525 if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
526 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
527 if not(node.body and isinstance(node.body[0], Expr)):
528 return None
529 node = node.body[0].value
530 if isinstance(node, Constant) and isinstance(node.value, str):
531 text = node.value
532 else:
533 return None
534 if clean:
535 import inspect
536 text = inspect.cleandoc(text)
537 return text
540# the following are directly imported from python3.8's Lib/ast.py #
542def copy_location(new_node, old_node):
543 """
544 Copy source location (`lineno`, `col_offset`, `end_lineno`, and
545 `end_col_offset` attributes) from *old_node* to *new_node* if possible,
546 and return *new_node*.
547 """
548 for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset':
549 if attr in old_node._attributes and attr in new_node._attributes \
550 and hasattr(old_node, attr):
551 setattr(new_node, attr, getattr(old_node, attr))
552 return new_node
555def fix_missing_locations(node):
556 """
557 When you compile a node tree with compile(), the compiler expects lineno
558 and col_offset attributes for every node that supports them. This is
559 rather tedious to fill in for generated nodes, so this helper adds these
560 attributes recursively where not already set, by setting them to the values
561 of the parent node. It works recursively starting at *node*.
562 """
563 def _fix(node, lineno, col_offset, end_lineno, end_col_offset):
564 if 'lineno' in node._attributes:
565 if not hasattr(node, 'lineno'):
566 node.lineno = lineno
567 else:
568 lineno = node.lineno
569 if 'end_lineno' in node._attributes:
570 if not hasattr(node, 'end_lineno'):
571 node.end_lineno = end_lineno
572 else:
573 end_lineno = node.end_lineno
574 if 'col_offset' in node._attributes:
575 if not hasattr(node, 'col_offset'):
576 node.col_offset = col_offset
577 else:
578 col_offset = node.col_offset
579 if 'end_col_offset' in node._attributes:
580 if not hasattr(node, 'end_col_offset'):
581 node.end_col_offset = end_col_offset
582 else:
583 end_col_offset = node.end_col_offset
584 for child in iter_child_nodes(node):
585 _fix(child, lineno, col_offset, end_lineno, end_col_offset)
586 _fix(node, 1, 0, 1, 0)
587 return node
590if _sys.version_info.major == 3 and _sys.version_info.minor >= 8:
591 get_source_segment = _ast.get_source_segment
592else:
593 # No end_lineno no end_col_offset info set for those version, so always
594 # return None
595 def get_source_segment(source, node, padded=False):
596 return None
599def increment_lineno(node, n=1):
600 """
601 Increment the line number and end line number of each node in the tree
602 starting at *node* by *n*. This is useful to "move code" to a different
603 location in a file.
604 """
605 for child in walk(node):
606 if 'lineno' in child._attributes:
607 child.lineno = (getattr(child, 'lineno', 0) or 0) + n
608 if 'end_lineno' in child._attributes:
609 child.end_lineno = (getattr(child, 'end_lineno', 0) or 0) + n
610 return node
612# Code import from Lib/ast.py
613#
614# minor changes: getattr(x, y, ...) is None => getattr(x, y, 42) is None
615#
616def dump(
617 node, annotate_fields=True, include_attributes=False,
618 # *, # removed for compatibility with python2 :-/
619 indent=None, show_empty=False,
620):
621 """
622 Return a formatted dump of the tree in node. This is mainly useful for
623 debugging purposes. If annotate_fields is true (by default),
624 the returned string will show the names and the values for fields.
625 If annotate_fields is false, the result string will be more compact by
626 omitting unambiguous field names. Attributes such as line
627 numbers and column offsets are not dumped by default. If this is wanted,
628 include_attributes can be set to true. If indent is a non-negative
629 integer or string, then the tree will be pretty-printed with that indent
630 level. None (the default) selects the single line representation.
631 If show_empty is False, then empty lists and fields that are None
632 will be omitted from the output for better readability.
633 """
634 def _format(node, level=0):
635 if indent is not None:
636 level += 1
637 prefix = '\n' + indent * level
638 sep = ',\n' + indent * level
639 else:
640 prefix = ''
641 sep = ', '
642 if isinstance(node, AST):
643 cls = type(node)
644 args = []
645 args_buffer = []
646 allsimple = True
647 keywords = annotate_fields
648 for name in node._fields:
649 try:
650 value = getattr(node, name)
651 except AttributeError:
652 keywords = True
653 continue
654 if value is None and getattr(cls, name, 42) is None:
655 keywords = True
656 continue
657 if not show_empty:
658 if value == []:
659 if not keywords:
660 args_buffer.append(repr(value))
661 continue
662 if not keywords:
663 args.extend(args_buffer)
664 args_buffer = []
665 value, simple = _format(value, level)
666 allsimple = allsimple and simple
667 if keywords:
668 args.append('%s=%s' % (name, value))
669 else:
670 args.append(value)
671 if include_attributes and node._attributes:
672 for name in node._attributes:
673 try:
674 value = getattr(node, name)
675 except AttributeError:
676 continue
677 if value is None and getattr(cls, name, 42) is None:
678 continue
679 value, simple = _format(value, level)
680 allsimple = allsimple and simple
681 args.append('%s=%s' % (name, value))
682 if allsimple and len(args) <= 3:
683 return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args
684 return '%s(%s%s)' % (node.__class__.__name__, prefix, sep.join(args)), False
685 elif isinstance(node, list):
686 if not node:
687 return '[]', True
688 return '[%s%s]' % (prefix, sep.join(_format(x, level)[0] for x in node)), False
689 return repr(node), True
691 if not isinstance(node, AST):
692 raise TypeError('expected AST, got %r' % node.__class__.__name__)
693 if indent is not None and not isinstance(indent, str):
694 indent = ' ' * indent
695 return _format(node)[0]