From 3abb372c810c13af4207124c175cfd0a842b5f56 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 13 Aug 2011 11:48:12 +0200 Subject: Fix #11513: wrong exception handling for the case that GzipFile itself raises an IOError. --- Lib/tarfile.py | 6 ++++-- Lib/test/test_tarfile.py | 8 ++++++++ Misc/NEWS | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 63c7554..3d52a81 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1804,11 +1804,13 @@ class TarFile(object): fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj) t = cls.taropen(name, mode, fileobj, **kwargs) except IOError: - if not extfileobj: + if not extfileobj and fileobj is not None: fileobj.close() + if fileobj is None: + raise raise ReadError("not a gzip file") except: - if not extfileobj: + if not extfileobj and fileobj is not None: fileobj.close() raise t._extfileobj = extfileobj diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 8036c5c..b45f168 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -1682,6 +1682,14 @@ class LinkEmulationTest(ReadTest): class GzipMiscReadTest(MiscReadTest): tarname = gzipname mode = "r:gz" + + def test_non_existent_targz_file(self): + # Test for issue11513: prevent non-existent gzipped tarfiles raising + # multiple exceptions. + with self.assertRaisesRegex(IOError, "xxx") as ex: + tarfile.open("xxx", self.mode) + self.assertEqual(ex.exception.errno, errno.ENOENT) + class GzipUstarReadTest(UstarReadTest): tarname = gzipname mode = "r:gz" diff --git a/Misc/NEWS b/Misc/NEWS index 354d09a..328ec61 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -44,6 +44,9 @@ Core and Builtins Library ------- +- Issue #11513: Fix exception handling ``tarfile.TarFile.gzopen()`` when + the file cannot be opened. + - Issue #12687: Fix a possible buffering bug when unpickling text mode (protocol 0, mostly) pickles. -- cgit v0.12