diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2502,6 +2502,11 @@ class MiscIOTest(unittest.TestCase): {"mode": "w+b", "buffering": 0}, ]: f = self.open(support.TESTFN, **kwargs) + raw = f + if hasattr(raw, 'buffer'): + raw = raw.buffer + if hasattr(raw, 'raw'): + raw = raw.raw f.close() self.assertRaises(ValueError, f.flush) self.assertRaises(ValueError, f.fileno) @@ -2512,6 +2517,7 @@ class MiscIOTest(unittest.TestCase): self.assertRaises(ValueError, f.read) if hasattr(f, "read1"): self.assertRaises(ValueError, f.read1, 1024) + self.assertRaises(ValueError, raw.readall) if hasattr(f, "readinto"): self.assertRaises(ValueError, f.readinto, bytearray(1024)) self.assertRaises(ValueError, f.readline) diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c --- 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);