Skip to content

Commit 6f988b0

Browse files
authored
pythongh-85524: Raise "UnsupportedOperation" on FileIO.readall (python#141214)
io.UnsupportedOperation is a subclass of OSError and recommended by io.IOBase for this case; matches other read methods on io.FileIO.
1 parent 909f76d commit 6f988b0

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

Lib/test/test_io/test_general.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def test_invalid_operations(self):
125125
self.assertRaises(exc, fp.readline)
126126
with self.open(os_helper.TESTFN, "wb", buffering=0) as fp:
127127
self.assertRaises(exc, fp.read)
128+
self.assertRaises(exc, fp.readall)
128129
self.assertRaises(exc, fp.readline)
129130
with self.open(os_helper.TESTFN, "rb", buffering=0) as fp:
130131
self.assertRaises(exc, fp.write, b"blah")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Update ``io.FileIO.readall``, an implementation of :meth:`io.RawIOBase.readall`,
2+
to follow :class:`io.IOBase` guidelines and raise :exc:`io.UnsupportedOperation`
3+
when a file is in "w" mode rather than :exc:`OSError`

Modules/_io/clinic/fileio.c.h

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/fileio.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ new_buffersize(fileio *self, size_t currentsize)
728728
@permit_long_docstring_body
729729
_io.FileIO.readall
730730
731+
cls: defining_class
732+
/
733+
731734
Read all data from the file, returned as bytes.
732735
733736
Reads until either there is an error or read() returns size 0 (indicates EOF).
@@ -738,8 +741,8 @@ data is available (EAGAIN is returned before bytes are read) returns None.
738741
[clinic start generated code]*/
739742

740743
static PyObject *
741-
_io_FileIO_readall_impl(fileio *self)
742-
/*[clinic end generated code: output=faa0292b213b4022 input=10d8b2ec403302dc]*/
744+
_io_FileIO_readall_impl(fileio *self, PyTypeObject *cls)
745+
/*[clinic end generated code: output=d546737ec895c462 input=cecda40bf9961299]*/
743746
{
744747
Py_off_t pos, end;
745748
PyBytesWriter *writer;
@@ -750,6 +753,10 @@ _io_FileIO_readall_impl(fileio *self)
750753
if (self->fd < 0) {
751754
return err_closed();
752755
}
756+
if (!self->readable) {
757+
_PyIO_State *state = get_io_state_by_cls(cls);
758+
return err_mode(state, "reading");
759+
}
753760

754761
if (self->stat_atopen != NULL && self->stat_atopen->st_size < _PY_READ_MAX) {
755762
end = (Py_off_t)self->stat_atopen->st_size;
@@ -873,7 +880,7 @@ _io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size)
873880
}
874881

875882
if (size < 0)
876-
return _io_FileIO_readall_impl(self);
883+
return _io_FileIO_readall_impl(self, cls);
877884

878885
if (size > _PY_READ_MAX) {
879886
size = _PY_READ_MAX;

0 commit comments

Comments
 (0)