summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAngstyDuck <solsticedante@gmail.com>2021-10-01 20:11:08 (GMT)
committerGitHub <noreply@github.com>2021-10-01 20:11:08 (GMT)
commita450398933d265011e1e8eae7f771b70f97945fb (patch)
tree7e76e2afb9fda9dbf50d577bc2290c93dc35a98d
parentcd760ceb67c5164983838ed7eb73a833d1597da0 (diff)
downloadcpython-a450398933d265011e1e8eae7f771b70f97945fb.zip
cpython-a450398933d265011e1e8eae7f771b70f97945fb.tar.gz
cpython-a450398933d265011e1e8eae7f771b70f97945fb.tar.bz2
bpo-44687: Ensure BufferedReader objects with unread buffers can peek even when the underlying file is closed (GH-28457)
-rw-r--r--Misc/NEWS.d/next/C API/2021-09-19-17-18-25.bpo-44687.3fqDRC.rst1
-rw-r--r--Modules/_io/bufferedio.c8
2 files changed, 6 insertions, 3 deletions
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 0000000..d38fa60
--- /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 5984d34..ba966f5 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;