Bytecode compilation output depends on order of files compiled
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從提供的 compileall.compile_file 重現程式開始,比較先編譯 a.py 再編譯 b.py 與僅編譯 b.py 時產生的 .pyc 雜湊和 unmarshall 後的常數。追蹤位元組碼編譯和 marshal 輸出路徑,以確定為什麼編譯順序會改變參照計數。完成標準是:等效原始碼無論編譯順序為何,都能產生具決定性的 .pyc 輸出,並且針對回報的案例具有回歸測試涵蓋率。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- compilers
- Issue 類型
- 缺陷
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100