Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions Lib/test/test_io/test_bufferedio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions Lib/test/test_io/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading