Martin Panter added the comment:

One option would be for any wrapper-type class (e.g. BufferedReader, SocketIO) 
to override __del__(). Instead of calling close(), it should call detach() or 
equivalent, and delete the returned reference to the underlying wrapped object 
without explicitly closing it, to allow a warning to be generated. Maybe a 
WrappedIO mixin class could help with this.

I am posting del-detach.patch which roughly implements the above option. It 
only seems to break the test_override_destructor() tests for the 
TextIOWrapperTest and CommonBufferedTests classes, which expect close() to be 
called.

Another more radical option might be to make IOBase.__del__() only call 
flush(), not close(), except for the actual implementations that really should 
emit a warning, like FileIO. I will have a go at doing this option in another 
patch.

Both these options would change the behaviour in some cases that rely on the 
garbage collector to close objects. They would defer the automatic closing 
until all references of the underlying object are garbage collected, instead of 
just when the wrapped object is garbage collected. A third option would be to 
add explicit __del__() methods emitting a warning in each class, along with any 
exception supression needed due to Python interpreter finalization quirks.

----------
keywords: +patch
Added file: http://bugs.python.org/file38134/del-detach.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19829>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to