https://github.com/python/cpython/commit/ec912e7c9d388b212cc6df01475a88124bbcd65e
commit: ec912e7c9d388b212cc6df01475a88124bbcd65e
branch: main
author: Cody Maloney <[email protected]>
committer: cmaloney <[email protected]>
date: 2026-11-19T11:47:55-08:00
summary:

gh-162031: Return None from readinto() on non-blocking streams with no data 
(#162032)

Match PEP-3116 specification for Non-blocking I/O.

First proposed in 2015 as `nonblock-none.patch` on gh-57531.

Co-authored-by: Claude Opus 5.5 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-11-15-03-10-31.gh-issue-162031.2k-3CG.rst
M Lib/_pyio.py
M Lib/test/test_io/test_bufferedio.py
M Lib/test/test_io/test_general.py
M Modules/_io/bufferedio.c

diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 21f40add53593b..ec1b20d0426768 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -733,6 +733,8 @@ def _readinto(self, b, read1):
             data = self.read1(len(b))
         else:
             data = self.read(len(b))
+        if data is None:
+            return None
         n = len(data)
 
         b[:n] = data
@@ -1222,14 +1224,22 @@ def _readinto(self, buf, read1):
                 if len(buf) - written > self.buffer_size:
                     n = self.raw.readinto(buf[written:])
                     if not n:
+                        if n is None and not written:
+                            return None # would block
                         break # eof
                     written += n
 
                 # Otherwise refill internal buffer - unless we're
                 # in read1 mode and already got some data
                 elif not (read1 and written):
-                    if not self._peek_unlocked(1):
+                    # The internal buffer is empty here.
+                    chunk = self.raw.read(self.buffer_size)
+                    if not chunk:
+                        if chunk is None and not written:
+                            return None # would block
                         break # eof
+                    self._read_buf = chunk
+                    self._read_pos = 0
 
                 # In readinto1 mode, return as soon as we have some data
                 if read1 and written:
diff --git a/Lib/test/test_io/test_bufferedio.py 
b/Lib/test/test_io/test_bufferedio.py
index ce594762c325ac..64789821afe59c 100644
--- a/Lib/test/test_io/test_bufferedio.py
+++ b/Lib/test/test_io/test_bufferedio.py
@@ -306,12 +306,18 @@ def test_readinto(self):
         self.assertEqual(b, b"gf")
         self.assertEqual(bufio.readinto(b), 0)
         self.assertEqual(b, b"gf")
-        rawio = self.MockRawIO((b"abc", None))
-        bufio = self.tp(rawio)
+        rawio = self.MockRawIO((b"abc", None, None, None, None, None))
+        bufio = self.tp(rawio, buffer_size=4)
         self.assertEqual(bufio.readinto(b), 2)
         self.assertEqual(b, b"ab")
         self.assertEqual(bufio.readinto(b), 1)
         self.assertEqual(b, b"cb")
+        self.assertIsNone(bufio.readinto(b))
+        self.assertIsNone(bufio.readinto1(b))
+        # Larger than buffer_size: read directly into b
+        b = bytearray(5)
+        self.assertIsNone(bufio.readinto(b))
+        self.assertIsNone(bufio.readinto1(b))
 
     def test_readinto1(self):
         buffer_size = 10
diff --git a/Lib/test/test_io/test_general.py b/Lib/test/test_io/test_general.py
index c4d5a80086718b..c6c138c2388d3e 100644
--- a/Lib/test/test_io/test_general.py
+++ b/Lib/test/test_io/test_general.py
@@ -713,15 +713,18 @@ def test_invalid_newline(self):
     def test_buffered_readinto_mixin(self):
         # Test the implementation provided by BufferedIOBase
         class Stream(self.BufferedIOBase):
+            def __init__(self, data):
+                self.data = data
             def read(self, size):
-                return b"12345"
+                return self.data
             read1 = read
-        stream = Stream()
         for method in ("readinto", "readinto1"):
             with self.subTest(method):
                 buffer = byteslike(5)
-                self.assertEqual(getattr(stream, method)(buffer), 5)
+                self.assertEqual(getattr(Stream(b"12345"), method)(buffer), 5)
                 self.assertEqual(bytes(buffer), b"12345")
+                # Non-blocking stream with no data
+                self.assertIsNone(getattr(Stream(None), method)(buffer))
 
     def test_fspath_support(self):
         def check_path_succeeds(path):
diff --git 
a/Misc/NEWS.d/next/Library/2026-11-15-03-10-31.gh-issue-162031.2k-3CG.rst 
b/Misc/NEWS.d/next/Library/2026-11-15-03-10-31.gh-issue-162031.2k-3CG.rst
new file mode 100644
index 00000000000000..01f1311c735d9b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-15-03-10-31.gh-issue-162031.2k-3CG.rst
@@ -0,0 +1,3 @@
+Fix :meth:`io.BufferedIOBase.readinto` and :meth:`~io.BufferedIOBase.readinto1`
+raising :exc:`TypeError` instead of returning ``None`` when called on a
+non-blocking stream with no data available.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 757e6a10558e48..237b6ea53c367a 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -60,6 +60,11 @@ _bufferediobase_readinto_generic(PyObject *self, Py_buffer 
*buffer, char readint
     if (data == NULL)
         return NULL;
 
+    /* Non-blocking stream with no data available */
+    if (data == Py_None) {
+        return data;
+    }
+
     if (!PyBytes_Check(data)) {
         Py_DECREF(data);
         PyErr_SetString(PyExc_TypeError, "read() should return bytes");

_______________________________________________
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