Skip to content

bpo-45711: assert that the type of exc_info is redundant #29518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)));
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down