Skip to content

Python .pyc cache

stegobox.io.pycfile.read(filename)

Source code in stegobox/io/pycfile.py
def read(filename: str) -> tuple[bytes, types.CodeType]:
    with open(filename, "rb") as f:
        # This is specific to Python version over 3.7, where the header is 16 bytes
        # https://gist.github.com/stecman/3751ac494795164efa82a683130cabe5
        # For reference:
        # - Python 2.x uses a 2x 32-bit field header.
        # - Python 3.2 changed to a 3x 32-bit field header.
        # - And finally, the header size is 4 bytes longer from Python 3.7.
        header = f.read(16)
        code = marshal.load(f)
        return (header, code)

stegobox.io.pycfile.write(header, code, filename)

Source code in stegobox/io/pycfile.py
def write(header: bytes, code: types.CodeType, filename: str) -> None:
    with open(filename, "wb") as f:
        f.write(header)
        marshal.dump(code, f)