summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorŁukasz Langa <lukasz@langa.pl>2010-11-22 23:31:26 (GMT)
committerŁukasz Langa <lukasz@langa.pl>2010-11-22 23:31:26 (GMT)
commite94980a64fe8db4170482f14eb165b46def733c8 (patch)
tree7e9dabdeb510dc03509b8649151d0debc8079a9c
parent022f049fededad8a2e8e902effd42eca19fbf2b1 (diff)
downloadcpython-e94980a64fe8db4170482f14eb165b46def733c8.zip
cpython-e94980a64fe8db4170482f14eb165b46def733c8.tar.gz
cpython-e94980a64fe8db4170482f14eb165b46def733c8.tar.bz2
Issue #9846: ZipExtFile provides no mechanism for closing the underlying file object
-rw-r--r--Lib/zipfile.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 184f4d2..eec8ece 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -473,9 +473,11 @@ class ZipExtFile(io.BufferedIOBase):
# Search for universal newlines or line chunks.
PATTERN = re.compile(br'^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)')
- def __init__(self, fileobj, mode, zipinfo, decrypter=None):
+ def __init__(self, fileobj, mode, zipinfo, decrypter=None,
+ close_fileobj=False):
self._fileobj = fileobj
self._decrypter = decrypter
+ self._close_fileobj = close_fileobj
self._compress_type = zipinfo.compress_type
self._compress_size = zipinfo.compress_size
@@ -647,6 +649,12 @@ class ZipExtFile(io.BufferedIOBase):
self._offset += len(data)
return data
+ def close(self):
+ try:
+ if self._close_fileobj:
+ self._fileobj.close()
+ finally:
+ super().close()
class ZipFile:
@@ -889,8 +897,10 @@ class ZipFile:
# given a file object in the constructor
if self._filePassed:
zef_file = self.fp
+ should_close = False
else:
zef_file = io.open(self.filename, 'rb')
+ should_close = True
# Make sure we have an info object
if isinstance(name, ZipInfo):
@@ -944,7 +954,7 @@ class ZipFile:
if h[11] != check_byte:
raise RuntimeError("Bad password for file", name)
- return ZipExtFile(zef_file, mode, zinfo, zd)
+ return ZipExtFile(zef_file, mode, zinfo, zd, close_fileobj=should_close)
def extract(self, member, path=None, pwd=None):
"""Extract a member from the archive to the current working directory,