METADATA 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. Metadata-Version: 2.4
  2. Name: uncompyle6
  3. Version: 3.9.3
  4. Summary: Python cross-version byte-code library and disassembler
  5. Author-email: Rocky Bernstein <rb@dustyfeet.com>
  6. License: GPL
  7. Project-URL: Homepage, https://github.com/rocky/python-uncompyle6
  8. Project-URL: Downloads, https://github.com/rocky/python-uncompyle6/releases
  9. Keywords: Python bytecode,bytecode,disassembler
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Intended Audience :: Developers
  12. Classifier: Programming Language :: Python
  13. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  14. Classifier: Programming Language :: Python :: 2.4
  15. Classifier: Programming Language :: Python :: 2.5
  16. Classifier: Programming Language :: Python :: 2.6
  17. Classifier: Programming Language :: Python :: 2.7
  18. Classifier: Programming Language :: Python :: 3.0
  19. Classifier: Programming Language :: Python :: 3.1
  20. Classifier: Programming Language :: Python :: 3.2
  21. Classifier: Programming Language :: Python :: 3.3
  22. Classifier: Programming Language :: Python :: 3.4
  23. Classifier: Programming Language :: Python :: 3.5
  24. Classifier: Programming Language :: Python :: 3.6
  25. Classifier: Programming Language :: Python :: 3.7
  26. Classifier: Programming Language :: Python :: 3.8
  27. Classifier: Programming Language :: Python :: 3.9
  28. Classifier: Programming Language :: Python :: 3.10
  29. Classifier: Programming Language :: Python :: 3.11
  30. Classifier: Programming Language :: Python :: 3.12
  31. Classifier: Programming Language :: Python :: 3.13
  32. Description-Content-Type: text/x-rst
  33. License-File: COPYING
  34. Requires-Dist: click
  35. Requires-Dist: spark-parser<1.9.2,>=1.8.9
  36. Requires-Dist: xdis<6.3,>=6.1.0
  37. Provides-Extra: dev
  38. Requires-Dist: pre-commit; extra == "dev"
  39. Requires-Dist: pytest; extra == "dev"
  40. Dynamic: license-file
  41. |buildstatus| |Pypi Installs| |Latest Version| |Supported Python Versions|
  42. |packagestatus|
  43. .. contents::
  44. uncompyle6
  45. ==========
  46. A native Python cross-version decompiler and fragment decompiler.
  47. The successor to decompyle, uncompyle, and uncompyle2.
  48. I gave a talk on this at `BlackHat Asia 2024 <https://youtu.be/H-7ZNrpsV50?si=nOaixgYHr7RbILVS>`_.
  49. Introduction
  50. ------------
  51. *uncompyle6* translates Python bytecode back into equivalent Python
  52. source code. It accepts bytecodes from Python version 1.0 to version
  53. 3.8, spanning over 24 years of Python releases. We include Dropbox's
  54. Python 2.5 bytecode and some PyPy bytecodes.
  55. Why this?
  56. ---------
  57. Ok, I'll say it: this software is amazing. It is more than your
  58. normal hacky decompiler. Using compiler_ technology, the program
  59. creates a parse tree of the program from the instructions; nodes at
  60. the upper levels that look a little like what might come from a Python
  61. AST. So we can really classify and understand what's going on in
  62. sections of Python bytecode.
  63. Building on this, another thing that makes this different from other
  64. CPython bytecode decompilers can deparse just
  65. *fragments* of source code and give source-code information around a given bytecode offset.
  66. I use the tree fragments to deparse fragments of code *at run time*
  67. inside my trepan_ debuggers_. For that, bytecode offsets are recorded
  68. and associated with fragments of the source code. This purpose,
  69. although compatible with the original intention, is yet a little bit
  70. different. See this_ for more information.
  71. Python fragment deparsing, given an instruction offset, is useful in
  72. showing stack traces and can be incorporated into any program that
  73. wants to show a location in more detail than just a line number at
  74. runtime. This code can also be used when source code information does
  75. not exist and there is just bytecode. Again, my debuggers make use of
  76. this.
  77. There were (and still are) several decompyle, uncompyle,
  78. uncompyle2, uncompyle3 forks around. Many of them come basically from
  79. the same code base, and (almost?) all of them are no longer actively
  80. maintained. One was really good at decompiling Python 1.5-2.3, another is really good at Python 2.7,
  81. but only that. Another handles Python 3.2
  82. only; another patched that and handled only 3.3. You get the
  83. idea. This code pulls all of these forks together and *moves
  84. forward*. There is some serious refactoring and cleanup in this code
  85. base over those old forks. Even more experimental refactoring is going
  86. on in decompyle3_.
  87. This demonstrably does the best in decompiling Python across all
  88. Python versions. And even when there is another project that only
  89. provides decompilation for a subset of Python versions, we generally do
  90. demonstrably better for those as well.
  91. How can we tell? By taking Python bytecode that comes distributed with
  92. that version of Python and decompiling it. Among those that
  93. successfully decompile, we can then make sure the resulting programs
  94. are syntactically correct by running the Python interpreter for that
  95. bytecode version. Finally, in cases where the program has a test for
  96. itself, we can run the check on the decompiled code.
  97. We use automated processes to find bugs. In the issue trackers for
  98. other decompilers, you will find several bugs we've found along
  99. the way. Very few of them are fixed in the other decompilers.
  100. Requirements
  101. ------------
  102. The code in the git repository can be run from Python 2.4 to the
  103. latest Python version, except Python 3.0 through
  104. 3.2. Volunteers are welcome to address these deficiencies if there is a
  105. desire to do so.
  106. The way it does this, though, is by segregating consecutive Python versions into
  107. git branches:
  108. master
  109. Python 3.11 and up
  110. python-3.6-to-3.10
  111. Python 3.6 to python-3.10 (uses type annotations)
  112. python-3.3-to-3.5
  113. Python 3.3 through 3.5 (Generic Python 3)
  114. python-2.4
  115. Python 2.4 through 2.7 (Generic Python 2)
  116. PyPy 3-2.4 and later works as well.
  117. The bytecode files it can read have been tested on Python
  118. bytecodes from versions 1.4, 2.1-2.7, and 3.0-3.8 and later PyPy
  119. versions.
  120. Installation
  121. ------------
  122. You can install from PyPI using the name ``uncompyle6``::
  123. pip install uncompyle6
  124. To install from source code, this project uses setup.py, so it follows the standard Python routine::
  125. $ pip install -e . # set up to run from source tree
  126. or::
  127. $ python setup.py install # may need sudo
  128. A GNU Makefile is also provided, so :code:`make install` (possibly as root or
  129. sudo) will do the steps above.
  130. Running Tests
  131. -------------
  132. ::
  133. make check
  134. A GNU makefile has been added to smooth over setting up and running the right
  135. command, and running tests from fastest to slowest.
  136. If you have remake_ installed, you can see the list of all tasks
  137. including tests via :code:`remake --tasks`
  138. Usage
  139. -----
  140. Run
  141. ::
  142. $ uncompyle6 *compiled-python-file-pyc-or-pyo*
  143. For usage help:
  144. ::
  145. $ uncompyle6 -h
  146. Verification
  147. ------------
  148. In older versions of Python, it was possible to verify bytecode by
  149. decompiling it and then compiling using the Python interpreter
  150. for that bytecode version. Having done this, the bytecode produced
  151. could be compared with the original bytecode. However, as Python's code
  152. generation got better, this was no longer feasible.
  153. If you want Python syntax verification of the correctness of the
  154. decompilation process, add the :code:`--syntax-verify` option. However since
  155. Python syntax changes. You should use this option if the bytecode is
  156. the right bytecode for the Python interpreter that will be checking
  157. the syntax.
  158. You can also cross-compare the results with another version of
  159. *uncompyle6* since there are sometimes regressions in decompiling specific bytecode, as the overall quality improves.
  160. For Python 3.7 and 3.8, the code in decompyle3_ is generally
  161. better.
  162. Or try another specific Python decompiler like uncompyle2_, unpyc37_,
  163. or pycdc_. Since the latter two work differently, bugs here often
  164. aren't in that, and vice versa.
  165. There is an interesting class of these programs that is readily
  166. available to give stronger verification: those programs that, when run,
  167. test themselves. Our test suite includes these.
  168. And Python comes with another set of programs like this: its test
  169. suite for the standard library. We have some code in :code:`test/stdlib` to
  170. facilitate this kind of checking too.
  171. Known Bugs/Restrictions
  172. -----------------------
  173. The biggest known and possibly fixable (but hard) problem has to do with handling control flow. (Python has probably the most diverse and
  174. screwy set of compound statements I've ever seen; there
  175. are "else" clauses on loops and try blocks that I suspect many
  176. programmers don't know about.)
  177. All of the Python decompilers that I have looked at have problems
  178. decompiling Python's control flow. In some cases, we can detect an
  179. erroneous decompilation and report that.
  180. Python support is pretty good for Python 2
  181. On the lower end of Python versions, decompilation seems pretty good, although
  182. we don't have any automated testing in place for Python's distributed tests.
  183. Also, we don't have a Python interpreter for versions 1.6 and 2.0.
  184. In the Python 3 series, Python support is strongest around 3.4 or
  185. 3.3 and drops off as you move further away from those versions. Python
  186. 3.0 is weird in that it, in some ways, resembles 2.6 more than it does
  187. 3.1 or 2.7. Python 3.6 changes things drastically by using word codes
  188. rather than byte codes. As a result, the jump offset field in a jump
  189. instruction argument has been reduced. This makes the :code:`EXTENDED_ARG` instructions now more prevalent in jump instructions; previously
  190. they had been rare. Perhaps to compensate for the additional
  191. :code:`EXTENDED_ARG` instructions, additional jump optimization has been
  192. added. So in sum handling control flow by ad hoc means, as is currently
  193. done is worse.
  194. Between Python 3.5, 3.6, 3.7, there have been major changes to the
  195. :code:`MAKE_FUNCTION` and :code:`CALL_FUNCTION` instructions.
  196. Python 3.8 removes :code:`SETUP_LOOP`, :code:`SETUP_EXCEPT`,
  197. :code:`BREAK_LOOP`, and :code:`CONTINUE_LOOP`, instructions which may
  198. make control-flow detection harder, lacking the more sophisticated
  199. control-flow analysis that is planned. We'll see.
  200. Currently, not all Python magic numbers are supported. Specifically in
  201. some versions of Python, notably Python 3.6, the magic number has
  202. changes several times within a version.
  203. **We support only released versions, not candidate versions.** Note, however, that the magic of a released version is usually the same as
  204. the *last* candidate version prior to release.
  205. There are also customized Python interpreters, notably Dropbox,
  206. which use their own magic and encrypt bytecode. With the exception of
  207. Dropbox's old Python 2.5 interpreter, this kind of thing is not
  208. handled.
  209. We also don't handle PJOrion_ or otherwise obfuscated code. For
  210. PJOrion try: PJOrion Deobfuscator_ to unscramble the bytecode to get
  211. valid bytecode before trying this tool; pydecipher_ might help with that.
  212. This program can't decompile Microsoft Windows EXE files created by
  213. Py2EXE_, although we can probably decompile the code after you extract
  214. the bytecode properly. `Pydeinstaller <https://github.com/charles-dyfis-net/pydeinstaller>`_ may help with unpacking Pyinstaller bundlers.
  215. Handling pathologically long lists of expressions or statements is
  216. slow. We don't handle Cython_ or MicroPython, which don't use bytecode.
  217. There are numerous bugs in decompilation. And that's true for every
  218. other CPython decompilers I have encountered, even the ones that
  219. claimed to be "perfect" on some particular version like 2.4.
  220. As Python progresses, decompilation also gets harder because the
  221. compilation is more sophisticated and the language itself is more
  222. sophisticated. I suspect that attempts there will be fewer ad-hoc
  223. attempts like unpyc37_ (which is based on a 3.3 decompiler) simply
  224. because it is harder to do so. The good news, at least from my
  225. standpoint, is that I think I understand what's needed to address the
  226. problems in a more robust way. But right now, until such time as
  227. the project is better funded, I do not intend to make any serious effort
  228. to support Python versions 3.8 or 3.9, including bugs that might come
  229. in. I imagine at some point I may be interested in it.
  230. You can easily find bugs by running the tests against the standard
  231. test suite that Python uses to check itself. At any given time, there are
  232. dozens of known problems that are pretty well isolated and that could
  233. be solved if one were to put in the time to do so. The problem is that
  234. there aren't that many people who have been working on bug fixing.
  235. Some of the bugs in 3.7 and 3.8 are simply a matter of back-porting
  236. the fixes in *decompyle3*. Any volunteers?
  237. You may run across a bug that you want to report. Please do so after
  238. reading `How to report a bug
  239. <https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md>`_ and
  240. follow the `instructions when opening an issue <https://github.com/rocky/python-uncompyle6/issues/new?assignees=&labels=&template=bug-report.md>`_.
  241. Be aware that it might not get my attention for a while. If you
  242. sponsor or support the project in some way, I'll prioritize your
  243. issues above the queue of other things I might be doing instead. In
  244. rare situations, I can do a hand decompilation of bytecode for a fee.
  245. However, this is expensive, usually beyond what most people are willing
  246. to spend.
  247. See Also
  248. --------
  249. * https://rocky.github.io/blackhat-asia-2024-additional/all-notes-print.html : How to Read and Write a High-Level Bytecode Decompiler: ``uncompyle6`` ``decompyle3`` -- BlackHat 2024 Asia (`video <https://www.youtube.com/watch?v=NA77SFncppE>`_). A big thanks to the Organizers and Reviewers for letting me speak. This kind of thing encourages me to work on projects like this.
  250. * https://github.com/rocky/python-decompile3 : Much smaller and more modern code, focusing on 3.7 and 3.8. Changes in that will get migrated back here.
  251. * https://code.google.com/archive/p/unpyc3/ : supports Python 3.2 only. The above projects use a different decompiling technique than what is used here. Currently unmaintained.
  252. * https://github.com/figment/unpyc3/ : fork of above, but supports Python 3.3 only. Includes some fixes like supporting function annotations. Currently unmaintained.
  253. * https://github.com/wibiti/uncompyle2 : supports Python 2.7 only, but does that fairly well. There are situations where :code:`uncompyle6` results are incorrect, while :code:`uncompyle2` results are not, but more often uncompyle6 is correct when uncompyle2 is not. Because :code:`uncompyle6` adheres to accuracy over idiomatic Python, :code:`uncompyle2` can produce more natural-looking code when it is correct. Currently:code:`uncompyle2` is lightly maintained. See its issue `tracker <https://github.com/wibiti/uncompyle2/issues>`_ for more details.
  254. * `How to report a bug <https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md>`_
  255. * The HISTORY_ file.
  256. * https://github.com/rocky/python-xdis : Cross Python version disassembler
  257. * https://github.com/rocky/python-xasm : Cross Python version assembler
  258. * https://github.com/rocky/python-uncompyle6/wiki : Wiki Documents that describe the code and aspects of it in more detail
  259. * https://github.com/zrax/pycdc : The README for this C++ code says it aims to support all versions of Python. You can aim your slingshot for the moon, too, but I doubt you are going to hit it. This code is best for Python versions around 2.7 and 3.3, when the code was initially developed. Accuracy for current versions of Python 3 and early versions of Python is lacking. Without major effort, it is unlikely that it can be made to support the current Python 3. See its `issue tracker <https://github.com/zrax/pycdc/issues>`_ for details. Currently lightly maintained.
  260. .. _Cython: https://en.wikipedia.org/wiki/Cython
  261. .. _trepan: https://pypi.python.org/pypi/trepan3k
  262. .. _compiler: https://github.com/rocky/python-uncompyle6/wiki/How-does-this-code-work%3F
  263. .. _HISTORY: https://github.com/rocky/python-uncompyle6/blob/master/HISTORY.md
  264. .. _report_bug: https://github.com/rocky/python-uncompyle6/blob/master/HOW-TO-REPORT-A-BUG.md
  265. .. _debuggers: https://pypi.python.org/pypi/trepan3k
  266. .. _remake: https://bashdb.sf.net/remake
  267. .. _pycdc: https://github.com/zrax/pycdc
  268. .. _decompyle3: https://github.com/rocky/python-decompile3
  269. .. _uncompyle2: https://github.com/wibiti/uncompyle2
  270. .. _unpyc37: https://github.com/andrew-tavera/unpyc37
  271. .. _this: https://github.com/rocky/python-uncompyle6/wiki/Deparsing-technology-and-its-use-in-exact-location-reporting
  272. .. |buildstatus| image:: https://circleci.com/gh/rocky/python-uncompyle6.svg?style=svg
  273. :target: https://app.circleci.com/pipelines/github/rocky/python-uncompyle6
  274. .. |packagestatus| image:: https://repology.org/badge/vertical-allrepos/python:uncompyle6.svg
  275. :target: https://repology.org/project/python:uncompyle6/versions
  276. .. _PJOrion: http://www.koreanrandom.com/forum/topic/15280-pjorion-%D1%80%D0%B5%D0%B4%D0%B0%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5-%D0%BA%D0%BE%D0%BC%D0%BF%D0%B8%D0%BB%D1%8F%D1%86%D0%B8%D1%8F-%D0%B4%D0%B5%D0%BA%D0%BE%D0%BC%D0%BF%D0%B8%D0%BB%D1%8F%D1%86%D0%B8%D1%8F-%D0%BE%D0%B1%D1%84
  277. .. _pydecipher: https://github.com/mitre/pydecipher
  278. .. _Deobfuscator: https://github.com/extremecoders-re/PjOrion-Deobfuscator
  279. .. _Py2EXE: https://en.wikipedia.org/wiki/Py2exe
  280. .. |Supported Python Versions| image:: https://img.shields.io/pypi/pyversions/uncompyle6.svg
  281. .. |Latest Version| image:: https://badge.fury.io/py/uncompyle6.svg
  282. :target: https://badge.fury.io/py/uncompyle6
  283. .. |Pypi Installs| image:: https://pepy.tech/badge/uncompyle6/month