[Python-checkins] cpython (2.7): Issue #16477: Close tarfile internal handlers in case of exception.

andrew.svetlov python-checkins at python.org
Thu Nov 29 13:22:47 CET 2012


http://hg.python.org/cpython/rev/ea4bdf5a2e69
changeset:   80651:ea4bdf5a2e69
branch:      2.7
parent:      80644:4102c1a02417
user:        Andrew Svetlov <andrew.svetlov at gmail.com>
date:        Thu Nov 29 14:22:26 2012 +0200
summary:
  Issue #16477: Close tarfile internal handlers in case of exception.

Patch by Serhiy Storchaka.

files:
  Lib/tarfile.py |  14 +++++++-------
  1 files changed, 7 insertions(+), 7 deletions(-)


diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1987,9 +1987,8 @@
 
         # Append the tar header and data to the archive.
         if tarinfo.isreg():
-            f = bltn_open(name, "rb")
-            self.addfile(tarinfo, f)
-            f.close()
+            with bltn_open(name, "rb") as f:
+                self.addfile(tarinfo, f)
 
         elif tarinfo.isdir():
             self.addfile(tarinfo)
@@ -2197,10 +2196,11 @@
         """Make a file called targetpath.
         """
         source = self.extractfile(tarinfo)
-        target = bltn_open(targetpath, "wb")
-        copyfileobj(source, target)
-        source.close()
-        target.close()
+        try:
+            with bltn_open(targetpath, "wb") as target:
+                copyfileobj(source, target)
+        finally:
+            source.close()
 
     def makeunknown(self, tarinfo, targetpath):
         """Make a file from a TarInfo object with an unknown type

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list