]> granicus.if.org Git - python/commitdiff
Do not close external file objects passed to tarfile.open(mode='w:bz2')
authorLars Gustäbel <lars@gustaebel.de>
Tue, 27 May 2008 12:39:23 +0000 (12:39 +0000)
committerLars Gustäbel <lars@gustaebel.de>
Tue, 27 May 2008 12:39:23 +0000 (12:39 +0000)
when the TarFile is closed.

Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index 4991ae1b60daf9498ad0625bd10d5f30e9ab27bf..619921328ae646b72e265d1c79dbd8463c2decc5 100644 (file)
@@ -692,7 +692,6 @@ class _BZ2Proxy(object):
         if self.mode == "w":
             raw = self.bz2obj.flush()
             self.fileobj.write(raw)
-        self.fileobj.close()
 # class _BZ2Proxy
 
 #------------------------
index f0e755e869066a18ca8cda897d04b0fca111c079..f3bc12d24beb22d0c6ee62fd1f00b851ddb35959 100644 (file)
@@ -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|"
 
index 4f1c3c763b902e40935d5d99388654e3dcf69b3c..1decc90e2b361d9bf22c4e210a5360f8a620e8c6 100644 (file)
--- 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.