Arusekk <arek_...@o2.pl> added the comment:

Since the issue seems to have been active lately, may I suggest my view on 
solving it.

One solution that comes to my mind is to keep a weak reference to the file, and 
to register an atexit function per file (and to unregister it when the file is 
closed). Example concept-illustrating python code for the _pyio module:

import atexit, weakref

# ...

class TextIOWrapper(TextIOBase):
    def __init__(self):
        # ...
        self._weakclose = operator.methodcaller('close')
        atexit.register(self._weakclose, weakref.proxy(self))

    # ...

    def close(self):
        atexit.unregister(self._weakclose)
        # and now actually close the file

There is a possibility of a negative impact arising from the use of 
operator.methodcaller, because it may return something cached in future 
versions of python. There is also the issue of unregistering an atexit function 
during atexit processing. But the general idea seems to be simple and worth 
considering.

----------
nosy: +Arusekk

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

Reply via email to