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

................
  r76383 | lars.gustaebel | 2009-11-18 21:29:25 +0100 (Wed, 18 Nov 2009) | 10 lines

  Merged revisions 76381 via svnmerge from
  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 3a3d2c925d710464733df890ad6928332ccaa8cd..8eb80f88ee9b78b6c308cbd780bcc34eb97ca194 100644 (file)
@@ -1554,27 +1554,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(self.fileobj.tell() - BLOCKSIZE)
-                    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(self.fileobj.tell() - BLOCKSIZE)
+                        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
 
     #--------------------------------------------------------------------------
     # Below are the classmethods which act as alternate constructors to the
index 07122361fb5afa06f836de39f3331431fe8f478e..f7c24d021a40a789b77a916769022aa7c3c551ca 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,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.