https://github.com/python/cpython/commit/22691a32b122cff3ad17460c068f70e806cd7e84
commit: 22691a32b122cff3ad17460c068f70e806cd7e84
branch: main
author: yangbaechu <[email protected]>
committer: cmaloney <[email protected]>
date: 2026-11-22T16:21:06-08:00
summary:

gh-155074: Fix remaining-space calculation in `_pyio.BufferedReader.readinto()` 
(#155075)


This PR fixes `_pyio.BufferedReader.readinto()` when data from the internal 
buffer only partially fills the destination before a refill.

The pure Python implementation limited each buffered copy by the destination's 
original length. After earlier bytes had been copied, this could make the next 
copy exceed the remaining memoryview slice and raise `ValueError`. The copy is 
now limited by the remaining destination space, `len(buf) - written`.

The regression test is shared by the C and pure Python implementations and 
verifies the returned byte count, destination contents, and unread data.

files:
A Misc/NEWS.d/next/Library/2026-08-02-19-08-17.gh-issue-155074.Vj8qL2.rst
M Lib/_pyio.py
M Lib/test/test_io/test_bufferedio.py

diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 034d81a9bc1885..935cbf151d0f88 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -1210,7 +1210,8 @@ def _readinto(self, buf, read1):
             while written < len(buf):
 
                 # First try to read from internal buffer
-                avail = min(len(self._read_buf) - self._read_pos, len(buf))
+                avail = min(len(self._read_buf) - self._read_pos,
+                            len(buf) - written)
                 if avail:
                     buf[written:written+avail] = \
                         self._read_buf[self._read_pos:self._read_pos+avail]
diff --git a/Lib/test/test_io/test_bufferedio.py 
b/Lib/test/test_io/test_bufferedio.py
index b114a927c7cb1e..38316894fb90ce 100644
--- a/Lib/test/test_io/test_bufferedio.py
+++ b/Lib/test/test_io/test_bufferedio.py
@@ -319,6 +319,14 @@ def test_readinto(self):
         self.assertIsNone(bufio.readinto(b))
         self.assertIsNone(bufio.readinto1(b))
 
+    def test_readinto_with_remaining_buffered_data(self):
+        bufio = self.tp(self.BytesIO(b"abcd"), buffer_size=2)
+        self.assertEqual(bufio.read(1), b"a")
+        b = bytearray(2)
+        self.assertEqual(bufio.readinto(b), 2)
+        self.assertEqual(b, b"bc")
+        self.assertEqual(bufio.read(), b"d")
+
     def test_readinto1(self):
         buffer_size = 10
         rawio = self.MockRawIO((b"abc", b"de", b"fgh", b"jkl"))
diff --git 
a/Misc/NEWS.d/next/Library/2026-08-02-19-08-17.gh-issue-155074.Vj8qL2.rst 
b/Misc/NEWS.d/next/Library/2026-08-02-19-08-17.gh-issue-155074.Vj8qL2.rst
new file mode 100644
index 00000000000000..70489c9d62f526
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-08-02-19-08-17.gh-issue-155074.Vj8qL2.rst
@@ -0,0 +1,2 @@
+Fix the pure Python implementation of ``BufferedReader.readinto()`` raising
+:exc:`ValueError` after partially filling the destination buffer.

_______________________________________________
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