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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With