diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index dac30cbf400..a286c126c9e 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2505,6 +2505,8 @@ def test_io_after_close(self): self.assertRaises(ValueError, f.read) if hasattr(f, "read1"): self.assertRaises(ValueError, f.read1, 1024) + if hasattr(f, "readall"): + self.assertRaises(ValueError, f.readall) if hasattr(f, "readinto"): self.assertRaises(ValueError, f.readinto, bytearray(1024)) self.assertRaises(ValueError, f.readline) diff --git a/Misc/NEWS b/Misc/NEWS index 8d19f7a2c14..05fc1b750b3 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -731,6 +731,9 @@ Core and Builtins Library ------- +- Issue #12175: FileIO.readall() now raises a ValueError instead of an IOError + if the file is closed. + - Issue #10916: mmap should not segfault when a file is mapped using 0 as length and a non-zero offset, and an attempt to read past the end of file is made (IndexError is raised instead). Patch by Ross Lagerwall. diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 1aa5ee992d7..141b6dece1b 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -583,6 +583,8 @@ fileio_readall(fileio *self) Py_ssize_t total = 0; int n; + if (self->fd < 0) + return err_closed(); if (!_PyVerify_fd(self->fd)) return PyErr_SetFromErrno(PyExc_IOError);