Skip to content

Commit

Permalink
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient al…
Browse files Browse the repository at this point in the history
…ternatives (python#102631)
  • Loading branch information
iritkatriel authored and Fidget-Spinner committed Mar 27, 2023
1 parent 1359271 commit 80fa4ed
Showing 1 changed file with 19 additions and 23 deletions.
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 @@ -1791,18 +1791,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 (Py_IsNone(exc) || 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 @@ -2043,28 +2040,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 comments on commit 80fa4ed

Please sign in to comment.