Skip to main content

Python cross-version byte-code decompiler

Project description

buildstatus Latest Version Supported Python Versions

uncompyle6

A native Python cross-version decompiler and fragment decompiler. The successor to decompyle, uncompyle, and uncompyle2.

Introduction

uncompyle6 translates Python bytecode back into equivalent Python source code. It accepts bytecodes from Python version 1.3 to version 3.8, spanning over 24 years of Python releases. We include Dropbox’s Python 2.5 bytecode and some PyPy bytecode.

Why this?

Ok, I’ll say it: this software is amazing. It is more than your normal hacky decompiler. Using compiler technology, the program creates a parse tree of the program from the instructions; nodes at the upper levels that look a little like what might come from a Python AST. So we can really classify and understand what’s going on in sections of Python bytecode.

Building on this, another thing that makes this different from other CPython bytecode decompilers is the ability to deparse just fragments of source code and give source-code information around a given bytecode offset.

I use the tree fragments to deparse fragments of code at run time inside my trepan debuggers. For that, bytecode offsets are recorded and associated with fragments of the source code. This purpose, although compatible with the original intention, is yet a little bit different. See this for more information.

Python fragment deparsing given an instruction offset is useful in showing stack traces and can be encorporated into any program that wants to show a location in more detail than just a line number at runtime. This code can be also used when source-code information does not exist and there is just bytecode. Again, my debuggers make use of this.

There were (and still are) a number of decompyle, uncompyle, uncompyle2, uncompyle3 forks around. Almost all of them come basically from the same code base, and (almost?) all of them are no longer actively maintained. One was really good at decompiling Python 1.5-2.3 or so, another really good at Python 2.7, but that only. Another handles Python 3.2 only; another patched that and handled only 3.3. You get the idea. This code pulls all of these forks together and moves forward. There is some serious refactoring and cleanup in this code base over those old forks.

This demonstrably does the best in decompiling Python across all Python versions. And even when there is another project that only provides decompilation for subset of Python versions, we generally do demonstrably better for those as well.

How can we tell? By taking Python bytecode that comes distributed with that version of Python and decompiling these. Among those that successfully decompile, we can then make sure the resulting programs are syntactically correct by running the Python interpreter for that bytecode version. Finally, in cases where the program has a test for itself, we can run the check on the decompiled code.

We are serious about testing, and use automated processes to find bugs. In the issue trackers for other decompilers, you will find a number of bugs we’ve found along the way. Very few to none of them are fixed in the other decompilers.

Requirements

The code here can be run on Python versions 2.6 or later, PyPy 3-2.4, or PyPy-5.0.1. Python versions 2.4-2.7 are supported in the python-2.4 branch. The bytecode files it can read have been tested on Python bytecodes from versions 1.4, 2.1-2.7, and 3.0-3.8 and the above-mentioned PyPy versions.

Installation

This uses setup.py, so it follows the standard Python routine:

pip install -e .  # set up to run from source tree
                  # Or if you want to install instead
python setup.py install # may need sudo

A GNU makefile is also provided so make install (possibly as root or sudo) will do the steps above.

Testing

make check

A GNU makefile has been added to smooth over setting running the right command, and running tests from fastest to slowest.

If you have remake installed, you can see the list of all tasks including tests via remake --tasks

Usage

Run

$ uncompyle6 *compiled-python-file-pyc-or-pyo*

For usage help:

$ uncompyle6 -h

If you want strong verification of the correctness of the decompilation process, add the –verify option. But there are situations where this will indicate a failure, although the generated program is semantically equivalent. Using option –weak-verify will tell you if there is something definitely wrong. Generally, large swaths of code are decompiled correctly, if not the entire program.

You can also cross compare the results with pycdc . Since they work differently, bugs here often aren’t in that, and vice versa.

Known Bugs/Restrictions

The biggest known and possibly fixable (but hard) problem has to do with handling control flow. (Python has probably the most diverse and screwy set of compound statements I’ve ever seen; there are “else” clauses on loops and try blocks that I suspect many programmers don’t know about.)

All of the Python decompilers that I have looked at have problems decompiling Python’s control flow. In some cases we can detect an erroneous decompilation and report that.

In older versions of Python it was possible to verify bytecode by decompiling bytecode, and then compiling using the Python interpreter for that bytecode version. Having done this the bytecode produced could be compared with the original bytecode. However as Python’s code generation got better, this is no longer feasible.

There verification that we use that doesn’t check bytecode for equivalence but does check to see if the resulting decompiled source is a valid Python program by running the Python interpreter. Because the Python language has changed so much, for best results you should use the same Python version in checking as was used in creating the bytecode.

There are however an interesting class of these programs that is readily available give stronger verification: those programs that when run check some computation, or even better themselves.

And already Python has a set of programs like this: the test suite for the standard library that comes with Python. We have some code in test/stdlib to facilitate this kind of checking.

Python support is strongest in Python 2 for 2.7 and drops off as you get further away from that. Support is also probably pretty good for python 2.3-2.4 since a lot of the goodness of early the version of the decompiler from that era has been preserved (and Python compilation in that era was minimal)

There is some work to do on the lower end Python versions which is more difficult for us to handle since we don’t have a Python interpreter for versions 1.6, and 2.0.

In the Python 3 series, Python support is is strongest around 3.4 or 3.3 and drops off as you move further away from those versions. Python 3.0 is weird in that it in some ways resembles 2.6 more than it does 3.1 or 2.7. Python 3.6 changes things drastically by using word codes rather than byte codes. As a result, the jump offset field in a jump instruction argument has been reduced. This makes the EXTENDED_ARG instructions are now more prevalent in jump instruction; previously they had been rare. Perhaps to compensate for the additional EXTENDED_ARG instructions, additional jump optimization has been added. So in sum handling control flow by ad hoc means as is currently done is worse.

Between Python 3.5, 3.6 and 3.7 there have been major changes to the MAKE_FUNCTION and CALL_FUNCTION instructions.

Currently not all Python magic numbers are supported. Specifically in some versions of Python, notably Python 3.6, the magic number has changes several times within a version. We support only the released magic. There are also customized Python interpreters, notably Dropbox, which use their own magic and encrypt bytcode. With the exception of the Dropbox’s old Python 2.5 interpreter this kind of thing is not handled.

We also don’t handle PJOrion obfuscated code. For that try: PJOrion Deobfuscator to unscramble the bytecode to get valid bytecode before trying this tool. This program can’t decompile Microsoft Windows EXE files created by Py2EXE, although we can probably decompile the code after you extract the bytecode properly. For situations like this, you might want to consider a decompilation service like Crazy Compilers. Handling pathologically long lists of expressions or statements is slow.

There is lots to do, so please dig in and help.

