[Python-checkins] cpython (2.7): Closes #16183: ZipExtFile object close without file handle closed (backporting

jesus.cea python-checkins at python.org
Sun Nov 4 02:33:45 CET 2012


http://hg.python.org/cpython/rev/6a14f692df1c
changeset:   80231:6a14f692df1c
branch:      2.7
parent:      80229:6c639a1ff53d
user:        Jesus Cea <jcea at jcea.es>
date:        Sun Nov 04 02:32:08 2012 +0100
summary:
  Closes #16183: ZipExtFile object close without file handle closed (backporting of Issue #9846)

files:
  Lib/zipfile.py |  15 +++++++++++++--
  1 files changed, 13 insertions(+), 2 deletions(-)


diff --git a/Lib/zipfile.py b/Lib/zipfile.py
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -475,9 +475,11 @@
     # Search for universal newlines or line chunks.
     PATTERN = re.compile(r'^(?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
@@ -649,6 +651,12 @@
         self._offset += len(data)
         return data
 
+    def close(self):
+        try :
+            if self._close_fileobj:
+                self._fileobj.close()
+        finally:
+            super(ZipExtFile, self).close()
 
 
 class ZipFile(object):
@@ -896,8 +904,10 @@
         # given a file object in the constructor
         if self._filePassed:
             zef_file = self.fp
+            should_close = False
         else:
             zef_file = open(self.filename, 'rb')
+            should_close = True
 
         # Make sure we have an info object
         if isinstance(name, ZipInfo):
@@ -951,7 +961,8 @@
             if ord(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,

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list