Skip to content

[3.12] gh-115059: Flush the underlying write buffer in io.BufferedRandom.read1() (GH-115163) #115205

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 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,28 @@ def test_interleaved_read_write(self):
f.flush()
self.assertEqual(raw.getvalue(), b'a2c')

def test_read1_after_write(self):
with self.BytesIO(b'abcdef') as raw:
with self.tp(raw, 3) as f:
f.write(b"1")
self.assertEqual(f.read1(1), b'b')
f.flush()
self.assertEqual(raw.getvalue(), b'1bcdef')
with self.BytesIO(b'abcdef') as raw:
with self.tp(raw, 3) as f:
f.write(b"1")
self.assertEqual(f.read1(), b'bcd')
f.flush()
self.assertEqual(raw.getvalue(), b'1bcdef')
with self.BytesIO(b'abcdef') as raw:
with self.tp(raw, 3) as f:
f.write(b"1")
# XXX: read(100) returns different numbers of bytes
# in Python and C implementations.
self.assertEqual(f.read1(100)[:3], b'bcd')
f.flush()
self.assertEqual(raw.getvalue(), b'1bcdef')

def test_interleaved_readline_write(self):
with self.BytesIO(b'ab\ncdef\ng\n') as raw:
with self.tp(raw) as f:
Expand All @@ -2518,6 +2540,36 @@ def test_interleaved_readline_write(self):
f.flush()
self.assertEqual(raw.getvalue(), b'1b\n2def\n3\n')

def test_xxx(self):
with self.BytesIO(b'abcdefgh') as raw:
with self.tp(raw) as f:
f.write(b'123')
self.assertEqual(f.read(), b'defgh')
f.write(b'456')
f.flush()
self.assertEqual(raw.getvalue(), b'123defgh456')
with self.BytesIO(b'abcdefgh') as raw:
with self.tp(raw) as f:
f.write(b'123')
self.assertEqual(f.read(3), b'def')
f.write(b'456')
f.flush()
self.assertEqual(raw.getvalue(), b'123def456')
with self.BytesIO(b'abcdefgh') as raw:
with self.tp(raw) as f:
f.write(b'123')
self.assertEqual(f.read1(), b'defgh')
f.write(b'456')
f.flush()
self.assertEqual(raw.getvalue(), b'123defgh456')
with self.BytesIO(b'abcdefgh') as raw:
with self.tp(raw) as f:
f.write(b'123')
self.assertEqual(f.read1(3), b'def')
f.write(b'456')
f.flush()
self.assertEqual(raw.getvalue(), b'123def456')

# You can't construct a BufferedRandom over a non-seekable stream.
test_unseekable = None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`io.BufferedRandom.read1` now flushes the underlying write buffer.
10 changes: 10 additions & 0 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,16 @@ _io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
Py_DECREF(res);
return NULL;
}
/* Flush the write buffer if necessary */
if (self->writable) {
PyObject *r = buffered_flush_and_rewind_unlocked(self);
if (r == NULL) {
LEAVE_BUFFERED(self)
Py_DECREF(res);
return NULL;
}
Py_DECREF(r);
}
_bufferedreader_reset_buf(self);
r = _bufferedreader_raw_read(self, PyBytes_AS_STRING(res), n);
LEAVE_BUFFERED(self)
Expand Down