[Python-checkins] cpython (merge 3.2 -> 3.2): Merge with cpython.

georg.brandl python-checkins at python.org
Sun Sep 4 08:41:51 CEST 2011


http://hg.python.org/cpython/rev/a600d84d815b
changeset:   72222:a600d84d815b
branch:      3.2
parent:      72221:33392e7372c8
parent:      71855:843cd43206b4
user:        Georg Brandl <georg at python.org>
date:        Sat Aug 13 11:54:33 2011 +0200
summary:
  Merge with cpython.

files:
  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
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1804,11 +1804,13 @@
             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
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1682,6 +1682,14 @@
 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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -44,6 +44,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list