ingests and deparses a given code block 'co'. If version is None, we will use the current Python interpreter version.
(
co,
out=sys.stdout,
version: Optional[tuple] = None,
debug_opts=DEFAULT_DEBUG_OPTS,
code_objects={},
compile_mode="exec",
is_pypy=IS_PYPY,
walker=SourceWalker,
start_offset: int = 0,
stop_offset: int = -1,
)
| 1300 | |
| 1301 | |
| 1302 | def code_deparse( |
| 1303 | co, |
| 1304 | out=sys.stdout, |
| 1305 | version: Optional[tuple] = None, |
| 1306 | debug_opts=DEFAULT_DEBUG_OPTS, |
| 1307 | code_objects={}, |
| 1308 | compile_mode="exec", |
| 1309 | is_pypy=IS_PYPY, |
| 1310 | walker=SourceWalker, |
| 1311 | start_offset: int = 0, |
| 1312 | stop_offset: int = -1, |
| 1313 | ) -> Optional[SourceWalker]: |
| 1314 | """ |
| 1315 | ingests and deparses a given code block 'co'. If version is None, |
| 1316 | we will use the current Python interpreter version. |
| 1317 | """ |
| 1318 | |
| 1319 | assert iscode(co) |
| 1320 | |
| 1321 | if out is None: |
| 1322 | out = sys.stdout |
| 1323 | |
| 1324 | if version is None: |
| 1325 | version = PYTHON_VERSION_TRIPLE |
| 1326 | |
| 1327 | # store final output stream for case of error |
| 1328 | scanner = get_scanner(version, is_pypy=is_pypy, show_asm=debug_opts["asm"]) |
| 1329 | |
| 1330 | tokens, customize = scanner.ingest( |
| 1331 | co, code_objects=code_objects, show_asm=debug_opts["asm"] |
| 1332 | ) |
| 1333 | |
| 1334 | if start_offset > 0: |
| 1335 | for i, t in enumerate(tokens): |
| 1336 | # If t.offset is a string, we want to skip this. |
| 1337 | if isinstance(t.offset, int) and t.offset >= start_offset: |
| 1338 | tokens = tokens[i:] |
| 1339 | break |
| 1340 | |
| 1341 | if stop_offset > -1: |
| 1342 | for i, t in enumerate(tokens): |
| 1343 | # In contrast to the test for start_offset If t.offset is |
| 1344 | # a string, we want to extract the integer offset value. |
| 1345 | if t.off2int() >= stop_offset: |
| 1346 | tokens = tokens[:i] |
| 1347 | break |
| 1348 | |
| 1349 | debug_parser = debug_opts.get("grammar", dict(PARSER_DEFAULT_DEBUG)) |
| 1350 | |
| 1351 | # Build Syntax Tree from disassembly. |
| 1352 | linestarts = dict(scanner.opc.findlinestarts(co)) |
| 1353 | deparsed = walker( |
| 1354 | version, |
| 1355 | out, |
| 1356 | scanner, |
| 1357 | showast=debug_opts.get("tree", TREE_DEFAULT_DEBUG), |
| 1358 | debug_parser=debug_parser, |
| 1359 | compile_mode=compile_mode, |