Skip to content

Commit 8666356

Browse files
closes bpo-28557: error message for bad raw readinto (GH-7496)
Co-authored-by: Benjamin Peterson <[email protected]>
1 parent 04fc4f2 commit 8666356

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

Lib/test/test_io.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,22 @@ def test_args_error(self):
15871587
with self.assertRaisesRegex(TypeError, "BufferedReader"):
15881588
self.tp(io.BytesIO(), 1024, 1024, 1024)
15891589

1590+
def test_bad_readinto_value(self):
1591+
rawio = io.BufferedReader(io.BytesIO(b"12"))
1592+
rawio.readinto = lambda buf: -1
1593+
bufio = self.tp(rawio)
1594+
with self.assertRaises(OSError) as cm:
1595+
bufio.readline()
1596+
self.assertIsNone(cm.exception.__cause__)
1597+
1598+
def test_bad_readinto_type(self):
1599+
rawio = io.BufferedReader(io.BytesIO(b"12"))
1600+
rawio.readinto = lambda buf: b''
1601+
bufio = self.tp(rawio)
1602+
with self.assertRaises(OSError) as cm:
1603+
bufio.readline()
1604+
self.assertIsInstance(cm.exception.__cause__, TypeError)
1605+
15901606

15911607
class PyBufferedReaderTest(BufferedReaderTest):
15921608
tp = pyio.BufferedReader

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,7 @@ Péter Szabó
16781678
John Szakmeister
16791679
Piotr Szczepaniak
16801680
Amir Szekely
1681+
David Szotten
16811682
Maciej Szulik
16821683
Joel Taddei
16831684
Arfrever Frehtes Taifersar Arahesis
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the error message for a misbehaving ``rawio.readinto``

Modules/_io/bufferedio.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,15 @@ _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
14831483
}
14841484
n = PyNumber_AsSsize_t(res, PyExc_ValueError);
14851485
Py_DECREF(res);
1486+
1487+
if (n == -1 && PyErr_Occurred()) {
1488+
_PyErr_FormatFromCause(
1489+
PyExc_OSError,
1490+
"raw readinto() failed"
1491+
);
1492+
return -1;
1493+
}
1494+
14861495
if (n < 0 || n > len) {
14871496
PyErr_Format(PyExc_OSError,
14881497
"raw readinto() returned invalid length %zd "

0 commit comments

Comments
 (0)