]> granicus.if.org Git - python/commitdiff
Merged revisions 76381 via svnmerge from
authorLars Gustäbel <lars@gustaebel.de>
Wed, 18 Nov 2009 21:09:35 +0000 (21:09 +0000)
committerLars Gustäbel <lars@gustaebel.de>
Wed, 18 Nov 2009 21:09:35 +0000 (21:09 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r76381 | lars.gustaebel | 2009-11-18 21:24:54 +0100 (Wed, 18 Nov 2009) | 3 lines

  Issue #7341: Close the internal file object in the TarFile
  constructor in case of an error.
........

Lib/tarfile.py
Misc/NEWS

index 687ef814845233701455480299737a6bd72b353b..7643a0bc647367f398c2a1b1a4f1eb243564c915 100644 (file)
@@ -1565,27 +1565,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 aecf5ca589371c20669ed1ae165c0da944be1e3f..c75f3699813d909c0d75974a5c97b9fec342e855 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7341: Close the internal file object in the TarFile constructor in
+  case of an error.
+
 - Issue #7328: pydoc no longer corrupts sys.path when run with the '-m' switch
 
 - Issue #7318: multiprocessing now uses a timeout when it fails to establish