Commit 480e2853 authored by Barry Warsaw's avatar Barry Warsaw

Issue #27066: Fixed SystemError if a custom opener (for open()) returns

a negative number without setting an exception.
parent fda4d678
...@@ -805,6 +805,22 @@ class IOTest(unittest.TestCase): ...@@ -805,6 +805,22 @@ class IOTest(unittest.TestCase):
with self.open("non-existent", "r", opener=opener) as f: with self.open("non-existent", "r", opener=opener) as f:
self.assertEqual(f.read(), "egg\n") self.assertEqual(f.read(), "egg\n")
def test_bad_opener_negative_1(self):
# Issue #27066.
def badopener(fname, flags):
return -1
with self.assertRaises(ValueError) as cm:
open('non-existent', 'r', opener=badopener)
self.assertEqual(str(cm.exception), 'opener returned -1')
def test_bad_opener_other_negative(self):
# Issue #27066.
def badopener(fname, flags):
return -2
with self.assertRaises(ValueError) as cm:
open('non-existent', 'r', opener=badopener)
self.assertEqual(str(cm.exception), 'opener returned -2')
def test_fileio_closefd(self): def test_fileio_closefd(self):
# Issue #4841 # Issue #4841
with self.open(__file__, 'rb') as f1, \ with self.open(__file__, 'rb') as f1, \
......
...@@ -10,6 +10,9 @@ Release date: tba ...@@ -10,6 +10,9 @@ Release date: tba
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #27066: Fixed SystemError if a custom opener (for open()) returns a
negative number without setting an exception.
- Issue #20041: Fixed TypeError when frame.f_trace is set to None. - Issue #20041: Fixed TypeError when frame.f_trace is set to None.
Patch by Xavier de Gaye. Patch by Xavier de Gaye.
......
...@@ -421,7 +421,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, ...@@ -421,7 +421,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
self->fd = _PyLong_AsInt(fdobj); self->fd = _PyLong_AsInt(fdobj);
Py_DECREF(fdobj); Py_DECREF(fdobj);
if (self->fd == -1) { if (self->fd < 0) {
if (!PyErr_Occurred()) {
/* The opener returned -1. See issue #27066 */
PyErr_Format(PyExc_ValueError,
"opener returned %d", self->fd);
}
goto error; goto error;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment