From c0fac96c29a3842d9370e4c954ae1f4d57f849d0 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 27 Jun 2003 22:25:03 +0000 Subject: SF patch #756996: Bare except in ZipFile.testzip() (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 | 21 +++++++++++++++++++++ Lib/zipfile.py | 2 +- Misc/NEWS | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 57f9a89..d21a9eb 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -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 diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 207fc34..b1943c1 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index ab04a40..08ca40f 100644 --- 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. -- cgit v0.12