mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #16477: Close tarfile internal handlers in case of exception.
Patch by Serhiy Storchaka.
This commit is contained in:
parent
43acbf1fd6
commit
ac26a2e736
1 changed files with 7 additions and 7 deletions
|
@ -1987,9 +1987,8 @@ class TarFile(object):
|
||||||
|
|
||||||
# Append the tar header and data to the archive.
|
# Append the tar header and data to the archive.
|
||||||
if tarinfo.isreg():
|
if tarinfo.isreg():
|
||||||
f = bltn_open(name, "rb")
|
with bltn_open(name, "rb") as f:
|
||||||
self.addfile(tarinfo, f)
|
self.addfile(tarinfo, f)
|
||||||
f.close()
|
|
||||||
|
|
||||||
elif tarinfo.isdir():
|
elif tarinfo.isdir():
|
||||||
self.addfile(tarinfo)
|
self.addfile(tarinfo)
|
||||||
|
@ -2197,10 +2196,11 @@ class TarFile(object):
|
||||||
"""Make a file called targetpath.
|
"""Make a file called targetpath.
|
||||||
"""
|
"""
|
||||||
source = self.extractfile(tarinfo)
|
source = self.extractfile(tarinfo)
|
||||||
target = bltn_open(targetpath, "wb")
|
try:
|
||||||
|
with bltn_open(targetpath, "wb") as target:
|
||||||
copyfileobj(source, target)
|
copyfileobj(source, target)
|
||||||
|
finally:
|
||||||
source.close()
|
source.close()
|
||||||
target.close()
|
|
||||||
|
|
||||||
def makeunknown(self, tarinfo, targetpath):
|
def makeunknown(self, tarinfo, targetpath):
|
||||||
"""Make a file from a TarInfo object with an unknown type
|
"""Make a file from a TarInfo object with an unknown type
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue