]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #16477: Close tarfile internal handlers in case of exception.
authorAndrew Svetlov <andrew.svetlov@gmail.com>
Thu, 29 Nov 2012 12:22:26 +0000 (14:22 +0200)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Thu, 29 Nov 2012 12:22:26 +0000 (14:22 +0200)
Patch by Serhiy Storchaka.

Lib/tarfile.py

index 02858ae035a19845cf64d193cb26acaaa8e7bff4..89d8cc3ffd5d9122a6d0ab11bbf363b98c723e45 100644 (file)
@@ -1987,9 +1987,8 @@ class TarFile(object):
 
         # 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 @@ class TarFile(object):
         """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