Issue #23796: peak and read1 methods of BufferedReader now raise ValueError
if they called on a closed object.
Patch by John Hergenroeder.
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 2fb1b1e..416c547 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -1115,6 +1115,14 @@
self.assertEqual(rawio._extraneous_reads, 0,
"failed for {}: {} != 0".format(n, rawio._extraneous_reads))
+ def test_read_on_closed(self):
+ # Issue #23796
+ b = io.BufferedReader(io.BytesIO(b"12"))
+ b.read(1)
+ b.close()
+ self.assertRaises(ValueError, b.peek)
+ self.assertRaises(ValueError, b.read1, 1)
+
class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
tp = io.BufferedReader
diff --git a/Misc/NEWS b/Misc/NEWS
index bc57613..7701261 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -45,6 +45,9 @@
Library
-------
+- Issue #23796: peak and read1 methods of BufferedReader now raise ValueError
+ if they called on a closed object. Patch by John Hergenroeder.
+
- Issue #24134: assertRaises(), assertRaisesRegex(), assertWarns() and
assertWarnsRegex() checks are not longer successful if the callable is None.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 3606cc8..365bb85 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -889,6 +889,8 @@
PyObject *res = NULL;
CHECK_INITIALIZED(self)
+ CHECK_CLOSED(self, "peek of closed file")
+
if (!PyArg_ParseTuple(args, "|n:peek", &n)) {
return NULL;
}
@@ -963,6 +965,9 @@
"read length must be positive");
return NULL;
}
+
+ CHECK_CLOSED(self, "read of closed file")
+
if (n == 0)
return PyBytes_FromStringAndSize(NULL, 0);