Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/gast/gast.py: 48%

86 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-06 06:04 +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 

6 

7try: 

8 from ast import TypeIgnore 

9except ImportError: 

10 class TypeIgnore(AST): 

11 pass 

12 

13try: 

14 from ast import pattern 

15except ImportError: 

16 class pattern(AST): 

17 pass 

18 

19 

20def _make_node(Name, Fields, Attributes, Bases): 

21 NBFields = len(Fields) 

22 

23 def create_node(self, *args, **kwargs): 

24 if args: 

25 if len(args) + len([k for k in kwargs if k in Fields]) != NBFields: 

26 raise TypeError( 

27 "{} constructor takes either 0 or {} mandatory arguments". 

28 format(Name, NBFields)) 

29 for argname, argval in zip(Fields, args): 

30 setattr(self, argname, argval) 

31 if kwargs: 

32 for argname, argval in kwargs.items(): 

33 setattr(self, argname, argval) 

34 

35 setattr(_sys.modules[__name__], 

36 Name, 

37 type(Name, 

38 Bases, 

39 {'__init__': create_node, 

40 '_fields': Fields, 

41 '_attributes': Attributes})) 

42 

43 

44_nodes = ( 

45 # mod 

46 ('Module', (('body', 'type_ignores'), (), (mod,))), 

47 ('Interactive', (('body',), (), (mod,))), 

48 ('Expression', (('body',), (), (mod,))), 

49 ('FunctionType', (('argtypes', 'returns'), (), (mod,))), 

50 ('Suite', (('body',), (), (mod,))), 

51 

52 # stmt 

53 ('FunctionDef', (('name', 'args', 'body', 'decorator_list', 'returns', 

54 'type_comment'), 

55 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

56 (stmt,))), 

57 ('AsyncFunctionDef', (('name', 'args', 'body', 

58 'decorator_list', 'returns', 

59 'type_comment'), 

60 ('lineno', 'col_offset', 

61 'end_lineno', 'end_col_offset',), 

62 (stmt,))), 

63 ('ClassDef', (('name', 'bases', 'keywords', 'body', 'decorator_list',), 

64 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

65 (stmt,))), 

66 ('Return', (('value',), 

67 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

68 (stmt,))), 

69 ('Delete', (('targets',), 

70 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

71 (stmt,))), 

72 ('Assign', (('targets', 'value', 'type_comment'), 

73 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

74 (stmt,))), 

75 ('AugAssign', (('target', 'op', 'value',), 

76 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

77 (stmt,))), 

78 ('AnnAssign', (('target', 'annotation', 'value', 'simple',), 

79 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

80 (stmt,))), 

81 ('Print', (('dest', 'values', 'nl',), 

82 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

83 (stmt,))), 

84 ('For', (('target', 'iter', 'body', 'orelse', 'type_comment'), 

85 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

86 (stmt,))), 

87 ('AsyncFor', (('target', 'iter', 'body', 'orelse', 'type_comment'), 

88 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

89 (stmt,))), 

90 ('While', (('test', 'body', 'orelse',), 

91 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

92 (stmt,))), 

93 ('If', (('test', 'body', 'orelse',), 

94 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

95 (stmt,))), 

96 ('With', (('items', 'body', 'type_comment'), 

97 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

98 (stmt,))), 

99 ('AsyncWith', (('items', 'body', 'type_comment'), 

100 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

101 (stmt,))), 

102 ('Match', (('subject', 'cases'), 

103 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

104 (stmt,))), 

105 ('Raise', (('exc', 'cause',), 

106 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

107 (stmt,))), 

108 ('Try', (('body', 'handlers', 'orelse', 'finalbody',), 

109 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

110 (stmt,))), 

111 ('TryStar', (('body', 'handlers', 'orelse', 'finalbody',), 

112 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

113 (stmt,))), 

114 ('Assert', (('test', 'msg',), 

115 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

116 (stmt,))), 

117 ('Import', (('names',), 

118 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

119 (stmt,))), 

120 ('ImportFrom', (('module', 'names', 'level',), 

121 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

122 (stmt,))), 

123 ('Exec', (('body', 'globals', 'locals',), 

124 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

125 (stmt,))), 

126 ('Global', (('names',), 

127 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

128 (stmt,))), 

129 ('Nonlocal', (('names',), 

130 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

131 (stmt,))), 

132 ('Expr', (('value',), 

133 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

134 (stmt,))), 

135 ('Pass', ((), ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

136 (stmt,))), 

137 ('Break', ((), ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

138 (stmt,))), 

139 ('Continue', ((), 

140 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

141 (stmt,))), 

142 

143 # expr 

144 

145 ('BoolOp', (('op', 'values',), 

146 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

147 (expr,))), 

148 ('NamedExpr', (('target', 'value',), 

149 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

150 (expr,))), 

151 ('BinOp', (('left', 'op', 'right',), 

152 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

153 (expr,))), 

154 ('UnaryOp', (('op', 'operand',), 

155 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

156 (expr,))), 

157 ('Lambda', (('args', 'body',), 

158 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

159 (expr,))), 

160 ('IfExp', (('test', 'body', 'orelse',), 

161 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

162 (expr,))), 

163 ('Dict', (('keys', 'values',), 

164 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

165 (expr,))), 

166 ('Set', (('elts',), 

167 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

168 (expr,))), 

169 ('ListComp', (('elt', 'generators',), 

170 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

171 (expr,))), 

172 ('SetComp', (('elt', 'generators',), 

173 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

174 (expr,))), 

175 ('DictComp', (('key', 'value', 'generators',), 

176 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

177 (expr,))), 

178 ('GeneratorExp', (('elt', 'generators',), 

179 ('lineno', 'col_offset', 

180 'end_lineno', 'end_col_offset',), 

181 (expr,))), 

182 ('Await', (('value',), 

183 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

184 (expr,))), 

185 ('Yield', (('value',), 

186 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

187 (expr,))), 

188 ('YieldFrom', (('value',), 

189 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

190 (expr,))), 

191 ('Compare', (('left', 'ops', 'comparators',), 

192 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

193 (expr,))), 

194 ('Call', (('func', 'args', 'keywords',), 

195 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

196 (expr,))), 

197 ('Repr', (('value',), 

198 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

199 (expr,))), 

200 ('FormattedValue', (('value', 'conversion', 'format_spec',), 

201 ('lineno', 'col_offset', 

202 'end_lineno', 'end_col_offset',), 

203 (expr,))), 

204 ('JoinedStr', (('values',), 

205 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

206 (expr,))), 

207 ('Constant', (('value', 'kind'), 

208 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

209 (expr,))), 

210 ('Attribute', (('value', 'attr', 'ctx',), 

211 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

212 (expr,))), 

213 ('Subscript', (('value', 'slice', 'ctx',), 

214 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

215 (expr,))), 

216 ('Starred', (('value', 'ctx',), 

217 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

218 (expr,))), 

219 ('Name', (('id', 'ctx', 'annotation', 'type_comment'), 

220 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

221 (expr,))), 

222 ('List', (('elts', 'ctx',), 

223 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

224 (expr,))), 

225 ('Tuple', (('elts', 'ctx',), 

226 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

227 (expr,))), 

228 

229 # expr_context 

230 ('Load', ((), (), (expr_context,))), 

231 ('Store', ((), (), (expr_context,))), 

232 ('Del', ((), (), (expr_context,))), 

233 ('AugLoad', ((), (), (expr_context,))), 

234 ('AugStore', ((), (), (expr_context,))), 

235 ('Param', ((), (), (expr_context,))), 

236 

237 # slice 

238 ('Slice', (('lower', 'upper', 'step'), 

239 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',), 

240 (slice,))), 

241 

242 # boolop 

243 ('And', ((), (), (boolop,))), 

244 ('Or', ((), (), (boolop,))), 

245 

246 # operator 

247 ('Add', ((), (), (operator,))), 

248 ('Sub', ((), (), (operator,))), 

249 ('Mult', ((), (), (operator,))), 

250 ('MatMult', ((), (), (operator,))), 

251 ('Div', ((), (), (operator,))), 

252 ('Mod', ((), (), (operator,))), 

253 ('Pow', ((), (), (operator,))), 

254 ('LShift', ((), (), (operator,))), 

255 ('RShift', ((), (), (operator,))), 

256 ('BitOr', ((), (), (operator,))), 

257 ('BitXor', ((), (), (operator,))), 

258 ('BitAnd', ((), (), (operator,))), 

259 ('FloorDiv', ((), (), (operator,))), 

260 

261 # unaryop 

262 ('Invert', ((), (), (unaryop, AST,))), 

263 ('Not', ((), (), (unaryop, AST,))), 

264 ('UAdd', ((), (), (unaryop, AST,))), 

265 ('USub', ((), (), (unaryop, AST,))), 

266 

267 # cmpop 

268 ('Eq', ((), (), (cmpop,))), 

269 ('NotEq', ((), (), (cmpop,))), 

270 ('Lt', ((), (), (cmpop,))), 

271 ('LtE', ((), (), (cmpop,))), 

272 ('Gt', ((), (), (cmpop,))), 

273 ('GtE', ((), (), (cmpop,))), 

274 ('Is', ((), (), (cmpop,))), 

275 ('IsNot', ((), (), (cmpop,))), 

276 ('In', ((), (), (cmpop,))), 

277 ('NotIn', ((), (), (cmpop,))), 

278 

279 # comprehension 

280 ('comprehension', (('target', 'iter', 'ifs', 'is_async'), (), (AST,))), 

281 

282 # excepthandler 

283 ('ExceptHandler', (('type', 'name', 'body'), 

284 ('lineno', 'col_offset', 

285 'end_lineno', 'end_col_offset'), 

286 (excepthandler,))), 

287 

288 # arguments 

289 ('arguments', (('args', 'posonlyargs', 'vararg', 'kwonlyargs', 

290 'kw_defaults', 'kwarg', 'defaults'), (), (AST,))), 

291 

292 # keyword 

293 ('keyword', (('arg', 'value'), 

294 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset'), 

295 (AST,))), 

296 

297 # alias 

298 ('alias', (('name', 'asname'), 

299 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset'), 

300 (AST,))), 

301 

302 # withitem 

303 ('withitem', (('context_expr', 'optional_vars'), (), (AST,))), 

304 

305 # match_case 

306 ('match_case', (('pattern', 'guard', 'body'), (), (AST,))), 

307 

308 # pattern 

309 ('MatchValue', (('value',), 

310 ('lineno', 'col_offset', 'end_lineno', 'end_col_offset'), 

311 (pattern,))), 

312 ('MatchSingleton', (('value',), 

313 ('lineno', 'col_offset', 

314 'end_lineno', 'end_col_offset'), 

315 (pattern,))), 

316 ('MatchSequence', (('patterns',), 

317 ('lineno', 'col_offset', 

318 'end_lineno', 'end_col_offset'), 

319 (pattern,))), 

320 ('MatchMapping', (('keys', 'patterns', 'rest'), 

321 ('lineno', 'col_offset', 

322 'end_lineno', 'end_col_offset'), 

323 (pattern,))), 

324 ('MatchClass', (('cls', 'patterns', 'kwd_attrs', 'kwd_patterns'), 

325 ('lineno', 'col_offset', 

326 'end_lineno', 'end_col_offset'), 

327 (pattern,))), 

328 ('MatchStar', (('name',), 

329 ('lineno', 'col_offset', 

330 'end_lineno', 'end_col_offset'), 

331 (pattern,))), 

332 ('MatchAs', (('pattern', 'name'), 

333 ('lineno', 'col_offset', 

334 'end_lineno', 'end_col_offset'), 

335 (pattern,))), 

336 ('MatchOr', (('patterns',), 

337 ('lineno', 'col_offset', 

338 'end_lineno', 'end_col_offset'), 

339 (pattern,))), 

340 

341 # type_ignore 

342 ('type_ignore', ((), ('lineno', 'tag'), (TypeIgnore,))), 

343 ) 

