Title: zipfile.ZipInfo slots can raise unexpected AttributeError
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.6, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Matthew Zipay, alanmcintyre, dino.viehland, serhiy.storchaka, takluyver, twouters
Priority: normal Keywords: patch

Created on 2016-01-23 10:41 by Matthew Zipay, last changed 2019-09-09 13:08 by dino.viehland.

File name Uploaded Description Edit
zipfile.ZipInfo.patch Matthew Zipay, 2016-01-23 10:41 patch of Lib/ and Lib/test/ review
Pull Requests
URL Status Linked Edit
PR 13441 merged Tiger-222, 2019-05-20 12:49
Messages (2)
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/", 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)
Date User Action Args
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