python / python/cpython

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

Open
#119,052 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Lib/zipfile/init.py, especially ZipFile.mkdir and ZipInfo.FileHeader, and reproduce the supplied ZipInfo.from_file('usr') example. Check the zipfile tests for directory entries and add coverage for this call; done means the example completes without AttributeError and the relevant tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.