diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 40d438c45c4..ec8af064982 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1618,7 +1618,7 @@ class TarFile(object): if not extfileobj and fileobj is not None: fileobj.close() raise - t._extfileobj = extfileobj + t._extfileobj = False return t @classmethod diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 72ae31addab..e0c91035bc0 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -857,6 +857,12 @@ class WriteTestBase(TarTest): tar.addfile(tarfile.TarInfo("foo")) tar.close() self.assertFalse(fobj.closed, "external fileobjs must never closed") + # Issue #20238: Incomplete gzip output with mode="w:gz" + data = fobj.getvalue() + del tar + support.gc_collect() + self.assertFalse(fobj.closed) + self.assertEqual(data, fobj.getvalue()) class WriteTest(WriteTestBase, unittest.TestCase): diff --git a/Misc/NEWS b/Misc/NEWS index a4aaafac62a..9a003f50f59 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -25,6 +25,9 @@ Core and Builtins Library ------- +- Issue #20238: TarFile opened with external fileobj and "w:gz" mode didn't + write complete output on close. + - Issue #20245: The open functions in the tarfile module now correctly handle empty mode.