gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Python/) by iritkatriel · Pull Request #102193 · python/cpython · GitHub
Skip to content
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
2 changes: 1 addition & 1 deletion Python/bytecodes.c
49 changes: 16 additions & 33 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()
#include "pycore_pyerrors.h" // _PyErr_Fetch(), _PyErr_GetRaisedException()
Comment thread
iritkatriel marked this conversation as resolved.
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_range.h" // _PyRangeIterObject
Expand Down Expand Up @@ -105,8 +105,7 @@ static void
dump_stack(_PyInterpreterFrame *frame, PyObject **stack_pointer)
{
PyObject **stack_base = _PyFrame_Stackbase(frame);
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyObject *exc = PyErr_GetRaisedException();
printf(" stack=[");
for (PyObject **ptr = stack_base; ptr < stack_pointer; ptr++) {
if (ptr != stack_base) {
Expand All @@ -120,7 +119,7 @@ dump_stack(_PyInterpreterFrame *frame, PyObject **stack_pointer)
}
printf("]\n");
fflush(stdout);
PyErr_Restore(type, value, traceback);
PyErr_SetRaisedException(exc);
}

static void
Expand Down Expand Up @@ -157,8 +156,7 @@ lltrace_resume_frame(_PyInterpreterFrame *frame)
return;
}
PyFunctionObject *f = (PyFunctionObject *)fobj;
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyObject *exc = PyErr_GetRaisedException();
PyObject *name = f->func_qualname;
if (name == NULL) {
name = f->func_name;
Expand All @@ -178,7 +176,7 @@ lltrace_resume_frame(_PyInterpreterFrame *frame)
}
printf("\n");
fflush(stdout);
PyErr_Restore(type, value, traceback);
PyErr_SetRaisedException(exc);
}
#endif
static int call_trace(Py_tracefunc, PyObject *,
Expand Down Expand Up @@ -1032,7 +1030,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
PyObject *v = POP();
Py_XDECREF(v);
}
PyObject *exc, *val, *tb;
if (lasti) {
int frame_lasti = _PyInterpreterFrame_LASTI(frame);
PyObject *lasti = PyLong_FromLong(frame_lasti);
Expand All @@ -1041,19 +1038,12 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}
PUSH(lasti);
}
_PyErr_Fetch(tstate, &exc, &val, &tb);

/* Make the raw exception data
available to the handler,
so a program can emulate the
Python main loop. */
_PyErr_NormalizeException(tstate, &exc, &val, &tb);
if (tb != NULL)
PyException_SetTraceback(val, tb);
else
PyException_SetTraceback(val, Py_None);
Py_XDECREF(tb);
Py_XDECREF(exc);
PUSH(val);
PUSH(_PyErr_GetRaisedException(tstate));
JUMPTO(handler);
/* Resume normal execution */
DISPATCH();
Expand Down Expand Up @@ -2075,19 +2065,15 @@ call_trace_protected(Py_tracefunc func, PyObject *obj,
PyThreadState *tstate, _PyInterpreterFrame *frame,
int what, PyObject *arg)
{
PyObject *type, *value, *traceback;
int err;
_PyErr_Fetch(tstate, &type, &value, &traceback);
err = call_trace(func, obj, tstate, frame, what, arg);
PyObject *exc = _PyErr_GetRaisedException(tstate);
int err = call_trace(func, obj, tstate, frame, what, arg);
if (err == 0)
{
_PyErr_Restore(tstate, type, value, traceback);
_PyErr_SetRaisedException(tstate, exc);
return 0;
}
else {
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(traceback);
Py_XDECREF(exc);
return -1;
}
}
Expand Down Expand Up @@ -2935,18 +2921,15 @@ format_exc_check_arg(PyThreadState *tstate, PyObject *exc,

if (exc == PyExc_NameError) {
// Include the name in the NameError exceptions to offer suggestions later.
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
PyNameErrorObject* exc = (PyNameErrorObject*) value;
if (exc->name == NULL) {
PyObject *exc = PyErr_GetRaisedException();
if (PyErr_GivenExceptionMatches(exc, PyExc_NameError)) {
if (((PyNameErrorObject*)exc)->name == NULL) {
// We do not care if this fails because we are going to restore the
// NameError anyway.
(void)PyObject_SetAttr(value, &_Py_ID(name), obj);
(void)PyObject_SetAttr(exc, &_Py_ID(name), obj);
}
}
PyErr_Restore(type, value, traceback);
PyErr_SetRaisedException(exc);
}
}

Expand Down
7 changes: 3 additions & 4 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "Python.h"
#include "pycore_atomic.h" // _Py_atomic_int
#include "pycore_ceval.h" // _PyEval_SignalReceived()
#include "pycore_pyerrors.h" // _PyErr_Fetch()
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pylifecycle.h" // _PyErr_Print()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_interp.h" // _Py_RunGC()
Expand Down Expand Up @@ -870,10 +870,9 @@ _Py_FinishPendingCalls(PyThreadState *tstate)
}

