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
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4016,6 +4016,22 @@ def write(self, data):
t.write("x"*chunk_size)
self.assertEqual([b"abcdef", b"ghi", b"x"*chunk_size], buf._write_stack)

def test_issue119506(self):
chunk_size = 8192

class MockIO(self.MockRawIO):
def write(self, data):
t.write("efg")
return super().write(data)

buf = MockIO()
t = self.TextIOWrapper(buf)
t.write("a" * (chunk_size - 1))
t.write("bcd")
t.flush()

self.assertEqual([b"a" * (chunk_size - 1), b"bcdefg"], buf._write_stack)


class PyTextIOWrapperTest(TextIOWrapperTest):
io = pyio
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in :meth:`!io.TextIOWrapper.write` method, which causes wrapper's internal buffer to be rewritten during flushing of large data.
21 changes: 16 additions & 5 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1719,21 +1719,31 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
bytes_len = PyBytes_GET_SIZE(b);
}

if (self->pending_bytes == NULL) {
self->pending_bytes_count = 0;
self->pending_bytes = b;
}
else if (self->pending_bytes_count + bytes_len > self->chunk_size) {
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) {
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;
}
Copy link
Member

Choose a reason for hiding this comment

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

why do you swap data here?

  • self->pending can be list
  • call from other thread might be returned already.
    • "first returned first write" seems correct than "first called first write".

Choose a reason for hiding this comment

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

If you look at the test case, where flushing the stream triggers a write, it's not a threading thing, it's because of write being called recursively.

Copy link
Member

Choose a reason for hiding this comment

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

Even single thread case, it is not possble to guarantee "first called, first written."
Simple implementation is more important.

}

if (self->pending_bytes == NULL) {
self->pending_bytes_count = 0;
self->pending_bytes = b;
}
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 @@ -1743,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