]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115059: Flush the underlying write buffer in io.BufferedRandom.read1() (GH-115163)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 9 Feb 2024 10:36:12 +0000 (12:36 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Feb 2024 10:36:12 +0000 (12:36 +0200)
Lib/test/test_io.py
Misc/NEWS.d/next/Library/2024-02-08-13-26-14.gh-issue-115059.DqP9dr.rst [new file with mode: 0644]
Modules/_io/bufferedio.c

index 73669ecc7927763233c5bda1594df68ac0a1bf0e..a24579dcc878cfbc4e38084325d9b725559922cc 100644 (file)
@@ -2497,6 +2497,28 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
                 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:
@@ -2509,6 +2531,36 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
                 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 (file)
index 0000000..331baed
--- /dev/null
@@ -0,0 +1 @@
+:meth:`io.BufferedRandom.read1` now flushes the underlying write buffer.
index f02207ace9f3d2626f40c2bf27aa4108960295b1..8ebe9ec7095586fe0e2d340bc8b2e8fe9d8bfcc1 100644 (file)
@@ -1050,6 +1050,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)