Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/gast/gast.py: 28%
78 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
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
14def _make_node(Name, Fields, Attributes, Bases):
15 def create_node(self, *args, **kwargs):
16 nbparam = len(args) + len(kwargs)
17 assert nbparam in (0, len(Fields)), \
18 "Bad argument number for {}: {}, expecting {}".\
19 format(Name, nbparam, len(Fields))
20 self._fields = Fields
21 self._attributes = Attributes
22 for argname, argval in zip(self._fields, args):
23 setattr(self, argname, argval)
24 for argname, argval in kwargs.items():
25 assert argname in Fields, \
26 "Invalid Keyword argument for {}: {}".format(Name, argname)
27 setattr(self, argname, argval)
29 setattr(_sys.modules[__name__],
30 Name,
31 type(Name,
32 Bases,
33 {'__init__': create_node}))
36_nodes = (
37 # mod
38 ('Module', (('body', 'type_ignores'), (), (mod,))),
39 ('Interactive', (('body',), (), (mod,))),
40 ('Expression', (('body',), (), (mod,))),
41 ('FunctionType', (('argtypes', 'returns'), (), (mod,))),
42 ('Suite', (('body',), (), (mod,))),
44 # stmt
45 ('FunctionDef', (('name', 'args', 'body', 'decorator_list', 'returns',
46 'type_comment'),
47 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
48 (stmt,))),
49 ('AsyncFunctionDef', (('name', 'args', 'body',
50 'decorator_list', 'returns',
51 'type_comment'),
52 ('lineno', 'col_offset',
53 'end_lineno', 'end_col_offset',),
54 (stmt,))),
55 ('ClassDef', (('name', 'bases', 'keywords', 'body', 'decorator_list',),
56 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
57 (stmt,))),
58 ('Return', (('value',),
59 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
60 (stmt,))),
61 ('Delete', (('targets',),
62 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
63 (stmt,))),
64 ('Assign', (('targets', 'value',),
65 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
66 (stmt,))),
67 ('AugAssign', (('target', 'op', 'value',),
68 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
69 (stmt,))),
70 ('AnnAssign', (('target', 'annotation', 'value', 'simple',),
71 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
72 (stmt,))),
73 ('Print', (('dest', 'values', 'nl',),
74 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
75 (stmt,))),
76 ('For', (('target', 'iter', 'body', 'orelse', 'type_comment'),
77 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
78 (stmt,))),
79 ('AsyncFor', (('target', 'iter', 'body', 'orelse', 'type_comment'),
80 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
81 (stmt,))),
82 ('While', (('test', 'body', 'orelse',),
83 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
84 (stmt,))),
85 ('If', (('test', 'body', 'orelse',),
86 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
87 (stmt,))),
88 ('With', (('items', 'body', 'type_comment'),
89 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
90 (stmt,))),
91 ('AsyncWith', (('items', 'body', 'type_comment'),
92 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
93 (stmt,))),
94 ('Raise', (('exc', 'cause',),
95 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
96 (stmt,))),
97 ('Try', (('body', 'handlers', 'orelse', 'finalbody',),
98 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
99 (stmt,))),
100 ('Assert', (('test', 'msg',),
101 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
102 (stmt,))),
103 ('Import', (('names',),
104 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
105 (stmt,))),
106 ('ImportFrom', (('module', 'names', 'level',),
107 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
108 (stmt,))),
109 ('Exec', (('body', 'globals', 'locals',),
110 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
111 (stmt,))),
112 ('Global', (('names',),
113 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
114 (stmt,))),
115 ('Nonlocal', (('names',),
116 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
117 (stmt,))),
118 ('Expr', (('value',),
119 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
120 (stmt,))),
121 ('Pass', ((), ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
122 (stmt,))),
123 ('Break', ((), ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
124 (stmt,))),
125 ('Continue', ((),
126 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
127 (stmt,))),
129 # expr
131 ('BoolOp', (('op', 'values',),
132 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
133 (expr,))),
134 ('BinOp', (('left', 'op', 'right',),
135 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
136 (expr,))),
137 ('UnaryOp', (('op', 'operand',),
138 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
139 (expr,))),
140 ('Lambda', (('args', 'body',),
141 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
142 (expr,))),
143 ('IfExp', (('test', 'body', 'orelse',),
144 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
145 (expr,))),
146 ('Dict', (('keys', 'values',),
147 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
148 (expr,))),
149 ('Set', (('elts',),
150 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
151 (expr,))),
152 ('ListComp', (('elt', 'generators',),
153 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
154 (expr,))),
155 ('SetComp', (('elt', 'generators',),
156 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
157 (expr,))),
158 ('DictComp', (('key', 'value', 'generators',),
159 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
160 (expr,))),
161 ('GeneratorExp', (('elt', 'generators',),
162 ('lineno', 'col_offset',
163 'end_lineno', 'end_col_offset',),
164 (expr,))),
165 ('Await', (('value',),
166 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
167 (expr,))),
168 ('Yield', (('value',),
169 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
170 (expr,))),
171 ('YieldFrom', (('value',),
172 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
173 (expr,))),
174 ('Compare', (('left', 'ops', 'comparators',),
175 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
176 (expr,))),
177 ('Call', (('func', 'args', 'keywords',),
178 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
179 (expr,))),
180 ('Repr', (('value',),
181 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
182 (expr,))),
183 ('FormattedValue', (('value', 'conversion', 'format_spec',),
184 ('lineno', 'col_offset',
185 'end_lineno', 'end_col_offset',),
186 (expr,))),
187 ('JoinedStr', (('values',),
188 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
189 (expr,))),
190 ('Constant', (('value', 'kind'),
191 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
192 (expr,))),
193 ('Attribute', (('value', 'attr', 'ctx',),
194 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
195 (expr,))),
196 ('Subscript', (('value', 'slice', 'ctx',),
197 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
198 (expr,))),
199 ('Starred', (('value', 'ctx',),
200 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
201 (expr,))),
202 ('Name', (('id', 'ctx', 'annotation', 'type_comment'),
203 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
204 (expr,))),
205 ('List', (('elts', 'ctx',),
206 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
207 (expr,))),
208 ('Tuple', (('elts', 'ctx',),
209 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
210 (expr,))),
212 # expr_context
213 ('Load', ((), (), (expr_context,))),
214 ('Store', ((), (), (expr_context,))),
215 ('Del', ((), (), (expr_context,))),
216 ('AugLoad', ((), (), (expr_context,))),
217 ('AugStore', ((), (), (expr_context,))),
218 ('Param', ((), (), (expr_context,))),
220 # slice
221 ('Slice', (('lower', 'upper', 'step'),
222 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
223 (slice,))),
225 # boolop
226 ('And', ((), (), (boolop,))),
227 ('Or', ((), (), (boolop,))),
229 # operator
230 ('Add', ((), (), (operator,))),
231 ('Sub', ((), (), (operator,))),
232 ('Mult', ((), (), (operator,))),
233 ('MatMult', ((), (), (operator,))),
234 ('Div', ((), (), (operator,))),
235 ('Mod', ((), (), (operator,))),
236 ('Pow', ((), (), (operator,))),
237 ('LShift', ((), (), (operator,))),
238 ('RShift', ((), (), (operator,))),
239 ('BitOr', ((), (), (operator,))),
240 ('BitXor', ((), (), (operator,))),
241 ('BitAnd', ((), (), (operator,))),
242 ('FloorDiv', ((), (), (operator,))),
244 # unaryop
245 ('Invert', ((), (), (unaryop, AST,))),
246 ('Not', ((), (), (unaryop, AST,))),
247 ('UAdd', ((), (), (unaryop, AST,))),
248 ('USub', ((), (), (unaryop, AST,))),
250 # cmpop
251 ('Eq', ((), (), (cmpop,))),
252 ('NotEq', ((), (), (cmpop,))),
253 ('Lt', ((), (), (cmpop,))),
254 ('LtE', ((), (), (cmpop,))),
255 ('Gt', ((), (), (cmpop,))),
256 ('GtE', ((), (), (cmpop,))),
257 ('Is', ((), (), (cmpop,))),
258 ('IsNot', ((), (), (cmpop,))),
259 ('In', ((), (), (cmpop,))),
260 ('NotIn', ((), (), (cmpop,))),
262 # comprehension
263 ('comprehension', (('target', 'iter', 'ifs', 'is_async'), (), (AST,))),
265 # excepthandler
266 ('ExceptHandler', (('type', 'name', 'body'),
267 ('lineno', 'col_offset',
268 'end_lineno', 'end_col_offset'),
269 (excepthandler,))),
271 # arguments
272 ('arguments', (('args', 'posonlyargs', 'vararg', 'kwonlyargs',
273 'kw_defaults', 'kwarg', 'defaults'), (), (AST,))),
275 # keyword
276 ('keyword', (('arg', 'value'),
277 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset'),
278 (AST,))),
280 # alias
281 ('alias', (('name', 'asname'), (), (AST,))),
283 # withitem
284 ('withitem', (('context_expr', 'optional_vars'), (), (AST,))),
286 # type_ignore
287 ('type_ignore', ((), ('lineno', 'tag'), (TypeIgnore,))),
288 )
290for name, descr in _nodes:
291 _make_node(name, *descr)
293if _sys.version_info.major == 2:
294 from .ast2 import ast_to_gast, gast_to_ast
295if _sys.version_info.major == 3:
296 from .ast3 import ast_to_gast, gast_to_ast
299def parse(*args, **kwargs):
300 return ast_to_gast(_ast.parse(*args, **kwargs))
303def literal_eval(node_or_string):
304 if isinstance(node_or_string, AST):
305 node_or_string = gast_to_ast(node_or_string)
306 return _ast.literal_eval(node_or_string)
309def get_docstring(node, clean=True):
310 if not isinstance(node, (FunctionDef, ClassDef, Module)):
311 raise TypeError("%r can't have docstrings" % node.__class__.__name__)
312 if node.body and isinstance(node.body[0], Expr) and \
313 isinstance(node.body[0].value, Constant):
314 if clean:
315 import inspect
316 holder = node.body[0].value
317 return inspect.cleandoc(getattr(holder, holder._fields[0]))
318 return node.body[0].value.s
321# the following are directly imported from python3.8's Lib/ast.py #
323def copy_location(new_node, old_node):
324 """
325 Copy source location (`lineno`, `col_offset`, `end_lineno`, and
326 `end_col_offset` attributes) from *old_node* to *new_node* if possible,
327 and return *new_node*.
328 """
329 for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset':
330 if attr in old_node._attributes and attr in new_node._attributes \
331 and hasattr(old_node, attr):
332 setattr(new_node, attr, getattr(old_node, attr))
333 return new_node
336def fix_missing_locations(node):
337 """
338 When you compile a node tree with compile(), the compiler expects lineno
339 and col_offset attributes for every node that supports them. This is
340 rather tedious to fill in for generated nodes, so this helper adds these
341 attributes recursively where not already set, by setting them to the values
342 of the parent node. It works recursively starting at *node*.
343 """
344 def _fix(node, lineno, col_offset, end_lineno, end_col_offset):
345 if 'lineno' in node._attributes:
346 if not hasattr(node, 'lineno'):
347 node.lineno = lineno
348 else:
349 lineno = node.lineno
350 if 'end_lineno' in node._attributes:
351 if not hasattr(node, 'end_lineno'):
352 node.end_lineno = end_lineno
353 else:
354 end_lineno = node.end_lineno
355 if 'col_offset' in node._attributes:
356 if not hasattr(node, 'col_offset'):
357 node.col_offset = col_offset
358 else:
359 col_offset = node.col_offset
360 if 'end_col_offset' in node._attributes:
361 if not hasattr(node, 'end_col_offset'):
362 node.end_col_offset = end_col_offset
363 else:
364 end_col_offset = node.end_col_offset
365 for child in iter_child_nodes(node):
366 _fix(child, lineno, col_offset, end_lineno, end_col_offset)
367 _fix(node, 1, 0, 1, 0)
368 return node
371def increment_lineno(node, n=1):
372 """
373 Increment the line number and end line number of each node in the tree
374 starting at *node* by *n*. This is useful to "move code" to a different
375 location in a file.
376 """
377 for child in walk(node):
378 if 'lineno' in child._attributes:
379 child.lineno = (getattr(child, 'lineno', 0) or 0) + n
380 if 'end_lineno' in child._attributes:
381 child.end_lineno = (getattr(child, 'end_lineno', 0) or 0) + n
382 return node