]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111375: Use `NULL` rather than `None` in the exception stack to indicate that...
authorCarey Metcalfe <carey@cmetcalfe.ca>
Thu, 21 Dec 2023 01:46:41 +0000 (20:46 -0500)
committerGitHub <noreply@github.com>
Thu, 21 Dec 2023 01:46:41 +0000 (01:46 +0000)
Misc/NEWS.d/next/Core and Builtins/2023-12-19-22-03-43.gh-issue-111375.M9vuA6.rst [new file with mode: 0644]
Objects/frameobject.c
Python/bytecodes.c
Python/errors.c
Python/executor_cases.c.h
Python/generated_cases.c.h

diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-19-22-03-43.gh-issue-111375.M9vuA6.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-19-22-03-43.gh-issue-111375.M9vuA6.rst
new file mode 100644 (file)
index 0000000..fbb5171
--- /dev/null
@@ -0,0 +1,2 @@
+Only use ``NULL`` in the exception stack to indicate an exception was
+handled. Patch by Carey Metcalfe.
index be330a775872c2df98af4c82f9332800347d2138..cafe4ef6141d9af620b4f417443f3cd3106d101c 100644 (file)
@@ -811,7 +811,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
             PyObject *exc = _PyFrame_StackPop(f->f_frame);
             assert(PyExceptionInstance_Check(exc) || exc == Py_None);
             PyThreadState *tstate = _PyThreadState_GET();
-            Py_XSETREF(tstate->exc_info->exc_value, exc);
+            Py_XSETREF(tstate->exc_info->exc_value, exc == Py_None ? NULL : exc);
         }
         else {
             PyObject *v = _PyFrame_StackPop(f->f_frame);
index 82d7a71d4989a4ce7ecd6a5977ec7a125af7aa24..29e1dab184ef4e139b6509e971234cb810461b3a 100644 (file)
@@ -1100,7 +1100,7 @@ dummy_func(
 
         inst(POP_EXCEPT, (exc_value -- )) {
             _PyErr_StackItem *exc_info = tstate->exc_info;
-            Py_XSETREF(exc_info->exc_value, exc_value);
+            Py_XSETREF(exc_info->exc_value, exc_value == Py_None ? NULL : exc_value);
         }
 
         inst(RERAISE, (values[oparg], exc -- values[oparg])) {
index ed5eec5c261970e005012748e0d0c3da40730df9..e5f176a5dd208e4d99f724af4b1db8d21693dee6 100644 (file)
@@ -121,11 +121,11 @@ _PyErr_GetTopmostException(PyThreadState *tstate)
     _PyErr_StackItem *exc_info = tstate->exc_info;
     assert(exc_info);
 
-    while ((exc_info->exc_value == NULL || exc_info->exc_value == Py_None) &&
-           exc_info->previous_item != NULL)
+    while (exc_info->exc_value == NULL && exc_info->previous_item != NULL)
     {
         exc_info = exc_info->previous_item;
     }
+    assert(!Py_IsNone(exc_info->exc_value));
     return exc_info;
 }
 
@@ -592,7 +592,7 @@ PyErr_GetHandledException(void)
 void
 _PyErr_SetHandledException(PyThreadState *tstate, PyObject *exc)
 {
-    Py_XSETREF(tstate->exc_info->exc_value, Py_XNewRef(exc));
+    Py_XSETREF(tstate->exc_info->exc_value, Py_XNewRef(exc == Py_None ? NULL : exc));
 }
 
 void
index 7cc29c8e644d8d6b96b3585507d69bd3e59696b6..69adb20ed4659576535b9f68d4557a1e68e1e4c3 100644 (file)
             PyObject *exc_value;
             exc_value = stack_pointer[-1];
             _PyErr_StackItem *exc_info = tstate->exc_info;
-            Py_XSETREF(exc_info->exc_value, exc_value);
+            Py_XSETREF(exc_info->exc_value, exc_value == Py_None ? NULL : exc_value);
             stack_pointer += -1;
             break;
         }
index e935f33fa2131ae2b41eb93a81b982d03c7476b4..f94dc6164dde2559f6eaf1f2baa3c89da922cb04 100644 (file)
             PyObject *exc_value;
             exc_value = stack_pointer[-1];
             _PyErr_StackItem *exc_info = tstate->exc_info;
-            Py_XSETREF(exc_info->exc_value, exc_value);
+            Py_XSETREF(exc_info->exc_value, exc_value == Py_None ? NULL : exc_value);
             stack_pointer += -1;
             DISPATCH();
         }