10000 GH-101578: Normalize the current exception by markshannon · Pull Request #101607 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-101578: Normalize the current exception #101607

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 13 commits into from
Feb 8, 2023
Prev Previous commit
Next Next commit
Refactor assertion.
  • Loading branch information
markshannon committed Feb 6, 2023
commit 508850f3840a123dd926f37ebe2a95cefaa7f81f
30 changes: 14 additions & 16 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
const char *format, va_list vargs);

void
ASSERT_EXCEPTION_NORMALIZED(PyObject *type, PyObject *value,
_PyErr_Restore1(PyThreadState *tstate, PyObject *exc)
{
PyObject *old_exc = tstate->current_exception;
tstate->current_exception = exc;
Py_XDECREF(old_exc);
}

void
_PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
PyObject *traceback)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's currently a requirement to pass in only normalised exceptions. This could break C extensions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add some extra logic. It will slow things down a bit, but safety first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could make _PyErr_Restore to expectonly normalised exceptions, but normalise them in PyErr_Restore?

/* Exceptions are normalized if all NULL,
#ifdef Py_DEBUG
/* Check that we are being passed a normalized exception.
*
* Exceptions are normalized if all NULL,
* or if curexc_type = Py_TYPE(curexc_value) and
* curexc_traceback = curexc_value->traceback
* and both type and traceback are valid */
Expand All @@ -48,21 +59,8 @@ ASSERT_EXCEPTION_NORMALIZED(PyObject *type, PyObject *value,
((PyBaseExceptionObject *)value)->traceback == NULL)
);
}
}

void
_PyErr_Restore1(PyThreadState *tstate, PyObject *exc)
{
PyObject *old_exc = tstate->current_exception;
tstate->current_exception = exc;
Py_XDECREF(old_exc);
}
#endif

void
_PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
PyObject *traceback)
{
ASSERT_EXCEPTION_NORMALIZED(type, value, traceback);
_PyErr_Restore1(tstate, value);
Py_XDECREF(type);
Py_XDECREF(traceback);
Expand Down
0