classification
Title: zipfile.ZipInfo slots can raise unexpected AttributeError
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Matthew Zipay, Tiger-222, alanmcintyre, dino.viehland, ned.deily, serhiy.storchaka, takluyver, twouters
Priority: normal Keywords: patch

Created on 2016-01-23 10:41 by Matthew Zipay, last changed 2020-08-02 21:21 by ned.deily. This issue is now closed.

Files
File name Uploaded Description Edit
zipfile.ZipInfo.patch Matthew Zipay, 2016-01-23 10:41 patch of Lib/zipfile.py and Lib/test/test_zipfile.py review
Pull Requests
URL Status Linked Edit
PR 13441 merged Tiger-222, 2019-05-20 12:49
Messages (4)
msg258859 - (view) Author: Matthew Zipay (Matthew Zipay) * Date: 2016-01-23 10:41
The zipfile.ZipInfo.__init__ method permits several of ZipInfo's slot attributes to go uninitialized unless the object is obtained from ZipFile.getinfo() or ZipFile.infolist().

As a result, accessing those attributes (header_offset, CRC, compress_size, or file_size) or attempting to repr() a ZipInfo object can fail unexpectedly with AttributeError. (I say "unexpectedly" because ZipInfo.__init__ and its attributes are public/documented, so the attributes ought to be properly initialized regardless of how the object gets created.)

A simple test to illustrate:

>>> import zipfile
>>> zinfo = zipfile.ZipInfo()
>>> repr(zinfo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "********/cpython/Lib/zipfile.py", line 376, in __repr__
    result.append(' file_size=%r' % self.file_size)
AttributeError: file_size

(If you assign zinfo.file_size = None, it next fails on compress_size.)

This problem has been noted before - see issues 3039 and 22217 - but has not been resolved.

Patch including tests is attached.
msg351452 - (view) Author: Dino Viehland (dino.viehland) * (Python committer) Date: 2019-09-09 13:08
New changeset 992347d7376765fe3f4fc958fb1be193ba21f6c3 by Dino Viehland (Mickaël Schoentgen) in branch 'master':
bpo-26185: Fix repr() on empty ZipInfo object (#13441)
https://github.com/python/cpython/commit/992347d7376765fe3f4fc958fb1be193ba21f6c3
msg374694 - (view) Author: Mickaël Schoentgen (Tiger-222) * Date: 2020-08-02 20:07
The ticket could be closed, right? The fix was merged quite some time ago.
msg374697 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-08-02 21:21
Fixed in 3.9.0
History
Date User Action Args
2020-08-02 21:21:02ned.deilysetstatus: open -> closed

versions: + Python 3.9, - Python 3.5, Python 3.6
nosy: + ned.deily

messages: + msg374697
resolution: fixed
stage: patch review -> resolved
2020-08-02 20:07:05Tiger-222setnosy: + Tiger-222
messages: + msg374694
2019-09-09 13:08:58dino.viehlandsetnosy: + dino.viehland
messages: + msg351452
2019-05-20 12:49:57Tiger-222setpull_requests: + pull_request13350
2016-01-30 12:16:39takluyversetnosy: + takluyver
2016-01-23 11:23:01SilentGhostsetstage: patch review
2016-01-23 11:22:46SilentGhostsetnosy: + twouters, alanmcintyre, serhiy.storchaka
2016-01-23 10:41:53Matthew Zipaycreate