开发者

How to recompile the Python 2.5 bytecode to 2.7?

How can I recompile some .pyc files made by Python 2.5 with Python 2.7?

I do no开发者_高级运维t have the source files and I cannot obtain it.

I am looking for a free solution.


You will need Python 2.5 and 2.7 and byteplay (http://code.google.com/p/byteplay/) installed to both.

diz.py:

#!/usr/bin/env python
import byteplay, marshal, sys
if __name__ == '__main__':
    sys.stdin.read(8)
    c = byteplay.Code.from_code(marshal.load(sys.stdin)).code
    labels = set([ x for l in c for x in l if isinstance(x, byteplay.Label) ])
    labels = dict([(l,i) for (i,l) in enumerate(labels)])
    byteplay.Label.__repr__ = lambda self: "labels[%d]" % labels[self]
    print repr(c)

az.py:

#!/usr/bin/env python
import byteplay, sys, imp, struct, marshal, time
if __name__ == '__main__':
    byteplay.labels = dict([(i, byteplay.Label()) for i in xrange(10000)])
    if sys.platform == "win32":
        import os, msvcrt
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    asm = sys.stdin.read()
    c = eval(asm, byteplay.__dict__)
    c = byteplay.Code(c, (), (), 0, 0, 0, '', '', 0, '').to_code()
    sys.stdout.write(imp.get_magic())
    sys.stdout.write(struct.pack('<L', time.time()))
    marshal.dump(c, sys.stdout)

usage:

python2.5 diz.py < foo.pyc > foo.az
python2.7 az.py < foo.az > foo.2.7.pyc
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