Skip to content

Commit

Permalink
pythongh-115059: Flush the underlying write buffer in io.BufferedRand…
Browse files Browse the repository at this point in the history
…om.read1() (pythonGH-115163)

(cherry picked from commit 846fd72)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
serhiy-storchaka authored and miss-islington committed Feb 9, 2024
1 parent 235c54f commit 0123722
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
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

0 comments on commit 0123722

Please sign in to comment.