After some serious debugging, I found that modifying the code from MAKE_FUNCTION should do the trick :
def MAKE_FUNCTION(self, addr, argc, is_closure = False): testType = self.stack.pop().val if isinstance(testType, str): code = Code(self.stack.pop().val, self.code) else: code = Code(testType, self.code)
Accedentely I lost all my project source code. But I still have my .pyc files. I need help decompiling them. I downloaded unpyc3 script that can decompile python 3.2 files. And made a change to allow it read python 3.3 pyc files correctly:, 4 days ago Feb 28, 2014 · I downloaded unpyc3 script that can decompile python 3.2 files. And made a change to allow it read python 3.3 pyc files correctly: def read_code (stream): # This helper is needed in order for the PEP 302 emulation to # correctly handle compiled files # Note: stream must be opened in "rb" mode import marshal magic = stream.read (4) if magic ... , I suspect that attempts there will be fewer ad-hoc attempts like unpyc37 (which is based on a 3.3 decompiler) simply because it is harder to do so. The good news, at least from my standpoint, is that I think I understand what's needed to address the problems in a more robust way. , As Python progresses decompilation also gets harder because the compilation is more sophisticated and the language itself is more sophisticated. I suspect that attempts there will be fewer ad-hoc attempts like unpyc37 (which is based on a 3.3 decompiler) simply because it is harder to do so.
def read_code(stream): # This helper is needed in order
for the PEP 302 emulation to # correctly handle compiled files # Note: stream must be opened in "rb"
mode
import marshal magic = stream.read(4) if magic != imp.get_magic(): print("*** Warning: file has wrong magic number ***") stream.read(8) # Skip timestamp and additional 4 bytes
for python 3.3
return marshal.load(stream)
def read_code(stream): # This helper is needed in order
for the PEP 302 emulation to # correctly handle compiled files # Note: stream must be opened in "rb"
mode
import marshal magic = stream.read(4) if magic != imp.get_magic(): print("*** Warning: file has wrong magic number ***") stream.read(8) # Skip timestamp and additional 4 bytes
for python 3.3
return marshal.load(stream)
class Code: def __init__(self, code_obj, parent = None): self.code_obj = code_obj self.parent = parent self.derefnames = [PyName(v) for v in code_obj.co_cellvars + code_obj.co_freevars]
def MAKE_FUNCTION(self, addr, argc, is_closure = False): testType = self.stack.pop().valif isinstance(testType, str): code = Code(self.stack.pop().val, self.code) else: code = Code(testType, self.code)
The aim is to be able to recreate Python3 source code from code objects. Current version is able to decompile itself successfully :). It has been tested with Python 3.3 only.,It currently reconstructs most of Python 3 constructs but probably needs to be tested more thoroughly. All feedback welcome.,the unpyc3 module is able de decompile itself! (try import unpyc3; unpyc3.decompile(unpyc3)) so theorically I could just distribute the .pyc file.,There was a problem preparing your codespace, please try again.
>>> from unpyc3
import decompile
>>>
def foo(x, y, z = 3, * args):
...global g
...
for i, j in zip(x, y):
...
if z == i + j or args[i] == j:
...g = i, j
...
return
...
>>>
print(decompile(foo))
def foo(x, y, z = 3, * args):
global g
for i, j in zip(x, y):
if z == i + j or args[i] == j:
g = i, j
return >>>