Как перекомпилировать байт-код Python 2.5 до 2.7? - PullRequest
12 голосов
/ 05 октября 2011

Как мне перекомпилировать некоторые файлы .pyc, созданные Python 2.5 с Python 2.7?

У меня нет исходных файлов, и я не могу получить его.

Я ищу бесплатное решение.

1 Ответ

3 голосов
/ 08 октября 2011

Вам понадобятся Python 2.5 и 2.7 и byteplay (http://code.google.com/p/byteplay/) установлен на обоих.

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)

использование:

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