summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorNoah <33094578+coolreader18@users.noreply.github.com>2021-08-03 02:17:18 (GMT)
committerGitHub <noreply@github.com>2021-08-03 02:17:18 (GMT)
commit83ca46b7784b7357d82ec47b33295e09ed7380cb (patch)
tree30c63b0b1f0a26e65dbe0954ad7065bdd795c566 /Python
parent54f185b6d321a6354aef2b2886c766677f487ecb (diff)
downloadcpython-83ca46b7784b7357d82ec47b33295e09ed7380cb.zip
cpython-83ca46b7784b7357d82ec47b33295e09ed7380cb.tar.gz
cpython-83ca46b7784b7357d82ec47b33295e09ed7380cb.tar.bz2
closes bpo-39091: Fix segfault when Exception constructor returns non-exception for gen.throw. (#17658)
Co-authored-by: Benjamin Peterson <benjamin@python.org>
Diffstat (limited to 'Python')
-rw-r--r--Python/errors.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 1f84215..eeb84e8 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