diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2021-11-12 21:37:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-12 21:37:26 (GMT) |
commit | de3db1448b1b983eeb9f4498d07e3d2f1fb6d29d (patch) | |
tree | ab8d4fd5b86385e32e4739a2488e8cf348ceccc5 | |
parent | 9d3271438431c37c29c359f5b73e8094b5f82912 (diff) | |
download | cpython-de3db1448b1b983eeb9f4498d07e3d2f1fb6d29d.zip cpython-de3db1448b1b983eeb9f4498d07e3d2f1fb6d29d.tar.gz cpython-de3db1448b1b983eeb9f4498d07e3d2f1fb6d29d.tar.bz2 |
bpo-45711: assert that the type of exc_info is redundant (GH-29518)
-rw-r--r-- | Python/ceval.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 030d983..0b24cbd 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1058,6 +1058,23 @@ fail: 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) @@ -2476,6 +2493,7 @@ check_eval_breaker: 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); @@ -2497,6 +2515,7 @@ check_eval_breaker: 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; @@ -2506,6 +2525,7 @@ check_eval_breaker: 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); @@ -2528,6 +2548,7 @@ check_eval_breaker: 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; @@ -2537,6 +2558,7 @@ check_eval_breaker: 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); @@ -3991,6 +4013,7 @@ check_eval_breaker: 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))); @@ -4009,6 +4032,7 @@ check_eval_breaker: 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); @@ -4990,6 +5014,7 @@ exception_unwind: PUSH(tb); PUSH(val); PUSH(exc); + ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val); JUMPTO(handler); /* Resume normal execution */ frame->f_state = FRAME_EXECUTING; |