]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
closes bpo-28557: error message for bad raw readinto (GH-7496)
authorDavid Szotten <davidszotten@gmail.com>
Mon, 15 Jun 2020 23:53:57 +0000 (00:53 +0100)
committerGitHub <noreply@github.com>
Mon, 15 Jun 2020 23:53:57 +0000 (18:53 -0500)
Co-authored-by: Benjamin Peterson <benjamin@python.org>
Lib/test/test_io.py
Misc/ACKS
Misc/NEWS.d/next/Library/2018-06-07-22-04-01.bpo-28557.ViNJnK.rst [new file with mode: 0644]
Modules/_io/bufferedio.c

index 7b8511b66bf10f299bb9463710d9bdce946d40cc..c0d67a17d8c6f1de9232fafa093c2ea0f8f3de1d 100644 (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
index 0fc1954a2223754d2898fa042ad6e471695fadd9..87f0dede365c249a860c74309294598193d0ab80 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1678,6 +1678,7 @@ Péter Szabó
 John Szakmeister
 Piotr Szczepaniak
 Amir Szekely
+David Szotten
 Maciej Szulik
 Joel Taddei
 Arfrever Frehtes Taifersar Arahesis
diff --git a/Misc/NEWS.d/next/Library/2018-06-07-22-04-01.bpo-28557.ViNJnK.rst b/Misc/NEWS.d/next/Library/2018-06-07-22-04-01.bpo-28557.ViNJnK.rst
new file mode 100644 (file)
index 0000000..4137e2f
--- /dev/null
@@ -0,0 +1 @@
+Improve the error message for a misbehaving ``rawio.readinto``
index f8e21f206f316ad647c7d42ba8d470323b16a21c..5984d34cc08290c4b17552ee43f2eb938af3ffa2 100644 (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 "