]> granicus.if.org Git - python/commitdiff
Issue #23796: peak and read1 methods of BufferedReader now raise ValueError
authorBerker Peksag <berker.peksag@gmail.com>
Tue, 12 May 2015 14:13:56 +0000 (17:13 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Tue, 12 May 2015 14:13:56 +0000 (17:13 +0300)
if they called on a closed object.

Patch by John Hergenroeder.

Lib/test/test_io.py
Misc/NEWS
Modules/_io/bufferedio.c

index 2fb1b1e24a1ec911c6d8c29f4e28e314fd1a049d..416c547334edb5248bf8e16304563ba7aca3dcb1 100644 (file)
@@ -1115,6 +1115,14 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
             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
index bc57613e9f7246dbbad4970e342906bff41fb655..7701261c49de5d0fa3f81be9a6680f6c36d05cd0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -45,6 +45,9 @@ Core and Builtins
 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.
 
index 3606cc8fe090bdf0ae1ead2a61eab6f385dfca25..365bb85c74fce0002542972cd93765cca6684b1e 100644 (file)
@@ -889,6 +889,8 @@ buffered_peek(buffered *self, PyObject *args)
     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 @@ buffered_read1(buffered *self, PyObject *args)
                         "read length must be positive");
         return NULL;
     }
+
+    CHECK_CLOSED(self, "read of closed file")
+
     if (n == 0)
         return PyBytes_FromStringAndSize(NULL, 0);