Skip to content

gh-111375: Use NULL in the exception stack to indicate an exception was handled #113302

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
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Only use ``NULL`` in the exception stack to indicate an exception was
handled. Patch by Carey Metcalfe.
2 changes: 1 addition & 1 deletion Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
6 changes: 3 additions & 3 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.