From 317def9fdb29893df1ab380d396fcdd2eafe0588 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 13 Dec 2017 01:39:26 +0100 Subject: [PATCH] bpo-17852: Revert incorrect fix based on misunderstanding of _Py_PyAtExit() semantics (#4826) --- Lib/_pyio.py | 24 --------- .../2017-12-13-00-00-37.bpo-17852.Q8BP8N.rst | 1 + Modules/_io/_iomodule.c | 2 - Modules/_io/_iomodule.h | 2 - Modules/_io/bufferedio.c | 51 +------------------ 5 files changed, 2 insertions(+), 78 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2017-12-13-00-00-37.bpo-17852.Q8BP8N.rst diff --git a/Lib/_pyio.py b/Lib/_pyio.py index adf5d0ecbf6..b59a6509787 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1182,7 +1182,6 @@ 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() @@ -2587,26 +2586,3 @@ 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) diff --git a/Misc/NEWS.d/next/Library/2017-12-13-00-00-37.bpo-17852.Q8BP8N.rst b/Misc/NEWS.d/next/Library/2017-12-13-00-00-37.bpo-17852.Q8BP8N.rst new file mode 100644 index 00000000000..768108d5a54 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-12-13-00-00-37.bpo-17852.Q8BP8N.rst @@ -0,0 +1 @@ +Revert incorrect fix based on misunderstanding of _Py_PyAtExit() semantics. diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 5db44f970d2..f0621f4d4ab 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -766,8 +766,6 @@ PyInit__io(void) !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0))) goto fail; - _Py_PyAtExit(_PyIO_atexit_flush); - state->initialized = 1; return m; diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h index 1dce5dada4e..db8403774ea 100644 --- a/Modules/_io/_iomodule.h +++ b/Modules/_io/_iomodule.h @@ -183,5 +183,3 @@ extern PyObject *_PyIO_empty_str; extern PyObject *_PyIO_empty_bytes; extern PyTypeObject _PyBytesIOBuffer_Type; - -extern void _PyIO_atexit_flush(void); diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 1ae7a70bbda..d7e82b9dba1 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -197,7 +197,7 @@ bufferediobase_write(PyObject *self, PyObject *args) } -typedef struct _buffered { +typedef struct { PyObject_HEAD PyObject *raw; @@ -239,18 +239,8 @@ typedef struct _buffered { PyObject *dict; PyObject *weakreflist; - - /* a doubly-linked chained list of "buffered" objects that need to - be flushed when the process exits */ - struct _buffered *next, *prev; } buffered; -/* the actual list of buffered objects */ -static buffered buffer_list_end = { - .next = &buffer_list_end, - .prev = &buffer_list_end -}; - /* Implementation notes: @@ -389,21 +379,10 @@ _enter_buffered_busy(buffered *self) (self->buffer_size * (size / self->buffer_size))) -static void -remove_from_linked_list(buffered *self) -{ - self->next->prev = self->prev; - self->prev->next = self->next; - self->prev = NULL; - self->next = NULL; -} - static void buffered_dealloc(buffered *self) { self->finalizing = 1; - if (self->next != NULL) - remove_from_linked_list(self); if (_PyIOBase_finalize((PyObject *) self) < 0) return; _PyObject_GC_UNTRACK(self); @@ -1827,38 +1806,10 @@ _io_BufferedWriter___init___impl(buffered *self, PyObject *raw, self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedWriter_Type && Py_TYPE(raw) == &PyFileIO_Type); - if (self->next == NULL) { - self->prev = &buffer_list_end; - self->next = buffer_list_end.next; - buffer_list_end.next->prev = self; - buffer_list_end.next = self; - } - self->ok = 1; return 0; } -/* -* 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. -*/ -void _PyIO_atexit_flush(void) -{ - while (buffer_list_end.next != &buffer_list_end) { - buffered *buf = buffer_list_end.next; - remove_from_linked_list(buf); - if (buf->ok && !buf->finalizing) { - /* good state and not finalizing */ - Py_INCREF(buf); - buffered_flush(buf, NULL); - Py_DECREF(buf); - PyErr_Clear(); - } - } -} - static Py_ssize_t _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len) {