bpo-45711: assert that the type of exc_info is redundant (GH-29518)

This commit is contained in:
Irit Katriel 2021-11-12 21:37:26 +00:00 committed by GitHub
parent 9d32714384
commit de3db1448b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1058,6 +1058,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)
@ -2476,6 +2493,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);
@ -2497,6 +2515,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;
@ -2506,6 +2525,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);
@ -2528,6 +2548,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;
@ -2537,6 +2558,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);
@ -3991,6 +4013,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)));
@ -4009,6 +4032,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);
@ -4990,6 +5014,7 @@ MISS_WITH_OPARG_COUNTER(BINARY_SUBSCR)
PUSH(tb);
PUSH(val);
PUSH(exc);
ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val);
JUMPTO(handler);
/* Resume normal execution */
frame->f_state = FRAME_EXECUTING;