]> granicus.if.org Git - python/commit
bpo-17852: Maintain a list of BufferedWriter objects. Flush them on exit. (#3372)
authorNeil Schemenauer <nas-github@arctrix.com>
Fri, 22 Sep 2017 17:17:30 +0000 (10:17 -0700)
committerGitHub <noreply@github.com>
Fri, 22 Sep 2017 17:17:30 +0000 (10:17 -0700)
commit0a1ff24acfc15d8c7f2dc41000a6f3d9a31e7480
tree6589151fac1f3544649dbd9a071ac9e302062c6f
parentda9b4cfb488119f2493a762fcb1d85c58494f51d
bpo-17852: Maintain a list of BufferedWriter objects.  Flush them on exit. (#3372)

* Maintain a list of BufferedWriter objects.  Flush them on exit.

In Python 3, the buffer and the underlying file object are separate
and so the order in which objects are finalized matters.  This is
unlike Python 2 where the file and buffer were a single object and
finalization was done for both at the same time.  In Python 3, if
the file is finalized and closed before the buffer then the data in
the buffer is lost.

This change adds a doubly linked list of open file buffers.  An atexit
hook ensures they are flushed before proceeding with interpreter
shutdown.  This is addition does not remove the need to properly close
files as there are other reasons why buffered data could get lost during
finalization.

Initial patch by Armin Rigo.

* Use weakref.WeakSet instead of WeakKeyDictionary.

* Simplify buffered double-linked list types.

* In _flush_all_writers(), suppress errors from flush().

* Remove NEWS entry, use blurb.

* Take more care when flushing file buffers from atexit.

The previous implementation was not careful enough to avoid
causing issues in multi-threaded cases.  Check for buf->ok
and buf->finalizing before actually doing the flush.  Also,
increase the refcnt to ensure the object does not disappear.
Lib/_pyio.py
Misc/NEWS.d/next/Core and Builtins/2017-09-04-12-46-25.bpo-17852.OxAtCg.rst [new file with mode: 0644]
Modules/_io/_iomodule.c
Modules/_io/_iomodule.h
Modules/_io/bufferedio.c