Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 not have the source files and I cannot obtain it.

I am looking for a free solution.

like image 892
sorin Avatar asked Oct 05 '11 15:10

sorin


People also ask

Does Python 2 code run in Python 3?

Python 2.6 includes many capabilities that make it easier to write code that works on both 2.6 and 3. As a result, you can program in Python 2 but using certain Python 3 extensions... and the resulting code works on both.

What is Python bytecode?

The bytecode can be thought of as a series of instructions or a low-level program for the Python interpreter. After version 3.6, Python uses 2 bytes for each instruction. One byte is for the code of that instruction which is called an opcode, and one byte is reserved for its argument which is called the oparg.


1 Answers

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
like image 134
spam_eggs Avatar answered Sep 28 '22 09:09

spam_eggs