Skip to main content
edited body
Source Link
Valentin Lorentz
  • 9.8k
  • 7
  • 49
  • 70

I want to read a .pyc file. However, I cannot find any documentation on the format.

The only one I found found does not work for Python 3 (although it does for Python 2):

>>> f = open('__pycache__/foo.cpython-34.pyc', 'rb') >>> f.read(4) b'\xee\x0c\r\n' >>> f.read(4) b'\xf8\x17\x08W' >>> marshal.load(f) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: bad marshal data (unknown type code) 

marshal only consumes one byte: \x00, which indeed is not a valid first character for marshall (as a comparison, the first byte of Python 2 bytecode for the same empty module is c)

So, how can I decode what comes after the header?

I want to read a .pyc file. However, I cannot find any documentation on the format.

The only one I found does not work for Python 3 (although it does for Python 2):

>>> f = open('__pycache__/foo.cpython-34.pyc', 'rb') >>> f.read(4) b'\xee\x0c\r\n' >>> f.read(4) b'\xf8\x17\x08W' >>> marshal.load(f) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: bad marshal data (unknown type code) 

marshal only consumes one byte: \x00, which indeed is not a valid first character for marshall (as a comparison, the first byte of Python 2 bytecode for the same empty module is c)

So, how can I decode what comes after the header?

I want to read a .pyc file. However, I cannot find any documentation on the format.

The only one I found does not work for Python 3 (although it does for Python 2):

>>> f = open('__pycache__/foo.cpython-34.pyc', 'rb') >>> f.read(4) b'\xee\x0c\r\n' >>> f.read(4) b'\xf8\x17\x08W' >>> marshal.load(f) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: bad marshal data (unknown type code) 

marshal only consumes one byte: \x00, which indeed is not a valid first character for marshall (as a comparison, the first byte of Python 2 bytecode for the same empty module is c)

So, how can I decode what comes after the header?

Source Link
Valentin Lorentz
  • 9.8k
  • 7
  • 49
  • 70

Python 3 bytecode format

I want to read a .pyc file. However, I cannot find any documentation on the format.

The only one I found does not work for Python 3 (although it does for Python 2):

>>> f = open('__pycache__/foo.cpython-34.pyc', 'rb') >>> f.read(4) b'\xee\x0c\r\n' >>> f.read(4) b'\xf8\x17\x08W' >>> marshal.load(f) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: bad marshal data (unknown type code) 

marshal only consumes one byte: \x00, which indeed is not a valid first character for marshall (as a comparison, the first byte of Python 2 bytecode for the same empty module is c)

So, how can I decode what comes after the header?