### Install decompyle3 from Source Source: https://github.com/rocky/python-decompile3/blob/master/README.rst Install decompyle3 from its source code using setup.py for development or direct installation. ```bash pip install -e . ``` ```bash python setup.py install ``` -------------------------------- ### Check Package Installation and Functionality Source: https://github.com/rocky/python-decompile3/wiki/Releasing Install the package from a Git repository, verify its version, and test its core functionality. ```bash pushd /tmp/gittest pip install -e git+https://github.com/rocky/python-decompyle3.git#egg=decompyle3 decompyle3 --version decompyle3 src/decompyle3/main.py pip uninstall decompyle3 popd ``` -------------------------------- ### Install decompyle3 from PyPI Source: https://github.com/rocky/python-decompile3/blob/master/README.rst Install the decompyle3 package using pip. This is the recommended method for general use. ```bash pip install decompyle3 ``` -------------------------------- ### Install decompyle3 using Make Source: https://github.com/rocky/python-decompile3/blob/master/README.rst Use the provided GNU Makefile for installation. This may require root or sudo privileges. ```bash make install ``` -------------------------------- ### Test Release Candidate on GitHub Source: https://github.com/rocky/python-decompile3/wiki/Releasing Install a specific tagged version of the package from GitHub and test its functionality. ```bash pushd /tmp/gittest pip install -e git+https://github.com/rocky/python-decompyle3.git@$__version__#egg=decompyle3 decompyle3 --version decompyle3 src/decompyle3/__pycache__/__pkginfo__.cpython-38.pyc pip uninstall decompyle3 popd ``` -------------------------------- ### Pull Latest Sources Source: https://github.com/rocky/python-decompile3/wiki/Releasing Fetch the most recent changes from the Git repository before starting the release process. ```bash git pull ``` -------------------------------- ### Partial Decompilation by Bytecode Offset Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a specific range of bytecode offsets from a .pyc file, from a start offset to a stop offset. ```bash decompyle3 --start-offset 10 --stop-offset 50 my_module.pyc ``` -------------------------------- ### Equivalent Python Code Simplification Source: https://github.com/rocky/python-decompile3/blob/master/HOW-TO-REPORT-A-BUG.md Python may optimize code by simplifying conditional logic. This example shows how nested ifs can be combined into a single 'and' condition. ```python if a: if b: x = 1 ``` ```python if a and b: x = 1 ``` -------------------------------- ### Python Constant Folding Optimization Source: https://github.com/rocky/python-decompile3/blob/master/HOW-TO-REPORT-A-BUG.md Python can optimize code by evaluating constant conditions at compile time. This example demonstrates how an 'if True' statement is reduced to its body. ```python if True: x = 5 ``` ```python x = 5 ``` -------------------------------- ### Decompile Code Object to String Source: https://context7.com/rocky/python-decompile3/llms.txt Use `deparse_code2str` as a convenience wrapper to get decompiled Python source as a string. It returns "# deparse failed" if decompilation is unsuccessful. Supports specifying the compile mode. ```python import sys from decompyle3 import deparse_code2str def compute(items): return [x ** 2 for x in items if x > 0] source = deparse_code2str(compute.__code__, version=sys.version_info[:2]) print(source) # Output: # def compute(items): # return [x ** 2 for x in items if x > 0] # Use a specific compile mode (e.g. lambda, eval) lambda_co = (lambda x: x + 1).__code__ source = deparse_code2str(lambda_co, version=sys.version_info[:2], compile_mode="lambda") print(source) # lambda x: x + 1 ``` -------------------------------- ### Build and Check Packages Source: https://github.com/rocky/python-decompile3/wiki/Releasing Create distribution packages for different Python versions and check their integrity using twine. ```bash ./admin-tools/make-dist-3.7-3.10.sh git tag release-python-3.7-$__version__ pyenv local 3.9 twine check dist/decompyle3-$__version__* ./admin-tools/make-dist-newest.sh twine check dist/decompyle3-$__version__* ``` -------------------------------- ### Display decompyle3 Usage Help Source: https://github.com/rocky/python-decompile3/blob/master/README.rst Show the help message for the decompyle3 command-line tool to understand available options. ```bash decompyle3 -h ``` -------------------------------- ### Show Version Source: https://context7.com/rocky/python-decompile3/llms.txt Displays the version of the decompyle3 command-line tool. ```bash decompyle3 --version ``` -------------------------------- ### Upload to PyPI Source: https://github.com/rocky/python-decompile3/wiki/Releasing Upload the built distribution files to the Python Package Index. ```bash twine upload dist/decompyle3-${__version__}*{whl,gz} ``` -------------------------------- ### Run Tests with Make Source: https://github.com/rocky/python-decompile3/blob/master/README.rst Execute the test suite for decompyle3 using the provided Makefile. This command runs tests from fastest to slowest. ```bash make check ``` -------------------------------- ### Update Version and Commit Source: https://github.com/rocky/python-decompile3/wiki/Releasing Modify the version file, source the version, and commit the changes with a release-ready message. ```bash emacs decompyle3/version.py source decompyle3/version.py echo $__version__ git commit -m"Get ready for release $__version__" . ``` -------------------------------- ### Show Tokenize Version Source: https://context7.com/rocky/python-decompile3/llms.txt Displays the version of the decompyle3-tokenize command-line tool. ```bash decompyle3-tokenize --version ``` -------------------------------- ### Show Tokenized Assembly Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and displays the tokenized assembly representation of the bytecode. ```bash decompyle3 --asm my_module.pyc ``` -------------------------------- ### Generate and Check ChangeLog Source: https://github.com/rocky/python-decompile3/wiki/Releasing Automatically generate the ChangeLog file and then spell-check its content. ```bash make ChangeLog codespell ChangeLog ``` -------------------------------- ### Update NEWS.md and Push Source: https://github.com/rocky/python-decompile3/wiki/Releasing Update the NEWS.md file from the ChangeLog, amend the previous commit, and push to enable CI testing. ```bash emacs NEWS.md make check git commit --amend . git push origin HEAD # get CI testing going early ``` -------------------------------- ### Show Tokenized Disassembly Using Source Path Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-tokenize` to display tokenized disassembly by providing the path to the .py source file, which will find the corresponding .pyc. ```bash decompyle3-tokenize my_module.py ``` -------------------------------- ### Show Xdis and Tokenized Assembly Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and displays both the xdis assembly and the tokenized assembly. ```bash decompyle3 --asm++ my_module.pyc ``` -------------------------------- ### Move Uploaded Distribution Files Source: https://github.com/rocky/python-decompile3/wiki/Releasing Organize the distribution files by moving them to an 'uploaded' directory after a successful upload. ```bash mv -v dist/decompyle3-${__version__}* dist/uploaded ``` -------------------------------- ### Pull Tags Source: https://github.com/rocky/python-decompile3/wiki/Releasing Ensure all tags, including the newly created release tag, are present locally. ```bash git pull --tags ``` -------------------------------- ### Bump Version to Development Source: https://github.com/rocky/python-decompile3/wiki/Releasing Update the version number in the version file to the next development release (e.g., appending '.dev0'). ```bash In `decompyle3/version.py` bump number and add `.dev0`. ``` -------------------------------- ### Decompile as Full Exec Module Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-code` to decompile the entire .pyc file as a full execution module. ```bash decompyle3-code --format exec my_module.pyc ``` -------------------------------- ### Check Newest Versions Script Source: https://github.com/rocky/python-decompile3/wiki/Releasing Execute a script to verify compatibility with newer Python versions. ```bash admin-tools/check-newest-versions.sh ``` -------------------------------- ### Show Parse Tree Before and After Transformation Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and displays the parse tree before and after transformations. ```bash decompyle3 --tree++ my_module.pyc ``` -------------------------------- ### Show Line-Number Correspondences Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and displays the line-number correspondences between the bytecode and the recovered source code. ```bash decompyle3 --linemaps my_module.pyc ``` -------------------------------- ### Run decompyle3 Decompiler Source: https://github.com/rocky/python-decompile3/blob/master/README.rst Execute the decompyle3 command-line tool to decompile Python bytecode files (.pyc or .pyo). ```bash decompyle3 *compiled-python-file-pyc-or-pyo* ``` -------------------------------- ### Decompile Lambda with Assembly and Redirected Output Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles lambda functions from a .pyc file, shows assembly output, and redirects the output to a specified directory. ```bash decompyle3-code --format lambda -a --output /tmp/out/ my_module.pyc ``` -------------------------------- ### Decompile with Syntax Verification Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and performs syntax verification on the recovered code. ```bash decompyle3 --verify syntax my_module.pyc ``` -------------------------------- ### decompyle3-tokenize CLI Source: https://context7.com/rocky/python-decompile3/llms.txt Tokenized disassembly using the decompyle3-tokenize command. ```APIDOC ## `decompyle3-tokenize` — Tokenized disassembly ```bash # Show tokenized disassembly of a .pyc file decompyle3-tokenize my_module.pyc # Also accepts a .py source path (finds the corresponding .pyc) decompyle3-tokenize my_module.py # Disassemble multiple files decompyle3-tokenize foo.pyc bar.pyc # Show version decompyle3-tokenize --version ``` ``` -------------------------------- ### Disassemble Multiple Files Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-tokenize` to display tokenized disassembly for multiple .pyc or .py files. ```bash decompyle3-tokenize foo.pyc bar.pyc ``` -------------------------------- ### Decompile with Execution Verification Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and verifies the recovered code by executing it. ```bash decompyle3 --verify run my_module.pyc ``` -------------------------------- ### Show Grammar Reductions During Parsing Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and displays the grammar reductions that occur during the parsing process. ```bash decompyle3 --grammar my_module.pyc ``` -------------------------------- ### Enable Syntax Verification in decompyle3 Source: https://github.com/rocky/python-decompile3/blob/master/README.rst Use the --syntax-verify option to ensure the decompiled Python code is syntactically correct for the target interpreter. ```bash decompyle3 --syntax-verify *compiled-python-file-pyc-or-pyo* ``` -------------------------------- ### Decompile .pyc File to Source Source: https://context7.com/rocky/python-decompile3/llms.txt Use `decompile_file` to load a compiled Python bytecode file and decompile all code objects within it. Output can be directed to stdout or a file. ```python import sys from decompyle3 import decompile_file # Decompile to stdout deparsed_list = decompile_file("my_module.pyc") # Decompile to a file with open("my_module_recovered.py", "w") as out: deparsed_list = decompile_file("my_module.pyc", outstream=out) # Inspect the deparsed result for deparsed in deparsed_list: print("Bytecode version:", deparsed.version) print("Recovered text length:", len(deparsed.text)) ``` -------------------------------- ### Show Tokenized Disassembly of a .pyc File Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-tokenize` to display the tokenized disassembly of a .pyc file. ```bash decompyle3-tokenize my_module.pyc ``` -------------------------------- ### Map Bytecode Line Numbers to Source Line Numbers Source: https://context7.com/rocky/python-decompile3/llms.txt Computes a mapping between bytecode line numbers in a .pyc file and the corresponding source line numbers in the original .py file. Returns a sorted list of [bytecode_line, source_line] pairs or an error string. ```python from decompyle3.linenumbers import line_number_mapping mapping = line_number_mapping("my_module.pyc", "my_module.py") if isinstance(mapping, str): print("Error:", mapping) else: for bytecode_line, source_line in mapping: print(f" bytecode line {bytecode_line} -> source line {source_line}") ``` -------------------------------- ### decompyle3 CLI Source: https://context7.com/rocky/python-decompile3/llms.txt The main decompyle3 command-line interface for decompiling .pyc files. ```APIDOC ## `decompyle3` — Main decompiler command ```bash # Decompile a single .pyc file to stdout decompyle3 my_module.pyc # Decompile to a specific output file decompyle3 --output recovered.py my_module.pyc # Decompile an entire directory recursively, writing .py files next to each .pyc decompyle3 --recurse --output /tmp/recovered/ /path/to/__pycache__/ # Decompile with syntax verification decompyle3 --verify syntax my_module.pyc # Decompile with execution verification (runs the recovered script) decompyle3 --verify run my_module.pyc # Show tokenized assembly after parsing decompyle3 --asm my_module.pyc # Show both xdis assembly and tokenized assembly decompyle3 --asm++ my_module.pyc # Show the parse tree before and after transformation decompyle3 --tree++ my_module.pyc # Show grammar reductions during parsing decompyle3 --grammar my_module.pyc # Show line-number correspondences between bytecode and output decompyle3 --linemaps my_module.pyc # Partial decompilation: decompile bytecode offsets 10 to 50 only decompyle3 --start-offset 10 --stop-offset 50 my_module.pyc # Show version decompyle3 --version ``` ``` -------------------------------- ### Merge and Check for Python 3.7-3.10 Source: https://github.com/rocky/python-decompile3/wiki/Releasing Run scripts to merge changes for specific Python versions and check compatibility across Python 3.7 to 3.10. ```bash ./admin-tools/merge-for-3.7.sh git commit . make check ./admin-tools/check-3.7-3.10-versions.sh git push origin HEAD ``` -------------------------------- ### Disassemble Python Bytecode File Source: https://context7.com/rocky/python-decompile3/llms.txt Disassembles a .pyc file using decompyle3's tokenization pipeline, augmenting standard disassembly with pseudo-instructions and expanded operands. Output can be directed to a stream. ```python import sys from decompyle3.disas import disassemble_file # Tokenized disassembly to stdout disassemble_file("my_module.pyc") # Capture tokenized disassembly import io buf = io.StringIO() disassemble_file("my_module.pyc", outstream=buf) print(buf.getvalue()[:500]) ``` -------------------------------- ### Decompile to a Specific Output File Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a .pyc file and writes the recovered source code to a specified output file. ```bash decompyle3 --output recovered.py my_module.pyc ``` -------------------------------- ### Decompile a Single .pyc File to Stdout Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a single .pyc file and outputs the recovered Python source code to standard output. ```bash decompyle3 my_module.pyc ``` -------------------------------- ### decompyle3-code CLI Source: https://context7.com/rocky/python-decompile3/llms.txt Decompile by code-object type using the decompyle3-code command. ```APIDOC ## `decompyle3-code` — Decompile by code-object type ```bash # Decompile only lambda functions decompyle3-code --format lambda my_module.pyc # Decompile only list comprehensions decompyle3-code --format list-comprehension my_module.pyc # Decompile only dict comprehensions decompyle3-code --format dict-comprehension my_module.pyc # Decompile only generator expressions decompyle3-code --format generator my_module.pyc # Decompile only set comprehensions decompyle3-code --format set-comprehension my_module.pyc # Decompile all fragments (all comprehension/generator/lambda types) decompyle3-code --format code-fragments my_module.pyc # Decompile as a full exec module decompyle3-code --format exec my_module.pyc # With assembly output and output redirected decompyle3-code --format lambda -a --output /tmp/out/ my_module.pyc ``` ``` -------------------------------- ### Decompile Lambda Functions from Bytecode Source: https://context7.com/rocky/python-decompile3/llms.txt Scans a bytecode file for all `` code objects and decompiles only those fragments. Useful for auditing or recovering embedded lambda logic. Can capture output to a buffer. ```python import sys from decompyle3.code_fns import decompile_lambda_fns # Decompile all lambdas in a .pyc file to stdout decompile_lambda_fns("my_module.pyc", sys.stdout) # Capture output import io buf = io.StringIO() succeeded = decompile_lambda_fns("my_module.pyc", buf, start_offset=0, stop_offset=-1) if succeeded: print("Recovered lambdas:") print(buf.getvalue()) ``` -------------------------------- ### Decompile Entire Directory Recursively Source: https://context7.com/rocky/python-decompile3/llms.txt Recursively decompiles all .pyc files in a directory and its subdirectories, writing the recovered .py files alongside the original .pyc files. ```bash decompyle3 --recurse --output /tmp/recovered/ /path/to/__pycache__/ ``` -------------------------------- ### Decompile Code with Debug Options Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a code object with detailed debug options to show AST before and after parsing, and grammar reductions. Requires a code object and debug options dictionary. ```python debug_opts = { "asm": "after", # show tokenized assembly after parsing "tree": {"before": True, "after": False}, "grammar": {"reduce": True, "rules": False, "transition": False, "errorstack": "full", "context": True, "dups": False}, } deparsed = code_deparse(co, out=sys.stdout, version=sys.version_info[:3], debug_opts=debug_opts) ``` -------------------------------- ### Decompile All Code Fragments Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-code` to decompile all code fragments, including lambdas, comprehensions, and generators. ```bash decompyle3-code --format code-fragments my_module.pyc ``` -------------------------------- ### Decompile Lambda Functions Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-code` to decompile only lambda functions found in a .pyc file. ```bash decompyle3-code --format lambda my_module.pyc ``` -------------------------------- ### line_number_mapping Source: https://context7.com/rocky/python-decompile3/llms.txt Computes a mapping between the line numbers in a compiled .pyc file and the corresponding line numbers in the original .py source. Returns a sorted list of [bytecode_line, source_line] pairs. ```APIDOC ## `line_number_mapping` — Map bytecode line numbers to source line numbers Computes a mapping between the line numbers in a compiled `.pyc` file and the corresponding line numbers in the original `.py` source. Returns a sorted list of `[bytecode_line, source_line]` pairs. ```python from decompyle3.linenumbers import line_number_mapping mapping = line_number_mapping("my_module.pyc", "my_module.py") if isinstance(mapping, str): print("Error:", mapping) else: for bytecode_line, source_line in mapping: print(f" bytecode line {bytecode_line} -> source line {source_line}") # Output: # bytecode line 1 -> source line 1 # bytecode line 5 -> source line 3 # bytecode line 12 -> source line 8 ``` ``` -------------------------------- ### Decompile All Comprehension and Generator Fragments Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles every sub-code-object of type ``, ``, ``, ``, and `` found within a bytecode file. Returns `True` on success, `False` on failure. ```python import sys from decompyle3.code_fns import decompile_all_fragments succeeded = decompile_all_fragments("my_module.pyc", sys.stdout) print("Success:", succeeded) ``` -------------------------------- ### Decompile Set Comprehensions Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-code` to decompile only set comprehensions found in a .pyc file. ```bash decompyle3-code --format set-comprehension my_module.pyc ``` -------------------------------- ### decompile Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a raw Python code object already loaded in memory. Supports specifying output streams and offset ranges for partial decompilation. ```APIDOC ## decompile ### Description Lower-level function that decompiles a single Python code object (`types.CodeType`) already loaded in memory. Useful when you have bytecode objects from live introspection or after loading a module manually with `xdis.load_module`. ### Method `decompile(co: types.CodeType, bytecode_version: Tuple[int, int], out: IO[str] = sys.stdout, start_offset: Optional[int] = None, stop_offset: Optional[int] = None, mapstream: Optional[IO[str]] = None)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import sys import inspect from decompyle3.main import decompile from xdis import sysinfo2float def my_function(x, y): """Example function to decompile.""" return x * 2 + y # Decompile the live code object of my_function co = my_function.__code__ version = sys.version_info[:2] # e.g. (3, 8) decompile(co, bytecode_version=version, out=sys.stdout) # Decompile with offset range (partial decompilation) decompile(co, bytecode_version=version, out=sys.stdout, start_offset=0, stop_offset=10) # Decompile and produce a line-number map stream import io map_out = io.StringIO() decompile(co, bytecode_version=version, out=sys.stdout, mapstream=map_out) print("Line map:", map_out.getvalue()) ``` ### Response #### Success Response (200) This function writes directly to the specified `out` stream and optionally `mapstream`. It does not return a value other than potentially `None` on failure. #### Response Example ``` # Output to sys.stdout: # # decompyle3 version 3.9.4.dev0 # # Python bytecode version base 3.8 # # ... # def my_function(x, y): # """Example function to decompile.""" # return x * 2 + y ``` ``` -------------------------------- ### decompile_file Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a given .pyc file into Python source code. It can output to stdout or a specified file stream. ```APIDOC ## decompile_file ### Description Loads a compiled Python bytecode file, detects its version and implementation (CPython / PyPy), and decompiles every code object inside it. Returns a list of `SourceWalker` deparsed objects. The output stream defaults to `sys.stdout`; pass an open file object to redirect. ### Method `decompile_file(filename: str, outstream: Optional[IO[str]] = None) -> List[SourceWalker]` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import sys from decompyle3 import decompile_file # Decompile to stdout deparsed_list = decompile_file("my_module.pyc") # Decompile to a file with open("my_module_recovered.py", "w") as out: deparsed_list = decompile_file("my_module.pyc", outstream=out) # Inspect the deparsed result for deparsed in deparsed_list: print("Bytecode version:", deparsed.version) print("Recovered text length:", len(deparsed.text)) ``` ### Response #### Success Response (200) - **deparsed_list** (List[SourceWalker]) - A list of deparsed objects, each containing decompiled source and metadata. #### Response Example ``` # Example deparsed object attributes: # deparsed.version: str # deparsed.text: str ``` ``` -------------------------------- ### Decompile Raw Code Object Source: https://context7.com/rocky/python-decompile3/llms.txt The `decompile` function decompiles a single Python code object already in memory. It supports specifying the bytecode version, an offset range for partial decompilation, and generating a line-number map. ```python import sys import inspect from decompyle3.main import decompile from xdis import sysinfo2float def my_function(x, y): """Example function to decompile.""" return x * 2 + y # Decompile the live code object of my_function co = my_function.__code__ version = sys.version_info[:2] # e.g. (3, 8) decompile(co, bytecode_version=version, out=sys.stdout) # Output: # # decompyle3 version 3.9.4.dev0 # # Python bytecode version base 3.8 # # ... # def my_function(x, y): # """Example function to decompile.""" # return x * 2 + y # Decompile with offset range (partial decompilation) decompile(co, bytecode_version=version, out=sys.stdout, start_offset=0, stop_offset=10) # Decompile and produce a line-number map stream import io map_out = io.StringIO() decompile(co, bytecode_version=version, out=sys.stdout, mapstream=map_out) print("Line map:", map_out.getvalue()) ``` -------------------------------- ### Decompile Code with Line Number Map Source: https://context7.com/rocky/python-decompile3/llms.txt Decompiles a code object and records a mapping from recovered source lines to original bytecode line numbers. Useful for debuggers. The returned walker has a `.source_linemap` attribute. ```python import sys from decompyle3.semantics.linemap import code_deparse_with_map def process(data): result = [] for item in data: if item > 0: result.append(item * 2) return result deparsed = code_deparse_with_map(process.__code__, version=sys.version_info[:3]) print(deparsed.text) print("\nLine map (recovered_line -> original_line):") for recovered_line, orig_line in sorted(deparsed.source_linemap.items()): print(f" recovered {recovered_line} <- original {orig_line}") ``` -------------------------------- ### Decompile Specific Fragment Types Source: https://context7.com/rocky/python-decompile3/llms.txt Specialized functions to decompile specific code-object types like list comprehensions, generator expressions, dict comprehensions, and set comprehensions. Each accepts filename, output stream, and optional offsets. ```python from decompyle3.code_fns import ( decompile_list_comprehensions, decompile_dict_comprehensions, decompile_generators, decompile_set_comprehensions, ) import sys # Only list comprehensions decompile_list_comprehensions("my_module.pyc", sys.stdout) # Only generator expressions decompile_generators("my_module.pyc", sys.stdout) # Only dict comprehensions decompile_dict_comprehensions("my_module.pyc", sys.stdout) # Only set comprehensions decompile_set_comprehensions("my_module.pyc", None, sys.stdout) ``` -------------------------------- ### Decompile List Comprehensions Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-code` to decompile only list comprehensions found in a .pyc file. ```bash decompyle3-code --format list-comprehension my_module.pyc ``` -------------------------------- ### Decompile Dict Comprehensions Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-code` to decompile only dictionary comprehensions found in a .pyc file. ```bash decompyle3-code --format dict-comprehension my_module.pyc ``` -------------------------------- ### Core Decompiler Function Source: https://context7.com/rocky/python-decompile3/llms.txt The `code_deparse` function is the core decompiler. It takes a code object and produces source output, returning a `SourceWalker` instance containing the decompiled text and version information, or `None` on failure. ```python import sys from decompyle3.semantics.pysource import code_deparse def greet(name): msg = f"Hello, {name}!" print(msg) return msg co = greet.__code__ deparsed = code_deparse(co, out=sys.stdout, version=sys.version_info[:3]) if deparsed is not None: print("\n--- Recovered source ---") print(deparsed.text) # def greet(name): # msg = f'Hello, {name}!' # print(msg) # return msg ``` -------------------------------- ### Decompile Generator Expressions Source: https://context7.com/rocky/python-decompile3/llms.txt Uses `decompyle3-code` to decompile only generator expressions found in a .pyc file. ```bash decompyle3-code --format generator my_module.pyc ``` -------------------------------- ### deparse_code2str Source: https://context7.com/rocky/python-decompile3/llms.txt A convenience function that wraps `code_deparse` to return decompiled Python source as a string. ```APIDOC ## deparse_code2str ### Description Convenience wrapper around `code_deparse` that returns the decompiled Python source as a plain `str` instead of writing to a stream. Returns `"# deparse failed"` if decompilation fails. ### Method `deparse_code2str(co: types.CodeType, version: Tuple[int, int], compile_mode: str = "exec") -> str` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import sys from decompyle3 import deparse_code2str def compute(items): return [x ** 2 for x in items if x > 0] source = deparse_code2str(compute.__code__, version=sys.version_info[:2]) print(source) # Use a specific compile mode (e.g. lambda, eval) lambda_co = (lambda x: x + 1).__code__ source = deparse_code2str(lambda_co, version=sys.version_info[:2], compile_mode="lambda") print(source) ``` ### Response #### Success Response (200) - **source** (str) - The decompiled Python source code as a string, or `"# deparse failed"` if an error occurs. #### Response Example ``` # Example output for compute function: def compute(items): return [x ** 2 for x in items if x > 0] # Example output for lambda function: lambda x: x + 1 ``` ``` -------------------------------- ### code_deparse Source: https://context7.com/rocky/python-decompile3/llms.txt The core decompiler function that processes a code object, builds a parse tree, and generates source output. ```APIDOC ## code_deparse ### Description The core decompiler function. Ingests a code object, scans it with the appropriate version-specific scanner, builds a parse tree, and traverses it with a `SourceWalker` to produce source output. Returns the `SourceWalker` instance (which carries `.text`, `.version`, and other attributes), or `None` on failure. ### Method `code_deparse(co: types.CodeType, out: IO[str], version: Tuple[int, int, int]) -> Optional[SourceWalker]` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import sys from decompyle3.semantics.pysource import code_deparse def greet(name): msg = f"Hello, {name}!" print(msg) return msg co = greet.__code__ deparsed = code_deparse(co, out=sys.stdout, version=sys.version_info[:3]) if deparsed is not None: print("\n--- Recovered source ---") print(deparsed.text) ``` ### Response #### Success Response (200) - **deparsed** (Optional[SourceWalker]) - The `SourceWalker` instance containing the decompiled source and metadata, or `None` if decompilation fails. #### Response Example ``` # Example output: # def greet(name): # msg = f'Hello, {name}!' # print(msg) # return msg ``` ``` -------------------------------- ### Python Unreachable Code Elimination Source: https://github.com/rocky/python-decompile3/blob/master/HOW-TO-REPORT-A-BUG.md Python compilers can remove entire code blocks that are determined to be unreachable, such as the body of an 'if False' statement. ```python if False: x = 1 y = 2 # ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.