ingests and deparses a given code block 'co' if `bytecode_version` is None, use the current Python interpreter version. Caller is responsible for closing `out` and `mapstream`
(
co,
bytecode_version: Tuple[int] = PYTHON_VERSION_TRIPLE,
out: Optional[TextIO] = sys.stdout,
showasm: Optional[str] = None,
showast={},
timestamp=None,
showgrammar=False,
source_encoding=None,
code_objects={},
source_size=None,
is_pypy: bool = False,
magic_int=None,
mapstream=None,
do_fragments=False,
compile_mode="exec",
start_offset: int = 0,
stop_offset: int = -1,
)
| 68 | |
| 69 | |
| 70 | def decompile( |
| 71 | co, |
| 72 | bytecode_version: Tuple[int] = PYTHON_VERSION_TRIPLE, |
| 73 | out: Optional[TextIO] = sys.stdout, |
| 74 | showasm: Optional[str] = None, |
| 75 | showast={}, |
| 76 | timestamp=None, |
| 77 | showgrammar=False, |
| 78 | source_encoding=None, |
| 79 | code_objects={}, |
| 80 | source_size=None, |
| 81 | is_pypy: bool = False, |
| 82 | magic_int=None, |
| 83 | mapstream=None, |
| 84 | do_fragments=False, |
| 85 | compile_mode="exec", |
| 86 | start_offset: int = 0, |
| 87 | stop_offset: int = -1, |
| 88 | ) -> Any: |
| 89 | """ |
| 90 | ingests and deparses a given code block 'co' |
| 91 | |
| 92 | if `bytecode_version` is None, use the current Python interpreter |
| 93 | version. |
| 94 | |
| 95 | Caller is responsible for closing `out` and `mapstream` |
| 96 | """ |
| 97 | if bytecode_version is None: |
| 98 | bytecode_version = PYTHON_VERSION_TRIPLE |
| 99 | |
| 100 | # store final output stream for case of error |
| 101 | real_out = out or sys.stdout |
| 102 | |
| 103 | def write(s): |
| 104 | s += "\n" |
| 105 | real_out.write(s) |
| 106 | |
| 107 | assert iscode(co), f"""{co} does not smell like code""" |
| 108 | |
| 109 | co_pypy_str = "PyPy " if is_pypy else "" |
| 110 | run_pypy_str = "PyPy " if IS_PYPY else "" |
| 111 | sys_version_lines = sys.version.split("\n") |
| 112 | if source_encoding: |
| 113 | write(f"# -*- coding: {source_encoding} -*-") |
| 114 | write( |
| 115 | "# uncompyle6 version %s\n" |
| 116 | "# %sPython bytecode version base %s%s\n# Decompiled from: %sPython %s" |
| 117 | % ( |
| 118 | __version__, |
| 119 | co_pypy_str, |
| 120 | version_tuple_to_str(bytecode_version), |
| 121 | " (%s)" % str(magic_int) if magic_int else "", |
| 122 | run_pypy_str, |
| 123 | "\n# ".join(sys_version_lines), |
| 124 | ) |
| 125 | ) |
| 126 | if co.co_filename: |
| 127 | write(f"# Embedded file name: {co.co_filename}") |