344 

345for name, descr in _nodes: 

346 _make_node(name, *descr) 

347 

348if _sys.version_info.major == 2: 

349 from .ast2 import ast_to_gast, gast_to_ast 

350if _sys.version_info.major == 3: 

351 from .ast3 import ast_to_gast, gast_to_ast 

352 

353 

354def parse(*args, **kwargs): 

355 return ast_to_gast(_ast.parse(*args, **kwargs)) 

356 

357 

358def unparse(gast_obj): 

359 from .unparser import unparse 

360 return unparse(gast_obj) 

361 

362 

363def literal_eval(node_or_string): 

364 if isinstance(node_or_string, AST): 

365 node_or_string = gast_to_ast(node_or_string) 

366 return _ast.literal_eval(node_or_string) 

367 

368 

369def get_docstring(node, clean=True): 

370 if not isinstance(node, (FunctionDef, ClassDef, Module)): 

371 raise TypeError("%r can't have docstrings" % node.__class__.__name__) 

372 if node.body and isinstance(node.body[0], Expr) and \ 

373 isinstance(node.body[0].value, Constant): 

374 if clean: 

375 import inspect 

376 holder = node.body[0].value 

377 return inspect.cleandoc(getattr(holder, holder._fields[0])) 

378 return node.body[0].value.s 

