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 摘要。