Skip to content

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

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 1 commit into from
Mar 13, 2023
Merged
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
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 (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 @@ -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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy