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.14 starts at 3600, and the number goes up by one for each change during its development. 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.
| Release | Magic | Bytes |
|---|---|---|
| 3.14.0 and later 3.14 releases | 3627 | 2B 0E 0D 0A |
The numbers of the pre-releases are listed 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’s four bytes read as a little-endian 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 examples are Python 3.14 compiling the same six-byte source,
x = 1 and a line feed.
00000000 2B 0E 0D 0A 00 00 00 00 00 F1 53 65 06 00 00 00
0x6553F100 (1700000000, 14 November 2023), source size 6 bytes00000000 2B 0E 0D 0A 03 00 00 00 1D B2 B6 32 5C A2 82 A7
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.
The tag of Python 3.14 is cpython-314.
| Source | Compiled |
|---|---|
spam.py, no optimisation | __pycache__/spam.cpython-314.pyc |
spam.py with -O | __pycache__/spam.cpython-314.opt-1.pyc |
spam.py with -OO | __pycache__/spam.cpython-314.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 in CPython’s own source,
Include/internal/pycore_magic_number.h. Only 3627 is the number of a
released version. The others come from alphas, betas and release candidates, and the
release shown is the pre-release that introduced them.
| Magic | Release | What changed |
|---|---|---|
| 3600 | 3.14a1 | Add LOAD_COMMON_CONSTANT |
| 3601 | 3.14a1 | Fix the miscompilation of private names in generic classes |
| 3602 | 3.14a1 | Add LOAD_SPECIAL; remove BEFORE_WITH and BEFORE_ASYNC_WITH |
| 3603 | 3.14a1 | Remove BUILD_CONST_KEY_MAP |
| 3604 | 3.14a1 | Do not duplicate the test at the end of while statements |
| 3605 | 3.14a1 | Move ENTER_EXECUTOR to 255 |
| 3606 | 3.14a1 | Specialise CALL_KW |
| 3607 | 3.14a1 | Add the pseudo-instructions JUMP_IF_TRUE and JUMP_IF_FALSE |
| 3608 | 3.14a1 | Add support for slices |
| 3609 | 3.14a2 | Add LOAD_SMALL_INT and LOAD_CONST_IMMORTAL; remove RETURN_CONST |
| 3610 | 3.14a4 | Add the VALUE_WITH_FAKE_GLOBALS format to annotationlib |
| 3611 | 3.14a4 | Add NOT_TAKEN |
| 3612 | 3.14a4 | Add POP_ITER and INSTRUMENTED_POP_ITER |
| 3613 | 3.14a4 | Add LOAD_CONST_MORTAL |
| 3614 | 3.14a5 | Add BINARY_OP_EXTEND |
| 3615 | 3.14a5 | CALL_FUNCTION_EX always takes a kwargs argument |
| 3616 | 3.14a5 | Remove BINARY_SUBSCR and its family, making them BINARY_OPs |
| 3617 | 3.14a6 | Branch monitoring for async for loops |
| 3618 | 3.14a6 | Add an argument to END_ASYNC_FOR |
| 3619 | 3.14a6 | Renumber RESUME from 149 to 128 |
| 3620 | 3.14a6 | Optimise bytecode for all, any and tuple called on a generator expression |
| 3621 | 3.14a7 | Optimise LOAD_FAST instructions into LOAD_FAST_BORROW |
| 3622 | 3.14a7 | Store annotations in different class dict keys |
| 3623 | 3.14a7 | Add BUILD_INTERPOLATION and BUILD_TEMPLATE |
| 3624 | 3.14b1 | Do not optimise LOAD_FAST when the local is killed by DELETE_FAST |
| 3625 | 3.14b3 | Fix handling of instructions that may leave operands on the stack when optimising LOAD_FAST |
| 3626 | 3.14rc2 | Fix missing exception handlers in logical expressions |
| 3627 | 3.14rc3, final | Fix the miscompilation of some module-level annotations |
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>; 3625 to 3627 are on the
3.14 branch). 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. 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 3571 as it was on CPython’s main branch (see the 3.13 header page: it
already had LOAD_COMMON_CONSTANT); against Python 3.13 final, 3600 adds
LOAD_COMMON_CONSTANT and removes LOAD_ASSERTION_ERROR. The number
3610 is in CPython’s comment list (a format of annotationlib) but no commit set
it, so no file has it. 3614 and 3615 were each set, reverted and set again; 3614 is the first
commit, and for 3615 the commit that stayed is used (75b49621578; the first one,
ab61d3f4303, split LOAD_METHOD off LOAD_ATTR again for
three days).
| Magic | Change in the instruction table, against the number before | Commit |
|---|---|---|
| 3600 | No change to the opcode table (see above) | 84be5244de7 |
| 3601 | No change to the opcode table | a9a74da4a0c |
| 3602 | Adds LOAD_SPECIAL; removes BEFORE_ASYNC_WITH, BEFORE_WITH; 114 numbers change | 9cefcc0ee78 |
| 3603 | Removes BUILD_CONST_KEY_MAP; 74 numbers change | 2e14a52cced |
| 3604 | No change to the opcode table | fe23f8ed970 |
| 3605 | ENTER_EXECUTOR moves to 255; 51 numbers change | 7a65439b93d |
| 3606 | Inline cache for CALL_KW (3 entries) | c13e7d98fb8 |
| 3607 | No change to the opcode table | 78aeb38f7d8 |
| 3608 | No change to the opcode table (marshal adds the slice type) | c6127af8685 |
| 3609 | Adds LOAD_SMALL_INT; removes RETURN_CONST; 11 numbers change | faa3272fb8d |
| 3611 | Adds NOT_TAKEN; 88 numbers change | d2f1d917e8b |
| 3612 | Adds POP_ITER; 87 numbers change | f826beca0ce |
| 3613 | No change to the opcode table | ddd959987c5 |
| 3614 | Inline cache of BINARY_OP grows to 5 entries (descr) | 3893a92d956 |
| 3615 | CALL_FUNCTION_EX moves to 4 and loses its argument; 48 numbers change | 75b49621578 |
| 3616 | Removes BINARY_SUBSCR (it is a BINARY_OP); 113 numbers change | a1417b211f0 |
| 3617 | No change to the opcode table | 2a18e80695a |
| 3618 | RESUME moves from 149 to 128 | aeb23273867 |
| 3619 | Adds an argument to END_ASYNC_FOR (a relative jump); 58 numbers change | 89df62c1209 |
| 3620 | LOAD_DEREF counts as a local variable instruction | 2c8f329dc63 |
| 3621 | Adds LOAD_FAST_BORROW, LOAD_FAST_BORROW_LOAD_FAST_BORROW; 33 numbers change | cd69d55f640 |
| 3622 | No change to the opcode table | 07b8d3117fd |
| 3623 | Adds BUILD_TEMPLATE, BUILD_INTERPOLATION; 115 numbers change | 60202609a2c |
| 3624 | No change to the opcode table | 78adb63ee19 |
| 3625 | No change to the opcode table | be2f32e60f3 |
| 3626 | No change to the opcode table | 0ccf244a197 |
| 3627 | No change to the opcode table; the final 3.14 table | 3643a26a01a |
Other changes the table does not show
- Marshal. The slice type arrives with 3608 (
marshal.catc6127af8685). - Arguments.
LOAD_ATTRkeeps its “push NULL or self” bit in 3615 and every other number of the release;YIELD_VALUEandRESUMEare as in 3.13.