]> granicus.if.org Git - python/commitdiff
SF patch #756996: Bare except in ZipFile.testzip()
authorRaymond Hettinger <python@rcn.com>
Fri, 27 Jun 2003 22:25:03 +0000 (22:25 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 27 Jun 2003 22:25:03 +0000 (22:25 +0000)
(Contributed by Steven Taschuk)

Replaces a bare except that caused all errors to be mis-reported as
archive errors.

Added a related NEWS item.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS

index 57f9a8972a679ab18b24492b79edfb641e6f7ca4..d21a9eba8cbd2dd27ac6c20119323a3135b43076 100644 (file)
@@ -76,3 +76,24 @@ except IOError:
 else:
     raise TestFailed("expected creation of readable ZipFile without\n"
                      "  a file to raise an IOError.")
+
+
+# Verify that testzip() doesn't swallow inappropriate exceptions.
+data = StringIO.StringIO()
+zipf = zipfile.ZipFile(data, mode="w")
+zipf.writestr("foo.txt", "O, for a Muse of Fire!")
+zipf.close()
+zipf = zipfile.ZipFile(data, mode="r")
+zipf.close()
+try:
+    zipf.testzip()
+except RuntimeError:
+    # This is correct; calling .read on a closed ZipFile should throw
+    # a RuntimeError, and so should calling .testzip.  An earlier
+    # version of .testzip would swallow this exception (and any other)
+    # and report that the first file in the archive was corrupt.
+    pass
+else:
+    raise TestFailed("expected calling .testzip on a closed ZipFile"
+                     " to raise a RuntimeError")
+del data, zipf
index 207fc34262b03853b49cf1da4b05307aaaa1ded8..b1943c17a920e3cbe451ca9e6948c16b17b68ed9 100644 (file)
@@ -327,7 +327,7 @@ class ZipFile:
         for zinfo in self.filelist:
             try:
                 self.read(zinfo.filename)       # Check CRC-32
-            except:
+            except BadZipfile:
                 return zinfo.filename
 
     def getinfo(self, name):
index ab04a4072795fef4569693a87d4c000924ce663f..08ca40ffa32125218672bf35de16c2e79b4dcad8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -83,6 +83,10 @@ Extension modules
 Library
 -------
 
+- ZipFile.testzip() now only traps BadZipfile exceptions.  Previously,
+  a bare except caught to much and reported all errors as a problem
+  in the archive.
+
 - The logging module now has a new function, makeLogRecord() making
   LogHandler easier to interact with DatagramHandler and SocketHandler.