379 

380 

381# the following are directly imported from python3.8's Lib/ast.py # 

382 

383def copy_location(new_node, old_node): 

384 """ 

385 Copy source location (`lineno`, `col_offset`, `end_lineno`, and 

386 `end_col_offset` attributes) from *old_node* to *new_node* if possible, 

387 and return *new_node*. 

388 """ 

389 for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset': 

390 if attr in old_node._attributes and attr in new_node._attributes \ 

391 and hasattr(old_node, attr): 

392 setattr(new_node, attr, getattr(old_node, attr)) 

393 return new_node 

394 

395 

396def fix_missing_locations(node): 

397 """ 

398 When you compile a node tree with compile(), the compiler expects lineno 

399 and col_offset attributes for every node that supports them. This is 

400 rather tedious to fill in for generated nodes, so this helper adds these 

401 attributes recursively where not already set, by setting them to the values 

402 of the parent node. It works recursively starting at *node*. 

403 """ 

404 def _fix(node, lineno, col_offset, end_lineno, end_col_offset): 

405 if 'lineno' in node._attributes: 

406 if not hasattr(node, 'lineno'): 

407 node.lineno = lineno 

408 else: 

409 lineno = node.lineno 

410 if 'end_lineno' in node._attributes: 

411 if not hasattr(node, 'end_lineno'): 

412 node.end_lineno = end_lineno 

413 else: 

414 end_lineno = node.end_lineno 

415 if 'col_offset' in node._attributes: 

416 if not hasattr(node, 'col_offset'): 

417 node.col_offset = col_offset 

418 else: 

419 col_offset = node.col_offset 

420 if 'end_col_offset' in node._attributes: 

421 if not hasattr(node, 'end_col_offset'): 

422 node.end_col_offset = end_col_offset 

423 else: 

424 end_col_offset = node.end_col_offset 

425 for child in iter_child_nodes(node): 

426 _fix(child, lineno, col_offset, end_lineno, end_col_offset) 

427 _fix(node, 1, 0, 1, 0) 

428 return node 

429 

430 

431def increment_lineno(node, n=1): 

432 """ 

433 Increment the line number and end line number of each node in the tree 

434 starting at *node* by *n*. This is useful to "move code" to a different 

435 location in a file. 

436 """ 

437 for child in walk(node): 

438 if 'lineno' in child._attributes: 

439 child.lineno = (getattr(child, 'lineno', 0) or 0) + n 

440 if 'end_lineno' in child._attributes: 

441 child.end_lineno = (getattr(child, 'end_lineno', 0) or 0) + n 

442 return node