Introduction
When CPython imports a module from a
.py file, it compiles the source to bytecode
and saves the result as a .pyc file in a __pycache__ folder
beside the source. The next import loads the .pyc file and skips the
compiler. The file holds the module’s code object: its instructions, constants and
names, with the code object of every function and class nested inside.
This page covers the format written by Python 3.10. Line numbers are stored in
co_linetable, a table of byte ranges and line steps, as PEP 626 specifies,
and a jump’s argument counts instructions rather than bytes.
A file only runs on the release that wrote it. The magic number at the start says which one that is.
A .pyc file dropped on any page of this site opens in the explorer, which
shows its structure, a disassembly and each code object.
Structure
A file is a 16-byte header followed by one object in Python’s
marshal format, the module’s code object. All numbers are little-endian.
0magic: 3439, then0D 0A4 bytes4flags: 0 for a date check, 1 or 3 for a hash check4 bytes8source_mtime and source_size, or source_hash8 bytes16code object of the module, onemarshalobjectvaries
The code object holds counts, flags, the instructions as a byte string, a tuple of constants (which holds nested code objects), the names it uses, and a line table. Exception handling is done by instructions in the code, not by a table.
What Python 3.10 changed
- Line numbers moved from
co_lnotabtoco_linetable, which can say that an instruction has no line at all (PEP 626, magic 3431). - A jump’s argument counts instructions instead of bytes (magic 3435).
matchstatements addedMATCH_CLASS,MATCH_KEYS,MATCH_MAPPING,MATCH_SEQUENCE,GET_LENandCOPY_DICT_WITHOUT_KEYS(PEP 634, magic 3434).GEN_STARTandROT_Nwere added.- The annotations passed to
MAKE_FUNCTIONbecame a tuple, not a dictionary. RERAISErestoresf_lastiwhen its argument is nonzero.
Each pre-release number is on the
header page. Files of 3430 to 3438 are
from pre-releases only, and each lacks the changes that came after its number: a 3430
file still has co_lnotab, files before 3435 count jumps in bytes, and
GEN_START and ROT_N first appear at 3436 and 3439. The
explorer follows the line table and jump rules by magic number, but names opcodes from
the final 3.10 table for every file, so before 3433 RERAISE, then opcode
48, shows by number.
Telling it apart
The first two bytes, little-endian, are the magic number, and the next two are
0D 0A. The final number of Python 3.10 is 3439, and its pre-releases used
3430 to 3438. The explorer takes this range and reads the file strictly: only 0, 1 and 3
are valid flags, and the code object must fill the rest of the file exactly.
Python 3.11 files have a magic number of 3450 or more, and a code object with no
co_nlocals and with an exception table.
00000000 6F 0D 0D 0A 00 00 00 00 00 F1 53 65 06 00 00 00
Not described
The viewer and this reference were checked against a real CPython 3.10 interpreter, with no differences on up to 250 library modules. What remains:
Known, not yet covered
- Viewer: files from 3.10 pre-releases (alpha, beta and candidate magic numbers) are read with the final 3.10 opcode table, so an instruction name can be wrong; the viewer shows a notice for them.
- Viewer: the interpreter comparison only exercised the opcodes that appear in the test modules as live instructions; the rest are checked against the opcode table.
- Reference: the header’s flags, timestamp and source size (or source hash) were not compared field by field with what the interpreter wrote.
- Reference: other implementations (PyPy, MicroPython’s
.mpy, Cython-built files) are not CPython.pycfiles and are not described.
Variants
- Python 1.5 to 2.0 bytecode – 8-byte header, one or three-byte instructions and 16-bit numbers in the code object
- Python 2.1 and 2.2 bytecode – adds free and cell variables to the code object
- Python 2.3 to 2.7 bytecode – the code object’s numbers become 32-bit
- Python 3.0 to 3.2 bytecode – keyword-only arguments; text and bytes become separate types
- Python 3.3 to 3.5 bytecode – 12-byte header with the source size; object references from 3.4
- Python 3.6 bytecode – two-byte instructions
- Python 3.7 bytecode – 16-byte header with a flags field
- Python 3.8 and 3.9 bytecode – positional-only arguments
- Python 3.10 bytecode –
co_linetable, and jumps that count instructions - Python 3.11 bytecode – one list of locals, a location table, an exception table and inline caches
- Python 3.12 bytecode – inlined comprehensions, method loads folded into
LOAD_ATTR, forward-only conditional jumps - Python 3.13 bytecode – instructions renumbered, explicit conversion to
bool, caches on jumps - Python 3.14 bytecode –
marshalversion 5 with slice constants, small integers in the argument - Python 3.15 bytecode –
marshalversion 6, lazy imports and a cache onRESUME
Links
Software
- CPython – the Python interpreter, which writes and loads these files;
python -m disshows the bytecode (Windows, macOS, Linux) - Decompyle++ (pycdc) – disassembler and decompiler for
.pycfiles of many Python versions (Windows, macOS, Linux)
Developers
- dis – Python’s own bytecode disassembler (Python)
- marshal – Python’s own module for the object format of the body (Python)
- py_compile – Python’s own module that writes
.pycfiles (Python) - importlib – Python’s import system, which reads and checks them (Python)
- xdis – cross-version disassembler (Python)
Reference
- dis – Python’s documentation of its bytecode instructions (Python 3.10)
- marshal – Python’s documentation of the module and its version numbers
- importlib – Python’s documentation of the import system
- PEP 3147 – the
__pycache__folder and its file names - PEP 552 – the flags field and hash-based
.pycfiles - PEP 626 – precise line numbers for tracing
- lnotab_notes.txt – CPython’s description of the line number table (Python 3.10)