python / python/cpython

ZipFile.mkdir(ZipInfo.from_file(...)) CRC AttributeError

未关闭
#119,052 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

Adding a directory entry to a zip file on the basis of a ZipInfo object (to pick up metadata) fails because ZipFile evidently expects the object to have a CRC attribute even though it's a directory rather than a file. I can work around this bug by storing the ZipInfo in a variable and giving it a nominal CRC (namely 0), but that shouldn't be necessary. Specifically,

import os
import zipfile
with zipfile.ZipFile("bug.zip", mode = "w") as z:
    os.chdir('/')
    z.mkdir(zipfile.ZipInfo.from_file('usr'))

fails:

  File ".../zipfile-bug.py", line 6, in <module>
    z.mkdir(zipfile.ZipInfo.from_file('usr'))
  File "/usr/lib/python3.12/zipfile/__init__.py", line 1935, in mkdir
    self.fp.write(zinfo.FileHeader(False))
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/zipfile/__init__.py", line 460, in FileHeader
    CRC = self.CRC
          ^^^^^^^^
AttributeError: 'ZipInfo' object has no attribute 'CRC'

Could you please take a look?

Thanks!

CPython versions tested on:

3.11, 3.12, 3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-119108

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/zipfile/init.py 开始,重点查看 ZipFile.mkdir 和 ZipInfo.FileHeader,并复现所提供的 ZipInfo.from_file('usr') 示例。检查 zipfile 中针对目录条目的测试,并为此调用添加覆盖;当示例在没有 AttributeError 的情况下完成且相关测试通过时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。