bpo-35950: Raise UnsupportedOperation in BufferedReader.truncate() (GH-18586)
The truncate() method of io.BufferedReader() should raise
UnsupportedOperation when it is called on a read-only
io.BufferedReader() instance.
https://bugs.python.org/issue35950
Automerge-Triggered-By: @methane
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 6f55577..a09082c 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -1315,15 +1315,19 @@
PyObject *res = NULL;
CHECK_INITIALIZED(self)
+ CHECK_CLOSED(self, "truncate of closed file")
+ if (!self->writable) {
+ return bufferediobase_unsupported("truncate");
+ }
if (!ENTER_BUFFERED(self))
return NULL;
- if (self->writable) {
- res = buffered_flush_and_rewind_unlocked(self);
- if (res == NULL)
- goto end;
- Py_CLEAR(res);
+ res = buffered_flush_and_rewind_unlocked(self);
+ if (res == NULL) {
+ goto end;
}
+ Py_CLEAR(res);
+
res = PyObject_CallMethodOneArg(self->raw, _PyIO_str_truncate, pos);
if (res == NULL)
goto end;