changeset: 101812:84c91d7d4667 parent: 101810:09ec7f7322b5 parent: 101811:4af64ab34eef user: Barry Warsaw date: Wed Jun 08 17:54:43 2016 -0400 files: Lib/test/test_io.py Misc/NEWS Modules/_io/fileio.c description: Issue #27066: Fixed SystemError if a custom opener (for open()) returns a negative number without setting an exception. diff -r 09ec7f7322b5 -r 84c91d7d4667 Lib/test/test_io.py --- a/Lib/test/test_io.py Wed Jun 08 14:37:05 2016 -0400 +++ b/Lib/test/test_io.py Wed Jun 08 17:54:43 2016 -0400 @@ -809,6 +809,22 @@ with self.open("non-existent", "r", opener=opener) as f: 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): # Issue #4841 with self.open(__file__, 'rb') as f1, \ diff -r 09ec7f7322b5 -r 84c91d7d4667 Misc/NEWS --- a/Misc/NEWS Wed Jun 08 14:37:05 2016 -0400 +++ b/Misc/NEWS Wed Jun 08 17:54:43 2016 -0400 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #27066: Fixed SystemError if a custom opener (for open()) returns a + negative number without setting an exception. + - Issue #26983: float() now always return an instance of exact float. The deprecation warning is emitted if __float__ returns an instance of a strict subclass of float. In a future versions of Python this can diff -r 09ec7f7322b5 -r 84c91d7d4667 Modules/_io/fileio.c --- a/Modules/_io/fileio.c Wed Jun 08 14:37:05 2016 -0400 +++ b/Modules/_io/fileio.c Wed Jun 08 17:54:43 2016 -0400 @@ -420,7 +420,13 @@ self->fd = _PyLong_AsInt(fdobj); Py_DECREF(fdobj); - if (self->fd == -1) { + if (self->fd < 0) { + if (!PyErr_Occurred()) { + /* The opener returned a negative but didn't set an + exception. See issue #27066 */ + PyErr_Format(PyExc_ValueError, + "opener returned %d", self->fd); + } goto error; } }