summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-08-03 10:10:38 (GMT)
committerGitHub <noreply@github.com>2021-08-03 10:10:38 (GMT)
commit8ce7f2f4ef04e19209f1dfd2a0cf50ddcd0e999f (patch)
treeff2643fb5de8c15ad3b1b123be5abc338df0bc4e /Python/errors.c
parent2b8d4eaec9a8f7e022295efd86105b19e359d2a4 (diff)
downloadcpython-8ce7f2f4ef04e19209f1dfd2a0cf50ddcd0e999f.zip
cpython-8ce7f2f4ef04e19209f1dfd2a0cf50ddcd0e999f.tar.gz
cpython-8ce7f2f4ef04e19209f1dfd2a0cf50ddcd0e999f.tar.bz2
bpo-39091: Fix segfault when Exception constructor returns non-exception for gen.throw. (GH-17658) (GH-27572)
Co-authored-by: Benjamin Peterson <benjamin@python.org> (cherry picked from commit 83ca46b7784b7357d82ec47b33295e09ed7380cb) Co-authored-by: Noah <33094578+coolreader18@users.noreply.github.com>
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/Python/errors.c b/Python/errors.c
index f743d30..d4b9db1 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