Nathaniel Smith <n...@pobox.com> added the comment: > I attempted to implement my weakref idea (i.e. raw file keeps a weakref to > the buffered file, calls flush before the raw file gets closed). That > doesn't work either because the GC clears the weakref before calling __del__.
This may be a bit of a left-field or too-big-a-hammer suggestion, but as far I can tell from this thread [1] it probably is technically possible to modify the GC to clear weakrefs after calling __del__. Nick wasn't a fan (he likes the invariant that weakrefs can't trigger a resurrection), but if all else fails it might be worth re-raising. You could add a secondary reference count on FileIO recording how many BufferedIO wrappers there are around it; then it's __del__ would skip calling close() if there are still BufferedIO wrappers, and BufferedIO.__del__ would decrement the reference count and close the underlying file if it hits zero and FileIO.__del__ had already been called. [1] https://mail.python.org/pipermail/python-dev/2016-October/146747.html ---------- nosy: +njs _______________________________________ 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