Issue #16477: Close tarfile internal handlers in case of exception.

Patch by Serhiy Storchaka.
This commit is contained in:
Andrew Svetlov 2012-11-29 14:20:47 +02:00
parent 4927633ab6
commit 718df1d638
1 changed files with 11 additions and 13 deletions

View File

@ -2077,9 +2077,8 @@ class TarFile(object):
# Append the tar header and data to the archive.
if tarinfo.isreg():
f = bltn_open(name, "rb")
with bltn_open(name, "rb") as f:
self.addfile(tarinfo, f)
f.close()
elif tarinfo.isdir():
self.addfile(tarinfo)
@ -2292,7 +2291,7 @@ class TarFile(object):
"""
source = self.fileobj
source.seek(tarinfo.offset_data)
target = bltn_open(targetpath, "wb")
with bltn_open(targetpath, "wb") as target:
if tarinfo.sparse is not None:
for offset, size in tarinfo.sparse:
target.seek(offset)
@ -2301,7 +2300,6 @@ class TarFile(object):
copyfileobj(source, target, tarinfo.size)
target.seek(tarinfo.size)
target.truncate()
target.close()
def makeunknown(self, tarinfo, targetpath):
"""Make a file from a TarInfo object with an unknown type