summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2012-11-29 12:22:26 (GMT)
committerAndrew Svetlov <andrew.svetlov@gmail.com>2012-11-29 12:22:26 (GMT)
commitac26a2e736ab58962c49395109d894e798b3714f (patch)
tree191c68accba4109b95175edacf0aa407ee632ccb
parent43acbf1fd67f144ca050195411e773eea16b8de1 (diff)
downloadcpython-ac26a2e736ab58962c49395109d894e798b3714f.zip
cpython-ac26a2e736ab58962c49395109d894e798b3714f.tar.gz
cpython-ac26a2e736ab58962c49395109d894e798b3714f.tar.bz2
Issue #16477: Close tarfile internal handlers in case of exception.
Patch by Serhiy Storchaka.
-rw-r--r--Lib/tarfile.py14
1 files changed, 7 insertions, 7 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 02858ae..89d8cc3 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -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