Package: pygetsource
pygetsourcepygetsourceis a decompiler for Python 3, aiming to convert compiled bytecode instructions back into Python code.OverviewWhen Python reads code, it first converts the instructions into bytecode. For instance:a=2is converted intoLOAD_CONST 1
STORE_FAST 0The latter form is typically stored in.pycfiles and in the__code__attribute of function objects. The goal ofpygetsourceis to reverse this process.The project takes its name from theinspect.getsourcefunction, which returns the source code of a function, but it is not always applicable, as explained above.pygetsourceis still in development. It should be able to recover the source code of simple functions for various programs from Python 3.7 to Python 3.11. It is not yet able to recover the source code of classes, import statements, try/except/match/with blocks, and does not support Python 2. While functional, the codebase has not been optimized and is in need of significant refactoring.Finally, this software is distributed under the permissive MIT license.InstallationInstall the package using pip:pipinstallpygetsourceUsageimportpygetsourcedeffunc():a=5whilei<10:ifa==2:breakelifa==4:return3a=i//5d=3e=4returne+dprint(pygetsource.getsource(func.__code__))produces the following output:a=5whilei<10:ifa==2:breakelifa==4:return3else:a=i//5d=3e=4returne+dNotice how theelsestatement was added to theelifstatement, yet the two programs are functionally equivalent.When is this useful ?pygetsourceproves useful when you need to recover the source code from a.pycfile, or when you want to get the source code of a function created through an eval statement or a lambda syntax. Indeed, runninginspect.getsourcefail in the latter case since the origin file of the function is either not available, or Python does not provide the exact boundaries, which are required in the case of lambda functions.Alternativesuncompyle6is a Python decompiler that supports Python 2 and 3 up to Python 3.8. It uses a grammar-based approach to rebuild code from bytecode patterns. This approach is less effective for higher versions that introduce various bytecode optimizations, especially regarding complex control structures like loops, or the example given above. At the moment, it supports a larger range of Python syntaxes (such as with blocks or try/excepts).
It is also licensed under a copyleft GPL license, making it less suitable for larger projects with permissive licenses.decompyle++ (pycdc)uses a state machine approach to build an AST iteratively by processing bytecode instructions. It's written in C++ and supports more Python versions than uncompyle6, but has more trouble decompiling  complex control structures like nested loops, break patterns, comprehensions, or the example given above. It also uses the copyleft GPL license.How does it work ?pygetsourceuses a distinct approach. The bytecode instructions are initially converted into a directed graph, representing the program's flow. This graph is then iteratively reduced, processing each node based on its opcode, argument, and position and generating theASTas it goes.
This method allows us to rely more on high-level patterns and less on Python’s idiosyncrasies when recreating complex structures like nested loops or break/return statements, and handle Python versions from 3.7 to 3.11 with the same codebase.In constrast withuncompyle6andpycdc,pygetsourceuses theastandastunparselibraries to generate the source code from the generated AST.Here is an example of a graph being reduced:When is a decompilation successful ?Since the compilation process is injective, it's impossible to recover the exact original source code. Multiple Python programs can yield the same bytecode instructions. Also, the original source code is typically unavailable for comparison (why would you use this software otherwise ?).If we recompile the generated program, we can compare the two sets of bytecode instructions to ensure functional equivalence. However, Python may introduce no-op codes (like 'NOP') that might cause this verification to fail despite the two code objects being functionally equivalent.Instead,pygetsourcecompares the graph of the original code object with the graph of the recovered code object, after a pruning step. During this step, no-op codes are removed, jump instructions are pruned (while maintaining edges between source and target nodes), and dead-code is eliminated.ContributingContributions are welcome. Feel free to open an issue or submit a pull request. Any issues related to the decompilation process should include version of the Python interpreter used to generate the bytecode, the source code of the function, and the bytecode instructions as printed bydis.dis(code).To install the project in development mode, clone the repository and runpip install -e '.[dev]'in the root directory. You can then run the tests usingpytest. Make sure to havegraphvizinstalled first. If you're on MacOS and have trouble with the Pythonpygraphvizpackage, try installing it using the following command:pipinstall\--global-option=build_ext\--global-option="-I$(brew--prefixgraphviz)/include/"\--global-option="-L$(brew--prefixgraphviz)/lib/"\pygraphvizTo inspect a failure case, use thedebug=Trueparameter of thegetsourcefunction. This will display the graph at different stages of the reduction process, as well as various debug information.