[Python-checkins] r76381 - in python/trunk: Lib/tarfile.py Misc/NEWS

lars.gustaebel python-checkins at python.org
Wed Nov 18 21:24:54 CET 2009


Author: lars.gustaebel
Date: Wed Nov 18 21:24:54 2009
New Revision: 76381

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


Modified:
   python/trunk/Lib/tarfile.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/tarfile.py
==============================================================================
--- python/trunk/Lib/tarfile.py	(original)
+++ python/trunk/Lib/tarfile.py	Wed Nov 18 21:24:54 2009
@@ -1557,27 +1557,33 @@
         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

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Nov 18 21:24:54 2009
@@ -440,6 +440,9 @@
 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.
 


More information about the Python-checkins mailing list