From: Berker Peksag Date: Tue, 12 May 2015 14:01:05 +0000 (+0300) Subject: Issue #23796: peak and read1 methods of BufferedReader now raise ValueError X-Git-Tag: v3.5.0b1~192 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d10d6ae2fad2c1963e119ed7bc031b723603b363;p=thirdparty%2FPython%2Fcpython.git 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 46b896fa67fe..f7c6e2ddb007 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1191,6 +1191,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 diff --git a/Misc/NEWS b/Misc/NEWS index ec71eb94f354..5a589d63449f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,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 #21795: smtpd now supports the 8BITMIME extension whenever the new *decode_data* constructor argument is set to False. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 2c9064855a6d..23ba3df866b1 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -871,6 +871,8 @@ _io__Buffered_peek_impl(buffered *self, Py_ssize_t size) PyObject *res = NULL; CHECK_INITIALIZED(self) + CHECK_CLOSED(self, "peek of closed file") + if (!ENTER_BUFFERED(self)) return NULL; @@ -947,6 +949,9 @@ _io__Buffered_read1_impl(buffered *self, Py_ssize_t n) "read length must be positive"); return NULL; } + + CHECK_CLOSED(self, "read of closed file") + if (n == 0) return PyBytes_FromStringAndSize(NULL, 0);