summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNeil Schemenauer <nas-github@arctrix.com>2017-09-22 17:17:30 (GMT)
committerGitHub <noreply@github.com>2017-09-22 17:17:30 (GMT)
commit0a1ff24acfc15d8c7f2dc41000a6f3d9a31e7480 (patch)
tree6589151fac1f3544649dbd9a071ac9e302062c6f /Lib
parentda9b4cfb488119f2493a762fcb1d85c58494f51d (diff)
downloadcpython-0a1ff24acfc15d8c7f2dc41000a6f3d9a31e7480.zip
cpython-0a1ff24acfc15d8c7f2dc41000a6f3d9a31e7480.tar.gz
cpython-0a1ff24acfc15d8c7f2dc41000a6f3d9a31e7480.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_pyio.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 1e105f2..6833883 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -1182,6 +1182,7 @@ class BufferedWriter(_BufferedIOMixin):
self.buffer_size = buffer_size
self._write_buf = bytearray()
self._write_lock = Lock()
+ _register_writer(self)
def writable(self):
return self.raw.writable()
@@ -2571,3 +2572,26 @@ class StringIO(TextIOWrapper):
def detach(self):
# This doesn't make sense on StringIO.
self._unsupported("detach")
+
+
+# ____________________________________________________________
+
+import atexit, weakref
+
+_all_writers = weakref.WeakSet()
+
+def _register_writer(w):
+ # keep weak-ref to buffered writer
+ _all_writers.add(w)
+
+def _flush_all_writers():
+ # Ensure all buffered writers are flushed before proceeding with
+ # normal shutdown. Otherwise, if the underlying file objects get
+ # finalized before the buffered writer wrapping it then any buffered
+ # data will be lost.
+ for w in _all_writers:
+ try:
+ w.flush()
+ except:
+ pass
+atexit.register(_flush_all_writers)