]> granicus.if.org Git - python/commitdiff
Issue #7341: Close the internal file object in the TarFile
authorLars Gustäbel <lars@gustaebel.de>
Wed, 18 Nov 2009 20:24:54 +0000 (20:24 +0000)
committerLars Gustäbel <lars@gustaebel.de>
Wed, 18 Nov 2009 20:24:54 +0000 (20:24 +0000)
constructor in case of an error.

Lib/tarfile.py
Misc/NEWS

index a87fe225d17f63c51a9c78b2282b0ca65a84d705..29e12f021674edb3d038cc89e588291942ce9d70 100644 (file)
@@ -1557,27 +1557,33 @@ class TarFile(object):
         self.inodes = {}        # dictionary caching the inodes of
                                 # archive members already added
 
-        if self.mode == "r":
-            self.firstmember = None
-            self.firstmember = self.next()
-
-        if self.mode == "a":
-            # Move to the end of the archive,
-            # before the first empty block.
-            self.firstmember = None
-            while True:
-                if self.next() is None:
-                    if self.offset > 0:
-                        self.fileobj.seek(- BLOCKSIZE, 1)
-                    break
-
-        if self.mode in "aw":
-            self._loaded = True
-
-            if self.pax_headers:
-                buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy())
-                self.fileobj.write(buf)
-                self.offset += len(buf)
+        try:
+            if self.mode == "r":
+                self.firstmember = None
+                self.firstmember = self.next()
+
+            if self.mode == "a":
+                # Move to the end of the archive,
+                # before the first empty block.
+                self.firstmember = None
+                while True:
+                    if self.next() is None:
+                        if self.offset > 0:
+                            self.fileobj.seek(- BLOCKSIZE, 1)
+                        break
+
+            if self.mode in "aw":
+                self._loaded = True
+
+                if self.pax_headers:
+                    buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy())
+                    self.fileobj.write(buf)
+                    self.offset += len(buf)
+        except:
+            if not self._extfileobj:
+                self.fileobj.close()
+            self.closed = True
+            raise
 
     def _getposix(self):
         return self.format == USTAR_FORMAT
index a52e28aad1a6f18d695693b1328cdf446d9f527e..d0ed47ce4fb4d478c9e6f09d6a194f2ae85f6ef8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -440,6 +440,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7341: Close the internal file object in the TarFile constructor in
+  case of an error.
+
 - Issue #7293: distutils.test_msvc9compiler is fixed to work on any fresh
   Windows box. Help provided by David Bolen.