bpo-45711: assert that the type of exc_info is redundant (GH-29518) · python/cpython@de3db14

@@ -1058,6 +1058,23 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,

10581058

static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);

10591059

static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);

106010601061+

#ifdef Py_DEBUG

1062+

static void

1063+

_assert_exception_type_is_redundant(PyObject* type, PyObject* val)

1064+

{

1065+

if (type == NULL || type == Py_None) {

1066+

assert(val == NULL || val == Py_None);

1067+

}

1068+

else {

1069+

assert(PyExceptionInstance_Check(val));

1070+

assert(PyExceptionInstance_Class(val) == type);

1071+

}

1072+

}

1073+1074+

#define ASSERT_EXC_TYPE_IS_REDUNDANT(t, v) _assert_exception_type_is_redundant(t, v)

1075+

#else

1076+

#define ASSERT_EXC_TYPE_IS_REDUNDANT(t, v)

1077+

#endif

1061107810621079

PyObject *

10631080

PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)

@@ -2476,6 +2493,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

24762493

exc_info->exc_type = POP();

24772494

exc_info->exc_value = POP();

24782495

exc_info->exc_traceback = POP();

2496+

ASSERT_EXC_TYPE_IS_REDUNDANT(exc_info->exc_type, exc_info->exc_value);

24792497

Py_XDECREF(type);

24802498

Py_XDECREF(value);

24812499

Py_XDECREF(traceback);

@@ -2497,6 +2515,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

24972515

type = POP();

24982516

value = POP();

24992517

traceback = POP();

2518+

ASSERT_EXC_TYPE_IS_REDUNDANT(type, value);

25002519

Py_DECREF(POP()); /* lasti */

25012520

_PyErr_Restore(tstate, type, value, traceback);

25022521

exc_info = tstate->exc_info;

@@ -2506,6 +2525,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

25062525

exc_info->exc_type = POP();

25072526

exc_info->exc_value = POP();

25082527

exc_info->exc_traceback = POP();

2528+

ASSERT_EXC_TYPE_IS_REDUNDANT(exc_info->exc_type, exc_info->exc_value);

25092529

Py_XDECREF(type);

25102530

Py_XDECREF(value);

25112531

Py_XDECREF(traceback);

@@ -2528,6 +2548,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

25282548

PyObject *exc = POP();

25292549

PyObject *val = POP();

25302550

PyObject *tb = POP();

2551+

ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val);

25312552

assert(PyExceptionClass_Check(exc));

25322553

_PyErr_Restore(tstate, exc, val, tb);

25332554

goto exception_unwind;

@@ -2537,6 +2558,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

25372558

PyObject *exc = POP();

25382559

PyObject *val = POP();

25392560

PyObject *tb = POP();

2561+

ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val);

25402562

assert(PyExceptionClass_Check(exc));

25412563

if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {

25422564

Py_DECREF(exc);

@@ -3991,6 +4013,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

39914013

exc = TOP();

39924014

val = SECOND();

39934015

tb = THIRD();

4016+

ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val);

39944017

assert(!Py_IsNone(exc));

39954018

assert(!PyLong_Check(exc));

39964019

assert(PyLong_Check(PEEK(7)));

@@ -4009,6 +4032,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

40094032

PyObject *type = TOP();

40104033

PyObject *value = SECOND();

40114034

PyObject *tb = THIRD();

4035+

ASSERT_EXC_TYPE_IS_REDUNDANT(type, value);

40124036

_PyErr_StackItem *exc_info = tstate->exc_info;

40134037

SET_THIRD(exc_info->exc_traceback);

40144038

SET_SECOND(exc_info->exc_value);

@@ -4990,6 +5014,7 @@ MISS_WITH_OPARG_COUNTER(BINARY_SUBSCR)

49905014

PUSH(tb);

49915015

PUSH(val);

49925016

PUSH(exc);

5017+

ASSERT_EXC_TYPE_IS_REDUNDANT(exc, val);

49935018

JUMPTO(handler);

49945019

/* Resume normal execution */

49955020

frame->f_state = FRAME_EXECUTING;