closes bpo-28557: error message for bad raw readinto (GH-7496)

Co-authored-by: Benjamin Peterson <benjamin@python.org>
This commit is contained in:
David Szotten 2020-06-16 00:53:57 +01:00 committed by GitHub
parent 04fc4f2a46
commit 8666356280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 0 deletions

View File

@ -1587,6 +1587,22 @@ class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
with self.assertRaisesRegex(TypeError, "BufferedReader"):
self.tp(io.BytesIO(), 1024, 1024, 1024)
def test_bad_readinto_value(self):
rawio = io.BufferedReader(io.BytesIO(b"12"))
rawio.readinto = lambda buf: -1
bufio = self.tp(rawio)
with self.assertRaises(OSError) as cm:
bufio.readline()
self.assertIsNone(cm.exception.__cause__)
def test_bad_readinto_type(self):
rawio = io.BufferedReader(io.BytesIO(b"12"))
rawio.readinto = lambda buf: b''
bufio = self.tp(rawio)
with self.assertRaises(OSError) as cm:
bufio.readline()
self.assertIsInstance(cm.exception.__cause__, TypeError)
class PyBufferedReaderTest(BufferedReaderTest):
tp = pyio.BufferedReader

View File

@ -1678,6 +1678,7 @@ Péter Szabó
John Szakmeister
Piotr Szczepaniak
Amir Szekely
David Szotten
Maciej Szulik
Joel Taddei
Arfrever Frehtes Taifersar Arahesis

View File

@ -0,0 +1 @@
Improve the error message for a misbehaving ``rawio.readinto``

View File

@ -1483,6 +1483,15 @@ _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
}
n = PyNumber_AsSsize_t(res, PyExc_ValueError);
Py_DECREF(res);
if (n == -1 && PyErr_Occurred()) {
_PyErr_FormatFromCause(
PyExc_OSError,
"raw readinto() failed"
);
return -1;
}
if (n < 0 || n > len) {
PyErr_Format(PyExc_OSError,
"raw readinto() returned invalid length %zd "