Skip to content

RustPython 3.13 uses CPython 3.12 magic number 3531 #6555

@rocky

Description

@rocky

Feature

RustPython as of Dec 26, 2025 uses CPython magic number 3531 when it should not.

$ cargo run --  -V

Python 3.13.0alpha (heads/main:27ab62de4, Dec 26 2025, 11:16:09)  [RustPython 0.4.0 with rustc 1.90.0 (1159e78c4 2025-09-14)]

$ cargo run --  -

>>>>> from importlib.util import MAGIC_NUMBER
>>>>> MAGIC_NUMBER
b'\xcb\r\r\n'
>>>>> import struct
>>>>> struct.unpack("<Hcc", b'\xcb\r\r\n')[0]
3531

This is also reflected in the magic number written in RustPython's bytecode file:

>>>>> import py_compile
>>>>> py_compile.compile("hello.py", "hello.pyc")
'hello.pyc'
00000000: cb0d 0d0a 0000 0000 3900 5069 1700 0000  ........9.Pi....
00000010: 6305 0000 0041 003c 0011 014d 0050 0105  c....A.<...M.P..
00000020: 0000 0001 0000 0000 0000 0001 0000 0006  ................
00000030: 0000 0001 0000 0006 0000 0001 0000 0006  ................
00000040: 0000 0001 0000 0006 0000 0001 0000 0000  ................
00000050: 0000 0000 0000 0000 0008 0000 0068 656c  .............hel
00000060: 6c6f 2e70 7901 0000 0002 0000 0008 0000  lo.py...........
00000070: 003c 6d6f 6475 6c65 3e08 0000 003c 6d6f  .<module>....<mo
00000080: 6475 6c65 3e00 0000 0002 0000 0075 0d00  dule>........u..
00000090: 0000 4865 6c6c 6f2c 2057 6f72 6c64 214e  ..Hello, World!N
000000a0: 0100 0000 0500 0000 7072 696e 7400 0000  ........print...
000000b0: 0000 0000 0000 0000 0004 0000 0080 0083  ................
000000c0: 6000 0000 00  

The magic number at the beginning of a file is not just a Python convention, but a POSIX file system convention:

$ file hello.pyc
hello.pyc: Byte-compiled Python module for CPython 3.12 or newer, timestamp-based, .py timestamp: Sat Dec 27 15:50:17 2025 UTC, .py size: 23 bytes

This is wrong. This file does not run on CPython 3.12; it is neither CPython nor 3.12.

I came across this in extending the cross-version Python interpreter xdis to handle RustPython bytecode.

Python Documentation or reference to CPython source code

From Python's PEP 3147 describing Magic numbers:

The magic number changes whenever Python changes the byte code format, e.g. by adding new byte codes to its virtual machine. This ensures that pyc files built for previous versions of the VM won’t cause problems.

This implies that if RustPython's 3.13 bytecode format is different from CPython's 3.12 bytecode format, it should not use the same magic number value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions