mirror of
https://github.com/python/cpython.git
synced 2025-10-13 10:23:28 +00:00
Merge issue #16477: Close tarfile internal handlers in case of exception.
Patch by Serhiy Storchaka.
This commit is contained in:
commit
7ecd13da5f
1 changed files with 11 additions and 13 deletions
|
@ -1924,9 +1924,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)
|
||||||
|
@ -2131,7 +2130,7 @@ class TarFile(object):
|
||||||
"""
|
"""
|
||||||
source = self.fileobj
|
source = self.fileobj
|
||||||
source.seek(tarinfo.offset_data)
|
source.seek(tarinfo.offset_data)
|
||||||
target = bltn_open(targetpath, "wb")
|
with bltn_open(targetpath, "wb") as target:
|
||||||
if tarinfo.sparse is not None:
|
if tarinfo.sparse is not None:
|
||||||
for offset, size in tarinfo.sparse:
|
for offset, size in tarinfo.sparse:
|
||||||
target.seek(offset)
|
target.seek(offset)
|
||||||
|
@ -2140,7 +2139,6 @@ class TarFile(object):
|
||||||
copyfileobj(source, target, tarinfo.size)
|
copyfileobj(source, target, tarinfo.size)
|
||||||
target.seek(tarinfo.size)
|
target.seek(tarinfo.size)
|
||||||
target.truncate()
|
target.truncate()
|
||||||
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