8000 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

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
refactor
  • Loading branch information
chgnrdv committed May 24, 2024
commit 43628cec598c3cfce72990035e2e3075d7a92d4c
8 changes: 6 additions & 2 deletions Modules/_io/textio.c
< 10000 div class="file-actions pt-0 mb-md-0 mb-2 ml-md-2 flex-shrink-0 flex-md-justify-end">
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,8 @@ _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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about while instead of if?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would also need a check to make sure that bytes_len > self->chunk_size isn't true.

// Prevent to concatenate more than chunk_size data.
if (_textiowrapper_writeflush(self) < 0) {
Expand All @@ -1728,9 +1730,9 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
if (self->pending_bytes) {
// gh-119506: call to _textiowrapper_writeflush()
// can write new data to pending_bytes
PyObject *tmp = self->pending_bytes;
pending = self->pending_bytes;
self->pending_bytes = b;
b = tmp;
b = pending;
}
}

Expand All @@ -1741,6 +1743,7 @@ _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 @@ -1750,6 +1753,7 @@ _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
Loading
0