[issue19829] _pyio.BufferedReader and _pyio.TextIOWrapper destructor don't emit ResourceWarning if the file is not closed

STINNER Victor report at bugs.python.org
Thu Mar 24 10:15:58 EDT 2016


STINNER Victor added the comment:

Attached patch modifies _pyio to mimick better the reference io module:

* Add IOBase._finalizing
* IOBase.__del__() sets _finalizing to True
* Add FileIO._dealloc_warn()
* Remove FileIO.__del__()
* FileIO.close(), _BufferedIOMixin.close() and TextIOWrapper.close() now calls _dealloc_warn() if _finalizing is true
* Override closed() method in FileIO: needed because FileIO.close() now calls super().close() before logging the ResourceWarning, and the file is expected to see open in the warning
* FileIO.close() now calls super().close() *before* closing the file descriptor

I added hasattr(.., '_dealloc_warn') sanity checks because _dealloc_warn() are added to concrete classes, not to base classes.

----------
Added file: http://bugs.python.org/file42271/pyio_res_warn.patch

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


More information about the Python-bugs-list mailing list