https://github.com/python/cpython/commit/20c6535693acacd8136820a032dfcfd9d99a6f89
commit: 20c6535693acacd8136820a032dfcfd9d99a6f89
branch: 3.11
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-02-09T11:01:47Z
summary:

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

(cherry picked from commit 846fd721d518dda88a7d427ec3d2c03c45d9fa90)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-02-08-13-26-14.gh-issue-115059.DqP9dr.rst
M Lib/test/test_io.py
M Modules/_io/bufferedio.c

diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 3dae7890ec3574..7d56260f7a7b00 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -2403,6 +2403,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:
@@ -2415,6 +2437,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
 
diff --git 
a/Misc/NEWS.d/next/Library/2024-02-08-13-26-14.gh-issue-115059.DqP9dr.rst 
b/Misc/NEWS.d/next/Library/2024-02-08-13-26-14.gh-issue-115059.DqP9dr.rst
new file mode 100644
index 00000000000000..331baedd3b24c5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-02-08-13-26-14.gh-issue-115059.DqP9dr.rst
@@ -0,0 +1 @@
+:meth:`io.BufferedRandom.read1` now flushes the underlying write buffer.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index fd1e6389906581..4013d52219cfc0 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -929,6 +929,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)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to