[Python-checkins] r74245 - in python/trunk: Lib/test/test_zipfile.py Lib/zipfile.py Misc/NEWS

amaury.forgeotdarc python-checkins at python.org
Wed Jul 29 00:15:30 CEST 2009


Author: amaury.forgeotdarc
Date: Wed Jul 29 00:15:30 2009
New Revision: 74245

Log:
#6511: ZipFile will now raise BadZipfile when opening an empty or tiny file,
like it does for larger invalid files.


Modified:
   python/trunk/Lib/test/test_zipfile.py
   python/trunk/Lib/zipfile.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/test/test_zipfile.py
==============================================================================
--- python/trunk/Lib/test/test_zipfile.py	(original)
+++ python/trunk/Lib/test/test_zipfile.py	Wed Jul 29 00:15:30 2009
@@ -703,6 +703,16 @@
         # 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 = StringIO()

Modified: python/trunk/Lib/zipfile.py
==============================================================================
--- python/trunk/Lib/zipfile.py	(original)
+++ python/trunk/Lib/zipfile.py	Wed Jul 29 00:15:30 2009
@@ -198,7 +198,10 @@
     # 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:] == "\000\000":
         # the signature is correct and there's no comment, unpack structure

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Jul 29 00:15:30 2009
@@ -354,6 +354,9 @@
 Library
 -------
 
+- Issue #6511: ZipFile now raises BadZipfile (instead of an IOError) when
+  opening an empty or very small file.
+
 - Issue #6553: Fixed a crash in cPickle.load(), when given a file-like object
   containing incomplete data.
 


More information about the Python-checkins mailing list