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

169 statements  

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 

20try: 

21 from ast import type_param 

22except ImportError: 

23 class type_param(AST): 

24 pass 

25 

26 

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

28 

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))) 

37 

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) 

43 

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})) 

52 

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)) 

57 

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,))), 

65 

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,))), 

159 

160 # expr 

161 

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,))), 

252 

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,))), 

260 

261 # slice 

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

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

264 (slice,))), 

265 

266 # boolop 

267 ('And', ((), (), (boolop,))), 

268 ('Or', ((), (), (boolop,))), 

269 

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,))), 

284 

285 # unaryop 

286 ('Invert', ((), (), (unaryop, AST,))), 

287 ('Not', ((), (), (unaryop, AST,))), 

288 ('UAdd', ((), (), (unaryop, AST,))), 

289 ('USub', ((), (), (unaryop, AST,))), 

290 

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,))), 

302 

303 # comprehension 

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

305 

306 # excepthandler 

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

308 ('lineno', 'col_offset', 

309 'end_lineno', 'end_col_offset'), 

310 (excepthandler,))), 

311 

312 # arguments 

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

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

315 

316 # keyword 

317 ('keyword', (('arg', 'value'), 

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

319 (AST,))), 

320 

321 # alias 

322 ('alias', (('name', 'asname'), 

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

324 (AST,))), 

325 

326 # withitem 

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

328 

329 # match_case 

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

331 

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,))), 

364 

365 # type_ignore 

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

367 

368 # type_param 

369 ('TypeVar', (('name', 'bound', 'default_value'), 

370 ('lineno', 'col_offset', 

371 'end_lineno', 'end_col_offset'), 

372 (type_param,))), 

373 ('ParamSpec', (('name', 'default_value'), 

374 ('lineno', 'col_offset', 

375 'end_lineno', 'end_col_offset'), 

376 (type_param,))), 

377 ('TypeVarTuple', (('name', 'default_value'), 

378 ('lineno', 'col_offset', 

379 'end_lineno', 'end_col_offset'), 

380 (type_param,))), 

381 ) 

382 

383for _name, _descr in _nodes: 

384 _make_node(_name, *_descr) 

385 

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): 

392 

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],),), 

400 

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,), ), 

429 

430 # expr 

431 

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), ), 

462 

463 # comprehension 

464 ('comprehension', (expr, expr, list[expr], int), ), 

465 

466 # excepthandler 

467 ('ExceptHandler', (expr | None, str | None, list[stmt]), ), 

468 

469 # arguments 

470 ('arguments', (list[expr], list[expr], expr | None, list[expr], list[expr], expr | None, list[expr]), ), 

471 

472 # keyword 

473 ('keyword', (str | None, expr), ), 

474 

475 # alias 

476 ('alias', (str, str | None), ), 

477 

478 # withitem 

479 ('withitem', (expr, expr | None), ), 

480 

481 # match_case 

482 ('match_case', (pattern, expr, list[stmt]), ), 

483 

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],), ), 

493 

494 # type_param 

495 ('TypeVar', (str, expr | None, expr | None), ), 

496 ('ParamSpec', (str, expr | None), ), 

497 ('TypeVarTuple', (str, expr | None), ), 

498 ) 

499 

500 for _name, _types in _node_types: 

501 _fill_field_types(_name, _types) 

502 

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 

507 

508 

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

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

511 

512 

513def unparse(gast_obj): 

514 from .unparser import unparse 

515 return unparse(gast_obj) 

516 

517 

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) 

522 

523 

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 

538 

539 

540# the following are directly imported from python3.15's Lib/ast.py # 

541 

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 

553 

554 

555def fix_missing_locations(node): 

556 """ 

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

558 col_offset attributes for every node that supports them. This is rather 

559 tedious to fill in for generated nodes, so this helper adds these attributes 

560 recursively where not already set, by setting them to the values of the 

561 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 getattr(node, 'end_lineno', None) is None: 

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 getattr(node, 'end_col_offset', None) is None: 

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 

588 

589 

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 

597 

598 

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 # TypeIgnore is a special case where lineno is not an attribute 

607 # but rather a field of the node itself. 

608 if isinstance(child, TypeIgnore): 

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

610 continue 

611 

612 if 'lineno' in child._attributes: 

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

614 if "end_lineno" in child._attributes: 

615 end_lineno = getattr(child, "end_lineno", 0) 

616 if end_lineno is not None: 

617 child.end_lineno = end_lineno + n 

618 return node 

619 

620 

621# Code import from Lib/ast.py 

622# 

623# minor changes: getattr(x, y, ...) is None => getattr(x, y, 42) is None 

624# 

625def dump( 

626 node, annotate_fields=True, include_attributes=False, 

627 # *, # removed for compatibility with python2 :-/ 

628 color=False, indent=None, show_empty=False, 

629): 

630 """ 

631 Return a formatted dump of the tree in node. This is mainly useful for 

632 debugging purposes. 

633 

634 If annotate_fields is true (by default), 

635 the returned string will show the names and the values for fields. 

636 If annotate_fields is false, the result string will be more compact by 

637 omitting unambiguous field names. 

638 

639 Attributes such as line numbers and column offsets are not dumped by 

640 default. If this is wanted, include_attributes can be set to true. 

641 

642 Color is available for compatibility with R.15+ but is not honored. 

643 

644 If indent is a non-negative integer or string, then the tree will be 

645 pretty-printed with that indent level. If indent is None (the default), the 

646 tree is dumped on a single line. 

647 

648 If show_empty is False, then empty lists and fields that are None 

649 will be omitted from the output for better readability. 

650 """ 

651 def _format(node, level=0): 

652 if indent is not None: 

653 level += 1 

654 prefix = '\n' + indent * level 

655 sep = ',\n' + indent * level 

656 else: 

657 prefix = '' 

658 sep = ', ' 

659 if isinstance(node, AST): 

660 cls = type(node) 

661 args = [] 

662 args_buffer = [] 

663 allsimple = True 

664 keywords = annotate_fields 

665 for name in node._fields: 

666 try: 

667 value = getattr(node, name) 

668 except AttributeError: 

669 keywords = True 

670 continue 

671 if value is None and getattr(cls, name, 42) is None: 

672 keywords = True 

673 continue 

674 if not show_empty: 

675 if value == []: 

676 if not keywords: 

677 args_buffer.append(repr(value)) 

678 continue 

679 if not keywords: 

680 args.extend(args_buffer) 

681 args_buffer = [] 

682 value, simple = _format(value, level) 

683 allsimple = allsimple and simple 

684 if keywords: 

685 args.append('%s=%s' % (name, value)) 

686 else: 

687 args.append(value) 

688 if include_attributes and node._attributes: 

689 for name in node._attributes: 

690 try: 

691 value = getattr(node, name) 

692 except AttributeError: 

693 continue 

694 if value is None and getattr(cls, name, 42) is None: 

695 continue 

696 value, simple = _format(value, level) 

697 allsimple = allsimple and simple 

698 args.append('%s=%s' % (name, value)) 

699 if allsimple and len(args) <= 3: 

700 return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args 

701 return '%s(%s%s)' % (node.__class__.__name__, prefix, sep.join(args)), False 

702 elif isinstance(node, list): 

703 if not node: 

704 return '[]', True 

705 return '[%s%s]' % (prefix, sep.join(_format(x, level)[0] for x in node)), False 

706 return repr(node), True 

707 

708 if not isinstance(node, AST): 

709 raise TypeError('expected AST, got %r' % node.__class__.__name__) 

710 if indent is not None and not isinstance(indent, str): 

711 indent = ' ' * indent 

712 return _format(node)[0]