8000 gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives by iritkatriel · Pull Request #102619 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives #102619

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

Closed
wants to merge 12 commits into from
Closed
Changes from 1 commit
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
Next Next commit
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternat…
…ives
  • Loading branch information
iritkatriel committed Mar 11, 2023
commit abeb903acaaab3dad192e04319535a9c7b041f9f
42 changes: 19 additions & 23 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_moduleobject.h" // PyModuleObject
#include "pycore_opcode.h" // EXTRA_CASES
#include "pycore_pyerrors.h" // _PyErr_Fetch(), _PyErr_GetRaisedException()
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_range.h" // _PyRangeIterObject
Expand Down Expand Up @@ -1783,18 +1783,15 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
if (exc == NULL) {
/* Reraise */
_PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
value = exc_info->exc_value;
if (Py_IsNone(value) || value == NULL) {
exc = exc_info->exc_value;
if (exc == NULL) {
_PyErr_SetString(tstate, PyExc_RuntimeError,
"No active exception to reraise");
return 0;
}
assert(PyExceptionInstance_Check(value));
type = PyExceptionInstance_Class(value);
Py_XINCREF(type);
Py_XINCREF(value);
PyObject *tb = PyException_GetTraceback(value); /* new ref */
_PyErr_Restore(tstate, type, value, tb);
Py_INCREF(exc);
assert(PyExceptionInstance_Check(exc));
_PyErr_SetRaisedException(tstate, exc);
return 1;
}

Expand Down Expand Up @@ -2035,28 +2032,27 @@ call_exc_trace(Py_tracefunc func, PyObject *self,
PyThreadState *tstate,
_PyInterpreterFrame *f)
{
PyObject *type, *value, *traceback, *orig_traceback, *arg;
int err;
_PyErr_Fetch(tstate, &type, &value, &orig_traceback);
if (value == NULL) {
value = Py_NewRef(Py_None);
PyObject *exc = _PyErr_GetRaisedException(tstate);
assert(exc && PyExceptionInstance_Check(exc));
PyObject *type = PyExceptionInstance_Class(exc);
PyObject *traceback = PyException_GetTraceback(exc);
if (traceback == NULL) {
traceback = Py_NewRef(Py_None);
}
_PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
arg = PyTuple_Pack(3, type, value, traceback);
PyObject *arg = PyTuple_Pack(3, type, exc, traceback);
Py_XDECREF(traceback);

if (arg == NULL) {
_PyErr_Restore(tstate, type, value, orig_traceback);
_PyErr_SetRaisedException(tstate, exc);
return;
}
err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
int err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Py_DECREF(arg);
if (err == 0) {
_PyErr_Restore(tstate, type, value, orig_traceback);
_PyErr_SetRaisedException(tstate, exc);
}
else {
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(orig_traceback);
Py_XDECREF(exc);
}
}

Expand Down
0