偶然地,我丢失了所有项目源代码。但是我仍然有我的.pyc文件。我需要反编译它们的帮助。我下载了可以反编译python 3.2文件的unpyc3脚本。并进行了更改以允许其正确读取python 3.3 pyc文件:

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)

通过运行此代码,我得到以下错误:“'str'对象在这里没有属性'co_cellvars'”:
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]

我发生在初始化代码类时出现字符串而不是代码对象code_obj的情况。
我需要帮助弄清楚为什么会发生这种情况以及如何解决它。如果有人知道unpyc3的工作原理并可以提供帮助,请给我写信。我可以发送.pyc示例。

最佳答案

经过一些认真的调试后,我发现修改MAKE_FUNCTION的代码应该可以解决问题:

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)

希望这可以帮助!

关于python - 使用unpyc3反编译Python 3.3 .pyc,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22096717/

10-12 17:01
查看更多