diff --git a/Python/ceval.c b/Python/ceval.c index fadb97adefb2b4..7451fc38637c97 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1060,6 +1060,23 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type, static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **); +#ifdef Py_DEBUG +static void +_assert_exception_type_is_redundant(PyObject* type, PyObject* val) +{ + if (type == NULL || type == Py_None) { + assert(val == NULL || val == Py_None); + } + else { + assert(PyExceptionInstance_Check(val)); + assert(PyExceptionInstance_Class(val) == type); + } +} + +#define ASSERT_EXC_TYPE_IS_REDUNDANT(t, v) _assert_exception_type_is_redundant(t, v) +#else +#define ASSERT_EXC_TYPE_IS_REDUNDANT(t, v) +#endif PyObject * PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) @@ -2847,6 +2864,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr exc_info->exc_type = POP(); exc_info->exc_value = POP(); exc_info->exc_traceback = POP(); + ASSERT_EXC_TYPE_IS_REDUNDANT(exc_info->exc_type, exc_info->exc_value); Py_XDECREF(type); Py_XDECREF(value); Py_XDECREF(traceback); @@ -2868,6 +2886,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr type = POP(); value = POP(); traceback = POP(); + ASSERT_EXC_TYPE_IS_REDUNDANT(type, value); Py_DECREF(POP()); /* lasti */ _PyErr_Restore(tstate, type, value, traceback); exc_info = tstate->exc_info; @@ -2877,6 +2896,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr exc_info->exc_type = POP(); exc_info->exc_value = POP(); exc_info->exc_traceback = POP(); + ASSERT_EXC_TYPE_IS_REDUNDANT(exc_info->exc_type, exc_info->exc_value); Py_XDECREF(type); Py_XDECREF(value); Py_XDECREF(traceback); @@ -2899,6 +2919,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr PyObject *exc = POP(); PyObject *val = POP(); PyObject *tb = POP(); + ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val); assert(PyExceptionClass_Check(exc)); _PyErr_Restore(tstate, exc, val, tb); goto exception_unwind; @@ -2908,6 +2929,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr PyObject *exc = POP(); PyObject *val = POP(); PyObject *tb = POP(); + ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val); assert(PyExceptionClass_Check(exc)); if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { Py_DECREF(exc); @@ -4362,6 +4384,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr exc = TOP(); val = SECOND(); tb = THIRD(); + ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val); assert(!Py_IsNone(exc)); assert(!PyLong_Check(exc)); assert(PyLong_Check(PEEK(7))); @@ -4380,6 +4403,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr PyObject *type = TOP(); PyObject *value = SECOND(); PyObject *tb = THIRD(); + ASSERT_EXC_TYPE_IS_REDUNDANT(type, value); _PyErr_StackItem *exc_info = tstate->exc_info; SET_THIRD(exc_info->exc_traceback); SET_SECOND(exc_info->exc_value); @@ -5238,6 +5262,7 @@ MISS_WITH_OPARG_COUNTER(BINARY_MULTIPLY) PUSH(tb); PUSH(val); PUSH(exc); + ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val); JUMPTO(handler); /* Resume normal execution */ frame->f_state = FRAME_EXECUTING;