[Python-checkins] cpython (3.5): Issue #27066: Fixed SystemError if a custom opener (for open()) returns

barry.warsaw python-checkins at python.org
Wed Jun 8 17:57:35 EDT 2016


https://hg.python.org/cpython/rev/4af64ab34eef
changeset:   101811:4af64ab34eef
branch:      3.5
parent:      101808:640c0b6a273d
user:        Barry Warsaw <barry at python.org>
date:        Wed Jun 08 17:47:26 2016 -0400
summary:
  Issue #27066: Fixed SystemError if a custom opener (for open()) returns
a negative number without setting an exception.

files:
  Lib/test/test_io.py  |  16 ++++++++++++++++
  Misc/NEWS            |   3 +++
  Modules/_io/fileio.c |   7 ++++++-
  3 files changed, 25 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -805,6 +805,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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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 #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
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -421,7 +421,12 @@
 
             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;
             }
         }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list