From b1a54a353021f3d478e1d9e667cef0fa92507c36 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lars=20Gust=C3=A4bel?= Date: Tue, 27 May 2008 12:39:23 +0000 Subject: [PATCH] Do not close external file objects passed to tarfile.open(mode='w:bz2') when the TarFile is closed. --- Lib/tarfile.py | 1 - Lib/test/test_tarfile.py | 16 ++++++++++++++-- Misc/NEWS | 3 +++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 4991ae1b60..619921328a 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -692,7 +692,6 @@ class _BZ2Proxy(object): if self.mode == "w": raw = self.bz2obj.flush() self.fileobj.write(raw) - self.fileobj.close() # class _BZ2Proxy #------------------------ diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index f0e755e869..f3bc12d24b 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -529,7 +529,19 @@ class PaxReadTest(LongnameTest): self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0) -class WriteTest(unittest.TestCase): +class WriteTestBase(unittest.TestCase): + # Put all write tests in here that are supposed to be tested + # in all possible mode combinations. + + def test_fileobj_no_close(self): + fobj = StringIO.StringIO() + tar = tarfile.open(fileobj=fobj, mode=self.mode) + tar.addfile(tarfile.TarInfo("foo")) + tar.close() + self.assert_(fobj.closed is False, "external fileobjs must never closed") + + +class WriteTest(WriteTestBase): mode = "w:" @@ -652,7 +664,7 @@ class WriteTest(unittest.TestCase): shutil.rmtree(tempdir) -class StreamWriteTest(unittest.TestCase): +class StreamWriteTest(WriteTestBase): mode = "w|" diff --git a/Misc/NEWS b/Misc/NEWS index 4f1c3c763b..1decc90e2b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -63,6 +63,9 @@ Extension Modules Library ------- +- Do not close external file objects passed to tarfile.open(mode='w:bz2') + when the TarFile is closed. + - Issue #2959: For consistency with other file-like objects, gzip's GzipFile.close() can now be called multiple times without raising an exception. -- 2.50.0