Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 480e285

Browse files
committed
Issue #27066: Fixed SystemError if a custom opener (for open()) returns
a negative number without setting an exception.
1 parent fda4d67 commit 480e285

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

Lib/test/test_io.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,22 @@ def opener(path, flags):
805805
with self.open("non-existent", "r", opener=opener) as f:
806806
self.assertEqual(f.read(), "egg\n")
807807

808+
def test_bad_opener_negative_1(self):
809+
# Issue #27066.
810+
def badopener(fname, flags):
811+
return -1
812+
with self.assertRaises(ValueError) as cm:
813+
open('non-existent', 'r', opener=badopener)
814+
self.assertEqual(str(cm.exception), 'opener returned -1')
815+
816+
def test_bad_opener_other_negative(self):
817+
# Issue #27066.
818+
def badopener(fname, flags):
819+
return -2
820+
with self.assertRaises(ValueError) as cm:
821+
open('non-existent', 'r', opener=badopener)
822+
self.assertEqual(str(cm.exception), 'opener returned -2')
823+
808824
def test_fileio_closefd(self):
809825
# Issue #4841
810826
with self.open(__file__, 'rb') as f1, \

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #27066: Fixed SystemError if a custom opener (for open()) returns a
14+
negative number without setting an exception.
15+
1316
- Issue #20041: Fixed TypeError when frame.f_trace is set to None.
1417
Patch by Xavier de Gaye.
1518

Modules/_io/fileio.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
421421

422422
self->fd = _PyLong_AsInt(fdobj);
423423
Py_DECREF(fdobj);
424-
if (self->fd == -1) {
424+
if (self->fd < 0) {
425+
if (!PyErr_Occurred()) {
426+
/* The opener returned -1. See issue #27066 */
427+
PyErr_Format(PyExc_ValueError,
428+
"opener returned %d", self->fd);
429+
}
425430
goto error;
426431
}
427432
}

0 commit comments

Comments
 (0)