summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2016-06-08 21:47:26 (GMT)
committerBarry Warsaw <barry@python.org>2016-06-08 21:47:26 (GMT)
commit480e28538d42b9cf8e8b2163bfa9e23e27b80793 (patch)
treebd028ce2b5a97755fc3aec27208fc0b2067ebe1a
parentfda4d67842a406d2fa285f929ddf890c2393e1c2 (diff)
downloadcpython-480e28538d42b9cf8e8b2163bfa9e23e27b80793.zip
cpython-480e28538d42b9cf8e8b2163bfa9e23e27b80793.tar.gz
cpython-480e28538d42b9cf8e8b2163bfa9e23e27b80793.tar.bz2
Issue #27066: Fixed SystemError if a custom opener (for open()) returns
a negative number without setting an exception.
-rw-r--r--Lib/test/test_io.py16
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_io/fileio.c7
3 files changed, 25 insertions, 1 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 7323198..000b525 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -805,6 +805,22 @@ class IOTest(unittest.TestCase):
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 --git a/Misc/NEWS b/Misc/NEWS
index 1e5f7d3..e16fa19 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: tba
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.
Patch by Xavier de Gaye.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 83b6a32..3d41d81 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -421,7 +421,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
self->fd = _PyLong_AsInt(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;
}
}