Every .pyc file of this format starts with the same 16 bytes of header,
all little-endian. The header says which release wrote the file and how to tell whether
it is out of date. The module’s code object follows at offset 16.
0magic: the number, then0D 0A4 bytes4flags4 bytes8source_mtime, or the first half of source_hash4 bytes12source_size, or the second half of source_hash4 bytes
Magic number
The first two bytes are a 16-bit number, and the next two are always
0D 0A, a carriage return and a line feed. A file copied in text mode has
those bytes changed, so the magic number no longer matches and the file is rejected.
CPython changes the number whenever the bytecode changes in a way an older interpreter cannot run. Python 3.n starts at 2900 + 50n, so 3.13 starts at 3550, and the number goes up by one for each change within the release. A file loads only on a release with exactly the same number. The file does not say which release it came from in any other way.
Every final Python 3.13 release writes 3571, the bytes F3 0D 0D 0A. The
numbers 3550 to 3570 come from the alphas; the full list follows
further down.
Flags
A 32-bit field that says how the file is checked against its source. Two bits are defined by PEP 552, and CPython rejects a file with any other bit set.
| Bit | Meaning |
|---|---|
| 0 | Set: the next 8 bytes are a hash of the source. Clear: they are the source’s date and size. |
| 1 | Only with bit 0: the hash is checked against the source when the file is loaded. |
| Value | Kind of file |
|---|---|
| 0 | Checked by the source’s date and size. The usual kind. |
| 1 | Hash-based, not checked when loaded. The file is used even if the source has changed. |
| 3 | Hash-based, checked when loaded. The source is hashed and the file is used only if the hash matches. |
The value 2, bit 1 alone, is never written. CPython’s importer would read such a file
as one checked by date and size, since bit 0 is clear; the explorer turns it down.
py_compile and compileall make hash-based files with their
invalidation mode options. The interpreter’s --check-hash-based-pycs
option (default, always or never) can override
the check when a hash-based file is loaded.
source_mtime
Unsigned 32-bit, at offset 8, when flags is 0. The source file’s modification time in seconds since 1970, as a Unix timestamp, kept to 32 bits. A file is stale if this differs from the source file’s current time.
source_size
Unsigned 32-bit, at offset 12, when flags is 0. The size of the source file in bytes, modulo 232. A file is stale if this differs from the source’s current size.
source_hash
8 bytes at offset 8, when bit 0 of flags is set. A 64-bit SipHash of the source file’s
bytes, keyed by the magic number. importlib.util.source_hash() computes
it. Python’s importer does not recompute it when flags is 1, unless
--check-hash-based-pycs always is given.
Examples
Both files were written by Python 3.13 with py_compile, from a six-byte
source file, x = 1 and a line feed.
00000000 F3 0D 0D 0A 00 00 00 00 00 F1 53 65 06 00 00 00
0x6553F100 (1700000000, 14 November 2023), source size 6 bytes00000000 F3 0D 0D 0A 03 00 00 00 E7 86 E2 89 36 51 12 0E
File names and folders
importlib stores a module’s compiled form in a __pycache__
folder beside the source, named with the interpreter’s tag, as PEP 3147 describes.
The tag gives the release, so several versions of Python can share one source folder.
| Source | Compiled |
|---|---|
spam.py, no optimisation | __pycache__/spam.cpython-313.pyc |
spam.py, with -O | __pycache__/spam.cpython-313.opt-1.pyc |
spam.py, with -OO | __pycache__/spam.cpython-313.opt-2.pyc |
A spam.pyc file with no spam.py beside it is loaded as a
module that has no source. The .pyo extension is not used in this format.
All magic numbers
The list is the one kept in Python 3.13’s own source, in the comments of
Lib/importlib/_bootstrap_external.py. The number marked final is the one
every released 3.13 version writes. The others come from alphas, and the release shown
is the pre-release that introduced them.
| Magic | Release | What changed |
|---|---|---|
| 3550 | 3.13a1 | Plugin optimizer support |
| 3551 | 3.13a1 | Compact superinstructions |
| 3552 | 3.13a1 | Remove LOAD_FAST__LOAD_CONST and LOAD_CONST__LOAD_FAST |
| 3553 | 3.13a1 | Add SET_FUNCTION_ATTRIBUTE |
| 3554 | 3.13a1 | More efficient bytecodes for f-strings |
| 3555 | 3.13a1 | Generate specialised opcode metadata from bytecodes.c |
| 3556 | 3.13a1 | Convert LOAD_CLOSURE to a pseudo-instruction |
| 3557 | 3.13a1 | Make the conversion to boolean in jumps explicit |
| 3558 | 3.13a1 | Reorder the stack items for CALL |
| 3559 | 3.13a1 | Generate opcode numbers from bytecodes.c |
| 3560 | 3.13a1 | Add RESUME_CHECK |
| 3561 | 3.13a1 | Add a cache entry to branch instructions |
| 3562 | 3.13a1 | Assign numbers for internal instructions in a separate range |
| 3563 | 3.13a1 | Add CALL_KW, remove KW_NAMES |
| 3564 | 3.13a1 | Remove YIELD_VALUE’s argument, change RESUME’s argument values |
| 3565 | 3.13a1 | YIELD_VALUE’s argument says whether it is in a yield-from |
| 3566 | 3.13a1 | Emit JUMP_NO_INTERRUPT instead of JUMP for non-loop cases without a line number |
| 3567 | 3.13a1 | Reimplement line number propagation by the compiler |
| 3568 | 3.13a1 | Change the meaning of END_FOR |
| 3569 | 3.13a5 | Specialise CONTAINS_OP |
| 3570 | 3.13a6 | Add the __firstlineno__ class attribute |
| 3571 | 3.13b1, final | Fix the miscompilation of private names in generic classes |
Opcode changes in the pre-releases
The explorer reads a file with a pre-release magic number using the instruction table of
that very number. Each table was read from the opcode module of a real CPython
built from the git commit that first set the number (a commit is at
https://github.com/python/cpython/commit/<hash>). The build then compiled
60 of its own library modules, and the reader found the same code objects, instructions,
arguments, line numbers and exception tables as that build’s own dis, with no
differences, for every number in the table except 3571 (below). The table is the one at the
commit that set the number; a commit between it and the next can still differ. The first row
is against 3531, the final 3.12 table. The number 3553 is in CPython’s comment list
(SET_FUNCTION_ATTRIBUTE) but no commit set it: the commit that added that
instruction set 3554, a fix then set 3552, and the f-string commit set 3554 again, so no file
has 3553. The table of 3552 below already has SET_FUNCTION_ATTRIBUTE.
| Magic | Change in the instruction table, against the number before | Commit |
|---|---|---|
| 3550 | Adds ENTER_EXECUTOR (opcode 230); inline cache for JUMP_BACKWARD | 4bfa01b9d91 |
| 3551 | Adds LOAD_FAST_LOAD_FAST, STORE_FAST_LOAD_FAST, STORE_FAST_STORE_FAST | 06893403668 |
| 3552 | Adds SET_FUNCTION_ATTRIBUTE; MAKE_FUNCTION moves from 132 to 24 and loses its argument | c3d2d64b4c5 |
| 3554 | Adds FORMAT_SIMPLE, FORMAT_WITH_SPEC, CONVERT_VALUE; removes FORMAT_VALUE | 1d857da7f0e |
| 3555 | No change to the opcode table (the specialisation metadata is generated from bytecodes.c) | 33f0a8578b7 |
| 3556 | Adds EXIT_INIT_CHECK; removes LOAD_CLOSURE (a pseudo-instruction from here) | 8bff940ad69 |
| 3557 | Adds TO_BOOL with a 3-entry cache; COMPARE_OP’s argument is shifted left 5 and bit 4 means “convert to bool” | 7b2d94d8751 |
| 3558 | No change to the opcode table (the stack order of CALL changes) | a9caf9cf904 |
| 3559 | All opcode numbers are generated from bytecodes.c: instructions without an argument first, then those with one; 117 numbers change | 665a4391e10 |
| 3560 | 84 numbers change | 3b2ecbc1275 |
| 3561 | Inline caches for the four POP_JUMP_IF_* | bcce5e27181 |
| 3562 | Internal opcodes get their own number range: 115 numbers change; RESUME becomes 149 and the first opcode with an argument is no longer at 90 | 8b55adfa8ff |
| 3563 | Adds CALL_KW; removes KW_NAMES; 22 numbers change | 22e65eecaad |
| 3564 | YIELD_VALUE loses its argument; 74 numbers change | 52cc4af6ae9 |
| 3565 | YIELD_VALUE has an argument again (a flag); 74 numbers change | d49aba5a7a3 |
| 3566 | No change to the opcode table | d36a3651189 |
| 3567 | No change to the opcode table | 7e49f27b41d |
| 3568 | No change to the opcode table (END_FOR pops one value) | 981d172f7f0 |
| 3569 | Inline cache for CONTAINS_OP | 7114cf20c01 |
| 3570 | No change to the opcode table | 153b3f75306 |
| 3571 | The final 3.13 table, from the 3.13 branch (3.13b1 on). Not built: see below | the 3.13 branch |
Other changes the table does not show
- Arguments.
MAKE_FUNCTION’s argument is a set of flags until 3551;COMPARE_OP’s argument is shifted left 4 up to 3556 (from 3515) and left 5 from 3557.YIELD_VALUEandRESUMEarguments change at 3564 and 3565 (from the commit titles and CPython’s comments; the build’sdisprints no text for them, so only their presence was compared). - The number 3571 is two bytecode sets. The first commit on CPython’s main
branch with 3571 (
98e855fcc1f, 22 May 2024, after the 3.13 branch was cut) addedLOAD_COMMON_CONSTANTand removedLOAD_ASSERTION_ERROR, and main moved to 3600 two days later. Python 3.13’s own 3571 (3.13b1 and every final release) is read with the final 3.13 table, which was checked against real 3.13 interpreters. A file made by a main-branch build in those two days would be misread; nothing in the file tells them apart.