views:

1741

answers:

2

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 decompyle, but its outdated (python2.3) and no one maintains it anymore. Is there anything for python2.5 or later?

A: 

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.

Shay Erlichmen
Yes. For python 2.3, anything usable with Python 2.5+?
+6  A: 

UnPyc

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

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

From the project's readme.txt: "D - decompile (not implemented yet)". So no, this is a poor substitute for decompyle.
Chris S