10000 gh-119506: fix `_io.TextIOWrapper.write()` write during flush by chgnrdv · Pull Request #119507 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119506: fix _io.TextIOWrapper.write() write during flush #119507 8000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 3, 2024
Merged
Prev Previous commit
Next Next commit
simplify code and write more comments
  • Loading branch information
methane committed Jun 1, 2024
commit 483975c61d0f5eadf99f3f3417ac0bbab948efee
25 changes: 16 additions & 9 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1719,19 +1719,26 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
bytes_len = PyBytes_GET_SIZE(b);
}

// 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;
// We should avoid concatinating huge data.
// Flush the buffer before adding b to the buffer if b is not small.
// https://github.com/python/cpython/issues/87426
if (bytes_len >= self->chunk_size) {
// _textiowrapper_writeflush() calls buffer.write().
// self->pending_bytes can be appended during buffer->write()
// or other thread.
// We need to loop until buffer becomes empty.
// https://github.com/python/cpython/issues/118138
// https://github.com/python/cpython/issues/119506
while (self->pending_bytes != NULL) {
if (_textiowrapper_writeflush(self) < 0) {
Py_DECREF(b);
return NULL;
}
}
// _textiowrapper_writeflush() releases GIL during write.
// self->pending_bytes and self->pending_bytes_count may be changed now.
}

if (self->pending_bytes == NULL) {
self->pending_bytes_count = 0;
assert(self->pending_bytes_count == 0);
self->pending_bytes = b;
}
else if (!PyList_CheckExact(self->pending_bytes)) {
Expand Down
Loading
0