Skip to content
Merged
Prev Previous commit
Next Next commit
fixup
  • Loading branch information
methane committed May 31, 2024
commit 156940d71cb4e61f423e6bd9b31eb19df2b92abf
18 changes: 5 additions & 13 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1719,21 +1719,15 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
bytes_len = PyBytes_GET_SIZE(b);
}

PyObject *pending = self->pending_bytes;

if (self->pending_bytes_count + bytes_len > self->chunk_size) {
// Prevent to concatenate more than chunk_size data.
// Prevent to concatenate more than chunk_size data.
while (self->pending_bytes &&
bytes_len + self->pending_bytes_count > self->chunk_size) {
if (_textiowrapper_writeflush(self) < 0) {
Py_DECREF(b);
return NULL;
}
if (self->pending_bytes) {
// gh-119506: call to _textiowrapper_writeflush()
// can write new data to pending_bytes
pending = self->pending_bytes;
self->pending_bytes = b;
b = pending;
}
// _textiowrapper_writeflush() releases GIL during write.
// self->pending_bytes and self->pending_bytes_count may be changed now.
}

if (self->pending_bytes == NULL) {
Expand All @@ -1743,7 +1737,6 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
else if (!PyList_CheckExact(self->pending_bytes)) {
PyObject *list = PyList_New(2);
if (list == NULL) {
self->pending_bytes = pending;
Py_DECREF(b);
return NULL;
}
Expand All @@ -1753,7 +1746,6 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
}
else {
if (PyList_Append(self->pending_bytes, b) < 0) {
self->pending_bytes = pending;
Py_DECREF(b);
return NULL;
}
Expand Down