diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 1dab166..1f01670 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -781,6 +781,13 @@ class IOTest(unittest.TestCase): with self.open("non-existent", "r", opener=opener) as f: self.assertEqual(f.read(), "egg\n") + def test_bad_opener(self): + # Issue #27066 + def badopener(fname, flags): + return -1 + self.assertRaises(OSError, open, 'non-existent', 'r', + opener=badopener) + def test_fileio_closefd(self): # Issue #4841 with self.open(__file__, 'rb') as f1, \ diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index a02a9c1..539ff23 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -421,6 +421,9 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, self->fd = _PyLong_AsInt(fdobj); Py_DECREF(fdobj); if (self->fd == -1) { + if (!PyErr_Occurred()) + /* The opener returned -1. See issue #27066 */ + PyErr_SetString(PyExc_OSError, "opener returned -1"); goto error; } }