views:

12627

answers:

8

Is there a library or application that can decompile Python 2.4+ bytecode to obtain the source code?

A search revealed:

  • http://depython.net - an online service that you need to upload a pyc or pyo file to
  • the dis module - allows you to disassemble, but not decompile bytecode
  • decompile.py - works only for 1.5.2 or 2.0
  • decompyle - an decompiling online service that you need to pay for and upload your pyc to
A: 

I am incredibly surprised at the accuracy of depython.net

What I think they do is use the dis module and reconstruct your code with it. You'll have to find a way to do that or someone who has written the algorithm already.

Nick Stinemates
+3  A: 

I've used decompyle (the Ubuntu package, not the online service, I don't know if they're the same thing, though) in the past and was more than satisfied with the results. It saved me hours of work after a rm *.py instead of rm *.pyc.

Can Berk Güder
Yet another reason why version control is your friend. :-)
Just Some Guy
+3  A: 

Here is a little more info on decompyle: this is the same software that became the commercial decompyle service. It used to be open source and an old version of it is available/maintained as a debian package (including source code).

It will decompile Python up to version 2.3, but not 2.4+.

dF
+11  A: 

As others said, the free version of decompyle only works up to 2.3. But sometimes you can get it to work by converting your newer pyc to the old marshalling format.

The following script takes two arguments, the input and the output file, and converts it into something which decompyle will at least try its teeth on.

#!/usr/bin/python
import marshal
import sys

MAGIC23 = ';\xf2\r\n'

def load_pyc(filename):
        f = open(filename, 'rb')
        try:
                magic = f.read(4)
                timestamp = f.read(4)
                codeobject = marshal.load(f)
        finally:
                f.close()
                return magic, timestamp, codeobject

def dump_pyc_23(filename, timestamp, codeobject):
        assert len(timestamp)==4
        f = open(filename, 'wb')
        try:
                f.write(MAGIC23)
                f.write(timestamp)
                marshal.dump(codeobject, f, 0)
        finally:
                f.close()

magic, timestamp, codeobject = load_pyc(sys.argv[1])
dump_pyc_23(sys.argv[2], timestamp, codeobject)

Good Luck!

Sec
+2  A: 

There is a fork of decompyle called unpyc that has seen some activity recently. I tried using it with some pyc files but it didn't work with them.

Plinio
+3  A: 

I have a good experience with UnPyc - it perfectly recovered my Django models.py.

washeck
Just used UnPyc for a Fabric file and it worked very well. It got me about 90% back to original file.
richleland
A: 

You don't want an online service but depython.com is a good Python decompile service. Give it a try.

paloturk
A: 

hey how can I convert a .pyo file to .py file, I have to reach its source code. Is there anyone help me?

celka
-1: Not an answer.
gorsky