summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_zipfile.py10
-rw-r--r--Lib/zipfile.py5
-rw-r--r--Misc/NEWS5
3 files changed, 18 insertions, 2 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index d4bb4d2c75..b17f857778 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -701,6 +701,16 @@ class OtherTests(unittest.TestCase):
# quickly.
self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
+ def test_empty_file_raises_BadZipFile(self):
+ f = open(TESTFN, 'w')
+ f.close()
+ self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
+
+ f = open(TESTFN, 'w')
+ f.write("short file")
+ f.close()
+ self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
+
def test_closed_zip_raises_RuntimeError(self):
# Verify that testzip() doesn't swallow inappropriate exceptions.
data = io.BytesIO()
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index de06d820bd..0cfa37e881 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -200,7 +200,10 @@ def _EndRecData(fpin):
# Check to see if this is ZIP file with no archive comment (the
# "end of central directory" structure should be the last item in the
# file if this is the case).
- fpin.seek(-sizeEndCentDir, 2)
+ try:
+ fpin.seek(-sizeEndCentDir, 2)
+ except IOError:
+ return None
data = fpin.read()
if data[0:4] == stringEndArchive and data[-2:] == b"\000\000":
# the signature is correct and there's no comment, unpack structure
diff --git a/Misc/NEWS b/Misc/NEWS
index aa4cfd906d..a60f3b23e1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -956,7 +956,10 @@ Core and Builtins
Library
-------
-- Issue #6545: Removed assert statements in distutils.Extension, so the
+- Issue #6511: ZipFile now raises BadZipfile (instead of an IOError) when
+ opening an empty or very small file.
+
+- Issue #6545: Removed assert statements in distutils.Extension, so the
behavior is similar when used with -O.
- unittest has been split up into a package. All old names should still work.