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 1 commit
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
Next Next commit
gh-119506: fix _io.TextIOWrapper.write() write during flush
* check if call to `_textiowrapper_writeflush()` has left any data in `self->pending_bytes`. If so, store them in `self->pending_bytes` after `b`
* add test
  • Loading branch information
chgnrdv committed May 24, 2024
commit 5b924dc728ea6c8a49014d35326e07a88e7205f8
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
17 changes: 12 additions & 5 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1719,16 +1719,23 @@ _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) {
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
PyObject *tmp = self->pending_bytes;
self->pending_bytes = b;
b = tmp;
}
}

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