]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39091: Fix segfault when Exception constructor returns non-exception for gen...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 3 Aug 2021 10:10:54 +0000 (03:10 -0700)
committerGitHub <noreply@github.com>
Tue, 3 Aug 2021 10:10:54 +0000 (12:10 +0200)
Co-authored-by: Benjamin Peterson <benjamin@python.org>
(cherry picked from commit 83ca46b7784b7357d82ec47b33295e09ed7380cb)

Co-authored-by: Noah <33094578+coolreader18@users.noreply.github.com>
Lib/test/test_generators.py
Misc/ACKS
Misc/NEWS.d/next/Core and Builtins/2019-12-21-14-18-32.bpo-39091.dOexgQ.rst [new file with mode: 0644]
Python/errors.c

index 3bf152280868e8a0102582749cf3bca19ad1e47e..17fb87acc8f67762b3bed50bdfacb23ec9f0f5c4 100644 (file)
@@ -270,6 +270,32 @@ class ExceptionTest(unittest.TestCase):
         self.assertEqual(next(g), "done")
         self.assertEqual(sys.exc_info(), (None, None, None))
 
+    def test_except_throw_bad_exception(self):
+        class E(Exception):
+            def __new__(cls, *args, **kwargs):
+                return cls
+
+        def boring_generator():
+            yield
+
+        gen = boring_generator()
+
+        err_msg = 'should have returned an instance of BaseException'
+
+        with self.assertRaisesRegex(TypeError, err_msg):
+            gen.throw(E)
+
+        self.assertRaises(StopIteration, next, gen)
+
+        def generator():
+            with self.assertRaisesRegex(TypeError, err_msg):
+                yield
+
+        gen = generator()
+        next(gen)
+        with self.assertRaises(StopIteration):
+            gen.throw(E)
+
     def test_stopiteration_error(self):
         # See also PEP 479.
 
index fc5602d39b65af9d5d09f3259dda976137ef4621..59ae1462b86a24215a7c34125d4e6f89c7a20b51 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1279,6 +1279,7 @@ Peter Otten
 Michael Otteneder
 Richard Oudkerk
 Russel Owen
+Noah Oxer
 Joonas Paalasmaa
 Martin Packman
 Shriphani Palakodety
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-21-14-18-32.bpo-39091.dOexgQ.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-21-14-18-32.bpo-39091.dOexgQ.rst
new file mode 100644 (file)
index 0000000..c3b4e81
--- /dev/null
@@ -0,0 +1 @@
+Fix crash when using passing a non-exception to a generator's ``throw()`` method. Patch by Noah Oxer
index 87af39d527a512668dfc16ab44baf0da01e3a6af..2c020cd2660c539acb6d3318fbd6e3d45b395731 100644 (file)
@@ -85,17 +85,29 @@ _PyErr_GetTopmostException(PyThreadState *tstate)
 }
 
 static PyObject*
-_PyErr_CreateException(PyObject *exception, PyObject *value)
+_PyErr_CreateException(PyObject *exception_type, PyObject *value)
 {
+    PyObject *exc;
+
     if (value == NULL || value == Py_None) {
-        return _PyObject_CallNoArg(exception);
+        exc = _PyObject_CallNoArg(exception_type);
     }
     else if (PyTuple_Check(value)) {
-        return PyObject_Call(exception, value, NULL);
+        exc = PyObject_Call(exception_type, value, NULL);
     }
     else {
-        return PyObject_CallOneArg(exception, value);
+        exc = PyObject_CallOneArg(exception_type, value);
+    }
+
+    if (exc != NULL && !PyExceptionInstance_Check(exc)) {
+        PyErr_Format(PyExc_TypeError,
+                     "calling %R should have returned an instance of "
+                     "BaseException, not %s",
+                     exception_type, Py_TYPE(exc)->tp_name);
+        Py_CLEAR(exc);
     }
+
+    return exc;
 }
 
 void