Skip to content

Commit b17880f

Browse files
cmaloneyclaude
andcommitted
gh-162031: Return None from readinto() on non-blocking streams with no data
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 <noreply@anthropic.com>
1 parent 60eca2d commit b17880f

5 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎Lib/_pyio.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ def _readinto(self, b, read1):
733733
data = self.read1(len(b))
734734
else:
735735
data = self.read(len(b))
736+
if data is None:
737+
return None
736738
n = len(data)
737739

738740
b[:n] = data
@@ -1222,14 +1224,22 @@ def _readinto(self, buf, read1):
12221224
if len(buf) - written > self.buffer_size:
12231225
n = self.raw.readinto(buf[written:])
12241226
if not n:
1227+
if n is None and not written:
1228+
return None # would block
12251229
break # eof
12261230
written += n
12271231

12281232
# Otherwise refill internal buffer - unless we're
12291233
# in read1 mode and already got some data
12301234
elif not (read1 and written):
1231-
if not self._peek_unlocked(1):
1235+
# The internal buffer is empty here.
1236+
chunk = self.raw.read(self.buffer_size)
1237+
if not chunk:
1238+
if chunk is None and not written:
1239+
return None # would block
12321240
break # eof
1241+
self._read_buf = chunk
1242+
self._read_pos = 0
12331243

12341244
# In readinto1 mode, return as soon as we have some data
12351245
if read1 and written:

‎Lib/test/test_io/test_bufferedio.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,18 @@ def test_readinto(self):
306306
self.assertEqual(b, b"gf")
307307
self.assertEqual(bufio.readinto(b), 0)
308308
self.assertEqual(b, b"gf")
309-
rawio = self.MockRawIO((b"abc", None))
310-
bufio = self.tp(rawio)
309+
rawio = self.MockRawIO((b"abc", None, None, None, None, None))
310+
bufio = self.tp(rawio, buffer_size=4)
311311
self.assertEqual(bufio.readinto(b), 2)
312312
self.assertEqual(b, b"ab")
313313
self.assertEqual(bufio.readinto(b), 1)
314314
self.assertEqual(b, b"cb")
315+
self.assertIsNone(bufio.readinto(b))
316+
self.assertIsNone(bufio.readinto1(b))
317+
# Larger than buffer_size: read directly into b
318+
b = bytearray(5)
319+
self.assertIsNone(bufio.readinto(b))
320+
self.assertIsNone(bufio.readinto1(b))
315321

316322
def test_readinto1(self):
317323
buffer_size = 10

‎Lib/test/test_io/test_general.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,15 +713,18 @@ def test_invalid_newline(self):
713713
def test_buffered_readinto_mixin(self):
714714
# Test the implementation provided by BufferedIOBase
715715
class Stream(self.BufferedIOBase):
716+
def __init__(self, data):
717+
self.data = data
716718
def read(self, size):
717-
return b"12345"
719+
return self.data
718720
read1 = read
719-
stream = Stream()
720721
for method in ("readinto", "readinto1"):
721722
with self.subTest(method):
722723
buffer = byteslike(5)
723-
self.assertEqual(getattr(stream, method)(buffer), 5)
724+
self.assertEqual(getattr(Stream(b"12345"), method)(buffer), 5)
724725
self.assertEqual(bytes(buffer), b"12345")
726+
# Non-blocking stream with no data
727+
self.assertIsNone(getattr(Stream(None), method)(buffer))
725728

726729
def test_fspath_support(self):
727730
def check_path_succeeds(path):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :meth:`io.BufferedIOBase.readinto` and :meth:`~io.BufferedIOBase.readinto1`
2+
raising :exc:`TypeError` instead of returning ``None`` when called on a
3+
non-blocking stream with no data available.

‎Modules/_io/bufferedio.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ _bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readint
6060
if (data == NULL)
6161
return NULL;
6262

63+
/* Non-blocking stream with no data available */
64+
if (data == Py_None) {
65+
return data;
66+
}
67+
6368
if (!PyBytes_Check(data)) {
6469
Py_DECREF(data);
6570
PyErr_SetString(PyExc_TypeError, "read() should return bytes");

0 commit comments

Comments
 (0)