[Python-checkins] bpo-44687: Ensure BufferedReader objects with unread buffers can peek even when the underlying file is closed (GH-28457)

zooba webhook-mailer at python.org
Fri Oct 1 16:46:32 EDT 2021


https://github.com/python/cpython/commit/6035d650a322cec9619b306af2a877f3cead1580
commit: 6035d650a322cec9619b306af2a877f3cead1580
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: zooba <steve.dower at microsoft.com>
date: 2021-10-01T21:46:25+01:00
summary:

bpo-44687: Ensure BufferedReader objects with unread buffers can peek even when the underlying file is closed (GH-28457)

Co-authored-by: AngstyDuck <solsticedante at gmail.com>

files:
A Misc/NEWS.d/next/C API/2021-09-19-17-18-25.bpo-44687.3fqDRC.rst
M Modules/_io/bufferedio.c

diff --git a/Misc/NEWS.d/next/C API/2021-09-19-17-18-25.bpo-44687.3fqDRC.rst b/Misc/NEWS.d/next/C API/2021-09-19-17-18-25.bpo-44687.3fqDRC.rst
new file mode 100644
index 0000000000000..d38fa6057f6f9
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2021-09-19-17-18-25.bpo-44687.3fqDRC.rst	
@@ -0,0 +1 @@
+:meth:`BufferedReader.peek` no longer raises :exc:`ValueError` when the entire file has already been buffered.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index f8e21f206f316..b0fe9e4589102 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -341,11 +341,10 @@ _enter_buffered_busy(buffered *self)
      : buffered_closed(self)))
 
 #define CHECK_CLOSED(self, error_msg) \
-    if (IS_CLOSED(self)) { \
+    if (IS_CLOSED(self) & (Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) { \
         PyErr_SetString(PyExc_ValueError, error_msg); \
         return NULL; \
-    }
-
+    } \
 
 #define VALID_READ_BUFFER(self) \
     (self->readable && self->read_end != -1)
@@ -530,6 +529,9 @@ buffered_close(buffered *self, PyObject *args)
         Py_CLEAR(res);
     }
 
+    self->read_end = 0;
+    self->pos = 0;
+
 end:
     LEAVE_BUFFERED(self)
     return res;



More information about the Python-checkins mailing list