Exploring and decompiling python bytecode [closed]

天大地大妈咪最大 提交于 2019-12-28 05:02:24

问题


Lets say I have:

>>> def test(a):    
>>>    print a

Now, I want to explore see how test looks like in its compiled form.

>>> test.func_code.co_code
'|\x00\x00GHd\x00\x00S'

I can get the disassembled form using the dis module:

>>> import dis
>>> dis.dis(test)
  2           0 LOAD_FAST                0 (a)
              3 PRINT_ITEM
              4 PRINT_NEWLINE
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE

Is there an opensource and maintained decompiler I could use to turn the bytecode back into readable python code?

update: thanks for suggesting decompile, but it's outdated (python2.3) and no one maintains it anymore. Is there anything for python2.5 or later?


回答1:


UnPyc

http://sourceforge.net/projects/unpyc/

It is a maintained fork of the old decompyle updated to work with 2.5 and 2.6.




回答2:


get uncompyle2 from github! :)




回答3:


There is also now uncompyle6 which is written in Python and pycdc which is written in C++.

Both of these handle several versions of Python bytecode including Python 2 versions and Python 3 versions.




回答4:


decompyle

Decompyle is a python disassembler and decompiler which converts Python byte-code (.pyc or .pyo) back into equivalent Python source. Verification of the produced code (re-compiled) is avaliable as well.




回答5:


Uncompyle2 worked for me with Python 2.7.

https://github.com/wibiti/uncompyle2

Quick how to use uncompyle2 , Install it and then

>>>import uncompyle2
>>> with open("decompiled.py","wb") as f:
...   uncompyle2.uncompyle_file("compiled.pyc",f)

It will generate source code back in decompile.py




回答6:


In addition to what DevC wrote:

  1. Uncompyle2 works with Python 2.7

  2. with Uncompyle2, you can also un-compile from the command line:

    $ uncompyle2 compiled.pyc >> source.uncompyle2.py

  3. to install Uncompyle2, do

    $ git clone https://github.com/wibiti/uncompyle2

    $ cd uncompyle2

    $ sudo ./setup.py install



来源:https://stackoverflow.com/questions/1149513/exploring-and-decompiling-python-bytecode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!