From 0b551db04a99a97abb1e44a071c688c3ca704b67 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 3 Aug 2021 03:10:54 -0700 Subject: bpo-39091: Fix segfault when Exception constructor returns non-exception for gen.throw. (GH-17658) (GH-27573) Co-authored-by: Benjamin Peterson (cherry picked from commit 83ca46b7784b7357d82ec47b33295e09ed7380cb) Co-authored-by: Noah <33094578+coolreader18@users.noreply.github.com> --- Python/errors.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 87af39d527..2c020cd266 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -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 -- cgit v1.2.1