From d8c3fd14725db3e7b698fa2bdf25bd98fb87b925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Mon, 20 May 2019 14:25:09 +0200 Subject: [PATCH 1/3] bpo-26185: Fix repr() on empty ZipInfo object It was failing on AttributeError due to inexistant but required attributes file_size and compress_size. They are now initialized to 0 in ZipInfo.__init__(). --- Lib/test/test_zipfile.py | 27 +++++++++++++++++++ Lib/zipfile.py | 4 +-- .../2019-05-20-14-17-29.bpo-26185.pQW4mI.rst | 2 ++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-05-20-14-17-29.bpo-26185.pQW4mI.rst diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index bf5bb4d0f13e3d..c73fe62dde9e81 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -1657,6 +1657,33 @@ def test_create_zipinfo_before_1980(self): self.assertRaises(ValueError, zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0)) + def test_create_empty_zipinfo_repr(self): + """Before bpo-26185, repr() on empty ZipInfo object was failing.""" + zi = zipfile.ZipInfo(filename="empty") + self.assertEqual(repr(zi), "") + + def test_create_empty_zipinfo_default_attributes(self): + """Ensure all required attributes are set.""" + zi = zipfile.ZipInfo() + self.assertEqual(zi.orig_filename, "NoName") + self.assertEqual(zi.filename, "NoName") + self.assertEqual(zi.date_time, (1980, 1, 1, 0, 0, 0)) + self.assertEqual(zi.compress_type, zipfile.ZIP_STORED) + self.assertEqual(zi.comment, b"") + self.assertEqual(zi.extra, b"") + self.assertIn(zi.create_system, (0, 3)) + self.assertEqual(zi.create_version, zipfile.DEFAULT_VERSION) + self.assertEqual(zi.extract_version, zipfile.DEFAULT_VERSION) + self.assertEqual(zi.reserved, 0) + self.assertEqual(zi.flag_bits, 0) + self.assertEqual(zi.volume, 0) + self.assertEqual(zi.internal_attr, 0) + self.assertEqual(zi.external_attr, 0) + + # Before bpo-26185, both were missing + self.assertEqual(zi.file_size, 0) + self.assertEqual(zi.compress_size, 0) + def test_zipfile_with_short_extra_field(self): """If an extra field in the header is less than 4 bytes, skip it.""" zipdata = ( diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 8f8cb863b00343..a47dea95ab42ad 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -375,11 +375,11 @@ def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)): self.volume = 0 # Volume number of file header self.internal_attr = 0 # Internal attributes self.external_attr = 0 # External file attributes + self.compress_size = 0 # Size of the compressed file + self.file_size = 0 # Size of the uncompressed file # Other attributes are set by class ZipFile: # header_offset Byte offset to the file header # CRC CRC-32 of the uncompressed file - # compress_size Size of the compressed file - # file_size Size of the uncompressed file def __repr__(self): result = ['<%s filename=%r' % (self.__class__.__name__, self.filename)] diff --git a/Misc/NEWS.d/next/Library/2019-05-20-14-17-29.bpo-26185.pQW4mI.rst b/Misc/NEWS.d/next/Library/2019-05-20-14-17-29.bpo-26185.pQW4mI.rst new file mode 100644 index 00000000000000..dc71dae69246db --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-20-14-17-29.bpo-26185.pQW4mI.rst @@ -0,0 +1,2 @@ +Fix :func:`repr` on empty :class:`ZipInfo` object. Patch by Mickaƫl +Schoentgen. From 7d9f53fb7408690e0fe1ed5c7efbde4c6c706115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Mon, 20 May 2019 15:28:49 +0200 Subject: [PATCH 2/3] Remove useless hasattr() in ZipInfo._open_to_write() --- Lib/zipfile.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index a47dea95ab42ad..337f990fcc0e37 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1564,8 +1564,7 @@ def _open_to_write(self, zinfo, force_zip64=False): "Close the first handle before opening another.") # Sizes and CRC are overwritten with correct data after processing the file - if not hasattr(zinfo, 'file_size'): - zinfo.file_size = 0 + zinfo.file_size = 0 zinfo.compress_size = 0 zinfo.CRC = 0 From 04035431135ca55267e28b703ff1ce8a2500d2a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Mon, 20 May 2019 15:42:31 +0200 Subject: [PATCH 3/3] Completely remove file_size setting in _open_to_write(). --- Lib/zipfile.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 337f990fcc0e37..fe769af4a9f2ea 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1563,8 +1563,7 @@ def _open_to_write(self, zinfo, force_zip64=False): "another write handle open on it. " "Close the first handle before opening another.") - # Sizes and CRC are overwritten with correct data after processing the file - zinfo.file_size = 0 + # Size and CRC are overwritten with correct data after processing the file zinfo.compress_size = 0 zinfo.CRC = 0