See Also

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uncompyle6-3.3.1.tar.gz (1.6 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

uncompyle6-3.3.1-py36-none-any.whl (227.8 kB view details)

Uploaded Python 3.6

uncompyle6-3.3.1-py35-none-any.whl (227.8 kB view details)

Uploaded Python 3.5

uncompyle6-3.3.1-py34-none-any.whl (227.8 kB view details)

Uploaded Python 3.4

uncompyle6-3.3.1-py33-none-any.whl (221.3 kB view details)

Uploaded Python 3.3

uncompyle6-3.3.1-py32-none-any.whl (221.3 kB view details)

Uploaded Python 3.2

uncompyle6-3.3.1-py27-none-any.whl (215.5 kB view details)

Uploaded Python 2.7

uncompyle6-3.3.1-py26-none-any.whl (221.3 kB view details)

Uploaded Python 2.6

uncompyle6-3.3.1-py3.6.egg (443.8 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py3.5.egg (450.8 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py3.4.egg (453.0 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py3.3.egg (457.7 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py3.2.egg (452.3 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py2.7.egg (444.9 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py2.6.egg (445.6 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py2.5.egg (439.3 kB view details)

Uploaded Egg

uncompyle6-3.3.1-py2.4.egg (443.8 kB view details)

Uploaded Egg

File details

Details for the file uncompyle6-3.3.1.tar.gz.

File metadata

  • Download URL: uncompyle6-3.3.1.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1.tar.gz
Algorithm Hash digest
SHA256 88a99274b3e46d50aff6554b6d29b4efcc03fd92d750fa16aa607335c6df441e
MD5 1f89919c87f57ca739e479e0b8654ca0
BLAKE2b-256 dbac4f8fd585a6dd38fc9bb98dafc1ef39617b0501b2d821e3061ec4c8faa436

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py36-none-any.whl.

File metadata

  • Download URL: uncompyle6-3.3.1-py36-none-any.whl
  • Upload date:
  • Size: 227.8 kB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py36-none-any.whl
Algorithm Hash digest
SHA256 4111c362347b7c474aa1cc3fd20787b781aedaab49f4a895a544bd9fd3c7f264
MD5 e3ca9cb61ad736796cd479b1c57a8439
BLAKE2b-256 bbf50d3ed5918740c85202a4f3b46201ba88bed12b267aab9798a0a2e2765084

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py35-none-any.whl.

File metadata

  • Download URL: uncompyle6-3.3.1-py35-none-any.whl
  • Upload date:
  • Size: 227.8 kB
  • Tags: Python 3.5
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.6

File hashes

Hashes for uncompyle6-3.3.1-py35-none-any.whl
Algorithm Hash digest
SHA256 5a957a06282327e820b7958ca81fd8cc181cca0152b1b43a2d1cbebe6cf63a51
MD5 5636030ee70f267565dfe07a00462af0
BLAKE2b-256 83676fa583dd8ca1c285da8681a3b802b73b7c2469c4dedc320af1144dd6b7f1

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py34-none-any.whl.

File metadata

  • Download URL: uncompyle6-3.3.1-py34-none-any.whl
  • Upload date:
  • Size: 227.8 kB
  • Tags: Python 3.4
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py34-none-any.whl
Algorithm Hash digest
SHA256 55dba6af764987fe0cf34fca6d4c4d47c1d4adefa800dd703361205286882846
MD5 ce854a966f86c31c9ae79ae2e94e42e5
BLAKE2b-256 67ad2a729bf24a34f9bc216156989867fa9ebe95679f1069b5af3896c0e84de7

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py33-none-any.whl.

File metadata

  • Download URL: uncompyle6-3.3.1-py33-none-any.whl
  • Upload date:
  • Size: 221.3 kB
  • Tags: Python 3.3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py33-none-any.whl
Algorithm Hash digest
SHA256 1b3b5a5eb8ff0e5564dd7c6f5778a8b2196d19c85786d8f8b45e7ae3b8eff717
MD5 96a1030e45b9bbd34e96d49b86f54849
BLAKE2b-256 631ecbf07d8baa68e9bc0a37215d472072108a2f9e6229ea3973b3942bc0898f

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py32-none-any.whl.

File metadata

  • Download URL: uncompyle6-3.3.1-py32-none-any.whl
  • Upload date:
  • Size: 221.3 kB
  • Tags: Python 3.2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py32-none-any.whl
Algorithm Hash digest
SHA256 3ca3a88c30ec254c4f587f7ebee837431af94dd33f7369726cdff97163fe1489
MD5 a666d8400a95eb0154b683cbb550925a
BLAKE2b-256 4cb07f8faa7b0a71e106beb8bead8555b069d97147e3b2533fddb4be08c96aa6

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py27-none-any.whl.

File metadata

  • Download URL: uncompyle6-3.3.1-py27-none-any.whl
  • Upload date:
  • Size: 215.5 kB
  • Tags: Python 2.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py27-none-any.whl
Algorithm Hash digest
SHA256 4c2a314167dbcb7f193eff53a5015683dc272573163e4932976ed8b42e9a1a72
MD5 f6d1cce408f9fa9b6a3cce0978ff0306
BLAKE2b-256 230ab7b7ea7705633012f177e35dbd38c317d79c3a00a8b498921af82459531e

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py26-none-any.whl.

File metadata

  • Download URL: uncompyle6-3.3.1-py26-none-any.whl
  • Upload date:
  • Size: 221.3 kB
  • Tags: Python 2.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py26-none-any.whl
Algorithm Hash digest
SHA256 1dbc19b3520204e1db2a8a14d948a6aeef68fb9f6fe2337b206bbcbcfd83a751
MD5 b99a5d4d2665e6b78252a9193d20a24e
BLAKE2b-256 90aef0ec3500b71b169897143f7cfa3c8ec5264b9e9a73f128257b4e1cc5fb97

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py3.6.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py3.6.egg
  • Upload date:
  • Size: 443.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py3.6.egg
Algorithm Hash digest
SHA256 9def5e659d2dda7c4cd98af0b1124e1939f4880d93b5573e5fb5dbbc265a9e25
MD5 331ad2fce2f94c59ed34de497000bab7
BLAKE2b-256 6007f06ab426b9bb66304b4e345a33434dc3618804f80fec8f91643f33f966d8

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py3.5.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py3.5.egg
  • Upload date:
  • Size: 450.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.6

File hashes

Hashes for uncompyle6-3.3.1-py3.5.egg
Algorithm Hash digest
SHA256 022bbc31dabe622692fa86ca42466cbbdce6c75651121694947846822e23305d
MD5 2f35dbe2aa3a024f77abbe2f4575b17d
BLAKE2b-256 2122c3f2f553cb4c2a39d02c46c10cd64a7f5038d458e1f2afb6dd043588fcbb

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py3.4.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py3.4.egg
  • Upload date:
  • Size: 453.0 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py3.4.egg
Algorithm Hash digest
SHA256 34244325d58403fb7a486c07d380ef05a272778d3ae7c97e5fd321979259a564
MD5 f3dad32d98f5781edee4fbb5c2530f63
BLAKE2b-256 1d244f66855f21e957f2f464d395a1451b757c10a384ee0dd86325b618583f66

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py3.3.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py3.3.egg
  • Upload date:
  • Size: 457.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py3.3.egg
Algorithm Hash digest
SHA256 635a52e256c3a037c4499389670ef64d365fbfca7bf02e0db0dd94d6b4670e73
MD5 72041fb2e0e0afe00d61603002696844
BLAKE2b-256 76879047ffce66437f4544ffbb7f1a337803c4eff32a5102eb2badc0241f18ed

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py3.2.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py3.2.egg
  • Upload date:
  • Size: 452.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py3.2.egg
Algorithm Hash digest
SHA256 37d1f4a85a0a48857039320e687f1c58876c9574c1d3c36db455f1919c47b998
MD5 4dd7f90b775510c2e2f2efcfdbcfceca
BLAKE2b-256 31e6ac2bf4db59c6e07ad15b144c2b0717cea0455859d5ff3ae673aeb8a3cb4f

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py2.7.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py2.7.egg
  • Upload date:
  • Size: 444.9 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py2.7.egg
Algorithm Hash digest
SHA256 af2f1d2664ae84fafcbdf7e25bc9ad64a1e0b8a555c646d8eba5b589e1499208
MD5 9de6a527735805783790ab5c48b6ae86
BLAKE2b-256 34b474c7b0fdbc8d8b15875cedb948efc325277a3b3c7448f08809d7312792a1

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py2.6.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py2.6.egg
  • Upload date:
  • Size: 445.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py2.6.egg
Algorithm Hash digest
SHA256 028cc176ca4101cdd43f96bab4111d9c5e7b567bdfc3e66c8caede44262f348b
MD5 9234e428ba86b17898adefd0525296f4
BLAKE2b-256 5b339a200f5d91a696d43cc3db083608ba1c0cc7fae57cc7babd2f053a04c25c

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py2.5.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py2.5.egg
  • Upload date:
  • Size: 439.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py2.5.egg
Algorithm Hash digest
SHA256 4922408059908d6a5be7d04a6b362adc28cecf38f53fc12662fafd627adb1210
MD5 8c995de806cc52649077cc40b4a65e5b
BLAKE2b-256 8aa5775dbe2182359cd8bbb680eaa69462ed8b18722720127fa176d39bf61ea2

See more details on using hashes here.

File details

Details for the file uncompyle6-3.3.1-py2.4.egg.

File metadata

  • Download URL: uncompyle6-3.3.1-py2.4.egg
  • Upload date:
  • Size: 443.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.8

File hashes

Hashes for uncompyle6-3.3.1-py2.4.egg
Algorithm Hash digest
SHA256 9b4585fe04b04e501025013edd4732eebd1a402b7e933dc60fb86efe3e7c063e
MD5 f7e7748815f9f4196c7143bd80d3c957
BLAKE2b-256 03a5baee49d0cd807a48d83d2aa916e96a647115393c083c8f15a81a87d762af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page