]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #27066: Fixed SystemError if a custom opener (for open()) returns
authorBarry Warsaw <barry@python.org>
Wed, 8 Jun 2016 21:47:26 +0000 (17:47 -0400)
committerBarry Warsaw <barry@python.org>
Wed, 8 Jun 2016 21:47:26 +0000 (17:47 -0400)
a negative number without setting an exception.

Lib/test/test_io.py
Misc/NEWS
Modules/_io/fileio.c

index 73231981adebbf8a5352876bf1e208b719d5806f..000b525003be7525b15c2352605a9fbced56b6af 100644 (file)
@@ -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, \
index 1e5f7d328ed14be99fa9992bf3e2b84d45ed92d6..e16fa196da7a2407772399baa105a8003ca2c07d 100644 (file)
--- 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.
 
index 83b6a324efceeaaa182066c855bc11646dd10695..3d41d81179977173c2562abe4603708032897d54 100644 (file)
@@ -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;
             }
         }