]> granicus.if.org Git - python/commitdiff
Fix #11513: wrong exception handling for the case that GzipFile itself raises an...
authorGeorg Brandl <georg@python.org>
Sat, 13 Aug 2011 09:48:12 +0000 (11:48 +0200)
committerGeorg Brandl <georg@python.org>
Sat, 13 Aug 2011 09:48:12 +0000 (11:48 +0200)
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index 63c7554af20d0d3132cf6dae29ddc87a33f77c9c..3d52a81b9e1b8793f3f97c16831ce2d93057ffc0 100644 (file)
@@ -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
index 8036c5cda138a4486f59fadc8863dd593a9a6f6a..b45f16899017ab6cc279ce36f5da73c98fc4cb4c 100644 (file)
@@ -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"
index 354d09a070c10cbbdd2fade41f78ba83e6d5592c..328ec61c1eacde036f8f78e52dfae1dc8e4640c6 100644 (file)
--- 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.