if (make_pending_calls(tstate->interp) < 0) {
PyObject *exc, *val, *tb;
_PyErr_Fetch(tstate, &exc, &val, &tb);
PyObject *exc = _PyErr_GetRaisedException(tstate);
PyErr_BadInternalCall();
_PyErr_ChainExceptions(exc, val, tb);
_PyErr_ChainExceptions1(exc);
_PyErr_Print(tstate);
}
}
Expand Down
5 changes: 2 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1633,8 +1633,7 @@ static void
compiler_exit_scope(struct compiler *c)
{
// Don't call PySequence_DelItem() with an exception raised
PyObject *exc_type, *exc_val, *exc_tb;
PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
PyObject *exc = PyErr_GetRaisedException();

c->c_nestlevel--;
compiler_unit_free(c->u);
Expand All @@ -1655,7 +1654,7 @@ compiler_exit_scope(struct compiler *c)
c->u = NULL;
}

PyErr_Restore(exc_type, exc_val, exc_tb);
PyErr_SetRaisedException(exc);
}

/* Search if variable annotations are present statically in a block. */
Expand Down
9 changes: 3 additions & 6 deletions Python/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ PyFrameObject *
_PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame)
{
assert(frame->frame_obj == NULL);
PyObject *error_type, *error_value, *error_traceback;
PyErr_Fetch(&error_type, &error_value, &error_traceback);
PyObject *exc = PyErr_GetRaisedException();

PyFrameObject *f = _PyFrame_New_NoTrack(frame->f_code);
if (f == NULL) {
Py_XDECREF(error_type);
Py_XDECREF(error_value);
Py_XDECREF(error_traceback);
Py_XDECREF(exc);
return NULL;
}
PyErr_Restore(error_type, error_value, error_traceback);
PyErr_SetRaisedException(exc);
if (frame->frame_obj) {
// GH-97002: How did we get into this horrible situation? Most likely,
// allocating f triggered a GC collection, which ran some code that
Expand Down
2 changes: 1 addition & 1 deletion Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "pycore_interp.h" // _PyInterpreterState.runtime
#include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD
#include "pycore_pathconfig.h" // _Py_path_config
#include "pycore_pyerrors.h" // _PyErr_Fetch()
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pylifecycle.h" // _Py_PreInitializeFromConfig()
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h" // _PyThreadState_GET()
Expand Down
14 changes: 5 additions & 9 deletions Python/modsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,12 @@ static PyObject *do_mkvalue(const char**, va_list *, int);
static void
do_ignore(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
{
PyObject *v;
Py_ssize_t i;
assert(PyErr_Occurred());
v = PyTuple_New(n);
for (i = 0; i < n; i++) {
PyObject *exception, *value, *tb, *w;

PyErr_Fetch(&exception, &value, &tb);
w = do_mkvalue(p_format, p_va, flags);
PyErr_Restore(exception, value, tb);
PyObject *v = PyTuple_New(n);
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *exc = PyErr_GetRaisedException();
PyObject *w = do_mkvalue(p_format, p_va, flags);
PyErr_SetRaisedException(exc);
if (w != NULL) {
if (v != NULL) {
PyTuple_SET_ITEM(v, i, w);
Expand Down
26 changes: 11 additions & 15 deletions Python/sysmodule.c