Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d88e5af

Browse files
committed
Merge with cpython.
2 parents b3f0ce4 + 3abb372 commit d88e5af

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Lib/tarfile.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,11 +1804,13 @@ def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
18041804
fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj)
18051805
t = cls.taropen(name, mode, fileobj, **kwargs)
18061806
except IOError:
1807-
if not extfileobj:
1807+
if not extfileobj and fileobj is not None:
18081808
fileobj.close()
1809+
if fileobj is None:
1810+
raise
18091811
raise ReadError("not a gzip file")
18101812
except:
1811-
if not extfileobj:
1813+
if not extfileobj and fileobj is not None:
18121814
fileobj.close()
18131815
raise
18141816
t._extfileobj = extfileobj

Lib/test/test_tarfile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,14 @@ def test_symlink_extraction2(self):
16821682
class GzipMiscReadTest(MiscReadTest):
16831683
tarname = gzipname
16841684
mode = "r:gz"
1685+
1686+
def test_non_existent_targz_file(self):
1687+
# Test for issue11513: prevent non-existent gzipped tarfiles raising
1688+
# multiple exceptions.
1689+
with self.assertRaisesRegex(IOError, "xxx") as ex:
1690+
tarfile.open("xxx", self.mode)
1691+
self.assertEqual(ex.exception.errno, errno.ENOENT)
1692+
16851693
class GzipUstarReadTest(UstarReadTest):
16861694
tarname = gzipname
16871695
mode = "r:gz"

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ Core and Builtins
4444
Library
4545
-------
4646

47+
- Issue #11513: Fix exception handling ``tarfile.TarFile.gzopen()`` when
48+
the file cannot be opened.
49+
4750
- Issue #12687: Fix a possible buffering bug when unpickling text mode
4851
(protocol 0, mostly) pickles.
4952

0 commit comments

Comments
 (0)