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

iritkatriel webhook-mailer at python.org
Fri Nov 12 16:37:37 EST 2021


https://github.com/python/cpython/commit/de3db1448b1b983eeb9f4498d07e3d2f1fb6d29d
commit: de3db1448b1b983eeb9f4498d07e3d2f1fb6d29d
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-11-12T21:37:26Z
summary:

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

files:
M Python/ceval.c

diff --git a/Python/ceval.c b/Python/ceval.c
index 030d98396780b..0b24cbdcc2cc4 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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;



More information about the Python-checkins mailing list