python / python/cpython

compression.zstd produces a broken archive if nothing is written

未關閉
#155,286 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.15 3.16 extension-modules type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

Creating an empty zstd archive without any .write() calls produces a zero-byte file that fails to open later.

import compression.zstd as zstd
with zstd.open("test.zst", "w") as f:
    pass

with zstd.open("test.zst", "r") as f:
    f.read() # EOFError here. Won't happen if you just pass instead of reading

Traceback (most recent call last):
  File "<python-input-4>", line 2, in <module>
    f.read()
    ~~~~~~^^
  File "compression\zstd\_zstdfile.py", line 173, in read
  File "compression\_common\_streams.py", line 118, in readall
  File "compression\_common\_streams.py", line 99, in read
EOFError: Compressed file ended before the end-of-stream marker was reached

Reference Zstandard v.1.5.7 also won't open the resulting empty file:

zstd.exe -d test.zst -o aa.txt
zstd: test.zst: unexpected end of file

stat test.zst
  File: test.zst
  Size: 0               Blocks: 0          IO Block: 65536  regular empty file

Under same circumstances compression.gzip produces a proper non-empty gzip file that can decompresses into an empty string, which would probably be a desirable behaviour. In the current compression.zstd, you can have that by calling write(b"") instead of pass.

Tested on Windows with recent 3.15rc and 3.14 builds as well as on Linux with 3.10 via backports.zstd.

Python 3.15.0rc1 (tags/v3.15.0rc1:37e98da, Aug  4 2026, 11:37:18) [MSC v.1951 64 bit (AMD64)] on win32
Python 3.14.7 (tags/v3.14.7:823f032, Aug  5 2026, 10:51:32) [MSC v.1944 64 bit (AMD64)] on win32
Python 3.10.12 (main, Jun 22 2026, 18:55:27) [GCC 11.4.0] on linux  

Devguide suggested tagging @emmatyping for .zstd bugs.

CPython versions tested on:

3.15, 3.14, 3.10

Operating systems tested on:

Windows, Linux

Linked PRs
  • gh-155342

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 compression/zstd/_zstdfile.py 以及 compression/_common_streams.py 中所示的讀取路徑開始。重現空的內容管理器情況,然後將其與空寫入情況以及 gzip 的行為進行比較。當未呼叫 write() 所建立的封存檔可以重新開啟並以空檔案讀取,且已新增回歸測試涵蓋時,即表示完成;連結的 PR gh-155342 表示相關工作已經在進行中。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。