Convert the code object co into a python source fragment. :param version: The python version this code is from as a float, for example 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc. :param co: The code object to parse. :param out: Fil
(
co,
out=StringIO(),
version=None,
is_pypy=IS_PYPY,
debug_opts=DEFAULT_DEBUG_OPTS,
code_objects={},
compile_mode="exec",
walker=FragmentsWalker,
start_offset: int = 0,
stop_offset: int = -1,
)
| 2047 | |
| 2048 | |
| 2049 | def code_deparse( |
| 2050 | co, |
| 2051 | out=StringIO(), |
| 2052 | version=None, |
| 2053 | is_pypy=IS_PYPY, |
| 2054 | debug_opts=DEFAULT_DEBUG_OPTS, |
| 2055 | code_objects={}, |
| 2056 | compile_mode="exec", |
| 2057 | walker=FragmentsWalker, |
| 2058 | start_offset: int = 0, |
| 2059 | stop_offset: int = -1, |
| 2060 | ): |
| 2061 | """ |
| 2062 | Convert the code object co into a python source fragment. |
| 2063 | |
| 2064 | :param version: The python version this code is from as a float, for |
| 2065 | example 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc. |
| 2066 | :param co: The code object to parse. |
| 2067 | :param out: File like object to write the output to. |
| 2068 | :param debug_opts: A dictionary with keys |
| 2069 | 'asm': value determines whether to show |
| 2070 | mangled bytecode disassembly |
| 2071 | 'ast': value determines whether to show |
| 2072 | 'grammar': boolean determining whether to show |
| 2073 | grammar reduction rules. |
| 2074 | If value is a file-like object, output that object's write method will |
| 2075 | be used rather than sys.stdout |
| 2076 | |
| 2077 | :return: The deparsed source fragment. |
| 2078 | """ |
| 2079 | |
| 2080 | assert iscode(co) |
| 2081 | |
| 2082 | if version is None: |
| 2083 | version = PYTHON_VERSION_TRIPLE |
| 2084 | if is_pypy is None: |
| 2085 | is_pypy = IS_PYPY |
| 2086 | |
| 2087 | # store final output stream for case of error |
| 2088 | scanner = get_scanner(version, is_pypy=is_pypy, show_asm=debug_opts["asm"]) |
| 2089 | |
| 2090 | show_asm = debug_opts.get("asm", None) |
| 2091 | tokens, customize = scanner.ingest(co, code_objects=code_objects, show_asm=show_asm) |
| 2092 | |
| 2093 | tokens, customize = scanner.ingest(co) |
| 2094 | |
| 2095 | if start_offset > 0: |
| 2096 | for i, t in enumerate(tokens): |
| 2097 | # If t.offset is a string, we want to skip this. |
| 2098 | if isinstance(t.offset, int) and t.offset >= start_offset: |
| 2099 | tokens = tokens[i:] |
| 2100 | break |
| 2101 | |
| 2102 | if stop_offset > -1: |
| 2103 | for i, t in enumerate(tokens): |
| 2104 | # In contrast to the test for start_offset If t.offset is |
| 2105 | # a string, we want to extract the integer offset value. |
| 2106 | if t.off2int() >= stop_offset: |
no test coverage detected