[issue10855] wave.Wave_read.close() doesn't release file

Peter Creath report at bugs.python.org
Fri Jan 7 17:23:45 CET 2011


New submission from Peter Creath <pjcreath+python at gmail.com>:

Calling wave.close() fails to release all references to the file passed in via wave.open(filename_or_obj, "rb").  As a result, processing many wave files produces an IOError of too many files open.

This bug is often masked because this dangling reference is collected if the wave object is collected.  However, if the wave object is retained, calling wave_obj.close() won't release the reference, and so the file will never be closed.

There are two solutions:

1) The workaround: the client program can explicitly close the file object it passed to the wave object ("file_obj.close()").

2) The bug fix: the wave module can properly release the extra reference to the file, by setting "self._data_chunk = None" in the close() method.  Explanation:

Trunk code (and 2.7.1, and older):

    def close(self):
        if self._i_opened_the_file:
            self._i_opened_the_file.close()
            self._i_opened_the_file = None
        self._file = None

but note initfp(self, file):
        ...
        self._file = Chunk(file, bigendian = 0)
        ...
                chunk = Chunk(self._file, bigendian = 0)
                ...
                self._data_chunk = chunk
                ...

therefore close needs to add:

        self._data_chunk = None

----------
components: Library (Lib)
messages: 125654
nosy: pjcreath
priority: normal
severity: normal
status: open
title: wave.Wave_read.close() doesn't release file
type: resource usage
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10855>
_______________________________________


More information about the Python-bugs-list mailing list