Commit ac26a2e7 authored by Andrew Svetlov's avatar Andrew Svetlov

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

Patch by Serhiy Storchaka.
parent 43acbf1f
......@@ -1987,9 +1987,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)
......@@ -2197,10 +2196,11 @@ class TarFile(object):
"""Make a file called targetpath.
"""
source = self.extractfile(tarinfo)
target = bltn_open(targetpath, "wb")
try:
with bltn_open(targetpath, "wb") as target:
copyfileobj(source, target)
finally:
source.close()
target.close()
def makeunknown(self, tarinfo, targetpath):
"""Make a file from a TarInfo object with an unknown type
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment