[pypy-issue] [issue912] ZipFile.read() leaks file handles

Tyler Kennedy tracker at bugs.pypy.org
Tue Oct 18 11:38:56 CEST 2011

New submission from Tyler Kennedy <tk at tkte.ch>:

PyPy's implementation of ZipFile will constantly leak file handles if the 
ZipFile was originally opened given a file path instead of a file handle.

For example, this will thrown an OSError on ZipFiles with over 1024 files:

zp = zipfile.ZipFile('/path/to/zip/with/many/files.zip')
self.files = dict((f, zp.read(f)) for f in zp.namelist())

The offending bit of code is zipfile.py line 879 to 882:

# Only open a new file for instances where we were not
# given a file object in the constructor
if self._filePassed:
    zef_file = self.fp
    zef_file = open(self.filename, 'rb')

...in conjunction with:

def read(self, name, pwd=None):
    """Return file bytes (as a string) for name."""
    return self.open(name, "r", pwd).read()

Because read() never closes the handles, and the handles don't get collected 
inside of a loop.

messages: 3326
nosy: TkTech, pypy-issue
priority: bug
release: 1.6
status: unread
title: ZipFile.read() leaks file handles

PyPy bug tracker <tracker at bugs.pypy.org>

More information about the pypy-issue mailing list