Bytecode compilation output depends on order of files compiled
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
This is minimal reproduction of this downstream bug report: https://github.com/astral-sh/uv/issues/10619
The output of compileall.compile_file depends on the order in which the files are compiled. This means compilation is non-deterministic if builds are distributed over a process pool.
This becomes a problem when building docker images, where you usually bytecode compile ahead of time for faster startup, and where the hash of the image depends on all files in the image, including the .pyc files.
Specifically, the output of
a = {"foo", 2, 3}
def f():
b = {"foo", 2, 3}
is different if we previously compiled another file with
import foo
Reproducer script:
#!/bin/bash
set -e
script=$(cat << EOF
import compileall
import sys
for path in sys.argv[1:]:
compileall.compile_file(path)
EOF
)
cat << EOF > a.py
import foo
EOF
cat << EOF > b.py
a = {"foo", 2, 3}
def f():
b = {"foo", 2, 3}
EOF
# Both files
rm -rf __pycache__
python3.14 -c "$script" a.py b.py
sha256sum __pycache__/b.cpython-314.pyc
# For debugging
cp __pycache__/b.cpython-314.pyc b1.cpython-314.pyc
# Single file only
rm -rf __pycache__
python3.14 -c "$script" b.py
sha256sum __pycache__/b.cpython-314.pyc
# For debugging
cp __pycache__/b.cpython-314.pyc b2.cpython-314.pyc
This is caused be different refcounts in the marshalled files:
import marshal
import sys
with open("b1.cpython-313.pyc", "rb") as f:
f.read(16) # Skip header
pyc1 = marshal.load(f)
with open("b2.cpython-313.pyc", "rb") as f:
f.read(16) # Skip header
pyc2 = marshal.load(f)
print(sys.getrefcount(pyc1.co_consts[0]))
print(sys.getrefcount(pyc2.co_consts[0]))
This prints 2 and 3.
The original report is from 3.13, i've reproduced it with 3.14.0a4. It happens at least on linux and windows.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-156862
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided compileall.compile_file reproducer and compare the generated .pyc hashes and unmarshalled constants when compiling a.py before b.py versus b.py alone. Trace the bytecode compilation and marshal output paths to determine why compilation order changes refcounts. Done means equivalent source produces deterministic .pyc output regardless of compilation order, with regression coverage for the reported cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100