]> granicus.if.org Git - python/commitdiff
Add a testcase that checks if the TarFile constructor successfully
authorLars Gustäbel <lars@gustaebel.de>
Mon, 23 Nov 2009 15:46:19 +0000 (15:46 +0000)
committerLars Gustäbel <lars@gustaebel.de>
Mon, 23 Nov 2009 15:46:19 +0000 (15:46 +0000)
closes the internal file object in case of an error (issue #7341).

Lib/test/test_tarfile.py

index 20339dd0a7bb3845b8f74d6065bb0476b7012531..bc0ae5899dbba9258fee8e0423584c0e28584b7e 100644 (file)
@@ -310,6 +310,24 @@ class MiscReadTest(CommonReadTest):
             self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
         tar.close()
 
+    def test_init_close_fobj(self):
+        # Issue #7341: Close the internal file object in the TarFile
+        # constructor in case of an error. For the test we rely on
+        # the fact that opening an empty file raises a ReadError.
+        empty = os.path.join(TEMPDIR, "empty")
+        open(empty, "wb").write("")
+
+        try:
+            tar = object.__new__(tarfile.TarFile)
+            try:
+                tar.__init__(empty)
+            except tarfile.ReadError:
+                self.assertTrue(tar.fileobj.closed)
+            else:
+                self.fail("ReadError not raised")
+        finally:
+            os.remove(empty)
+
 
 class StreamReadTest(CommonReadTest):