Skip to content

Commit ba86d35

Browse files
iritkatrielFidget-Spinner
authored andcommitted
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (python#102816)
1 parent 1820891 commit ba86d35

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

Python/bytecodes.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,7 @@ dummy_func(
851851
}
852852
assert(exc && PyExceptionInstance_Check(exc));
853853
Py_INCREF(exc);
854-
PyObject *typ = Py_NewRef(PyExceptionInstance_Class(exc));
855-
PyObject *tb = PyException_GetTraceback(exc);
856-
_PyErr_Restore(tstate, typ, exc, tb);
854+
_PyErr_SetRaisedException(tstate, exc);
857855
goto exception_unwind;
858856
}
859857

@@ -864,9 +862,7 @@ dummy_func(
864862
}
865863
else {
866864
Py_INCREF(exc);
867-
PyObject *typ = Py_NewRef(PyExceptionInstance_Class(exc));
868-
PyObject *tb = PyException_GetTraceback(exc);
869-
_PyErr_Restore(tstate, typ, exc, tb);
865+
_PyErr_SetRaisedException(tstate, exc);
870866
goto exception_unwind;
871867
}
872868
}

Python/generated_cases.c.h

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/import.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ PyImport_AddModule(const char *name)
389389
static void
390390
remove_module(PyThreadState *tstate, PyObject *name)
391391
{
392-
PyObject *type, *value, *traceback;
393-
_PyErr_Fetch(tstate, &type, &value, &traceback);
392+
PyObject *exc = _PyErr_GetRaisedException(tstate);
394393

395394
PyObject *modules = MODULES(tstate->interp);
396395
if (PyDict_CheckExact(modules)) {
@@ -403,7 +402,7 @@ remove_module(PyThreadState *tstate, PyObject *name)
403402
}
404403
}
405404

406-
_PyErr_ChainExceptions(type, value, traceback);
405+
_PyErr_ChainExceptions1(exc);
407406
}
408407

409408

@@ -2324,32 +2323,34 @@ remove_importlib_frames(PyThreadState *tstate)
23242323
const char *remove_frames = "_call_with_frames_removed";
23252324
int always_trim = 0;
23262325
int in_importlib = 0;
2327-
PyObject *exception, *value, *base_tb, *tb;
23282326
PyObject **prev_link, **outer_link = NULL;
2327+
PyObject *base_tb = NULL;
23292328

23302329
/* Synopsis: if it's an ImportError, we trim all importlib chunks
23312330
from the traceback. We always trim chunks
23322331
which end with a call to "_call_with_frames_removed". */
23332332

2334-
_PyErr_Fetch(tstate, &exception, &value, &base_tb);
2335-
if (!exception || _PyInterpreterState_GetConfig(tstate->interp)->verbose) {
2333+
PyObject *exc = _PyErr_GetRaisedException(tstate);
2334+
if (exc == NULL || _PyInterpreterState_GetConfig(tstate->interp)->verbose) {
23362335
goto done;
23372336
}
23382337

2339-
if (PyType_IsSubtype((PyTypeObject *) exception,
2340-
(PyTypeObject *) PyExc_ImportError))
2338+
if (PyType_IsSubtype(Py_TYPE(exc), (PyTypeObject *) PyExc_ImportError)) {
23412339
always_trim = 1;
2340+
}
23422341

2342+
assert(PyExceptionInstance_Check(exc));
2343+
base_tb = PyException_GetTraceback(exc);
23432344
prev_link = &base_tb;
2344-
tb = base_tb;
2345+
PyObject *tb = base_tb;
23452346
while (tb != NULL) {
2347+
assert(PyTraceBack_Check(tb));
23462348
PyTracebackObject *traceback = (PyTracebackObject *)tb;
23472349
PyObject *next = (PyObject *) traceback->tb_next;
23482350
PyFrameObject *frame = traceback->tb_frame;
23492351
PyCodeObject *code = PyFrame_GetCode(frame);
23502352
int now_in_importlib;
23512353

2352-
assert(PyTraceBack_Check(tb));
23532354
now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) ||
23542355
_PyUnicode_EqualToASCIIString(code->co_filename, external_filename);
23552356
if (now_in_importlib && !in_importlib) {
@@ -2370,15 +2371,14 @@ remove_importlib_frames(PyThreadState *tstate)
23702371
Py_DECREF(code);
23712372
tb = next;
23722373
}
2373-
assert(PyExceptionInstance_Check(value));
2374-
assert((PyObject *)Py_TYPE(value) == exception);
23752374
if (base_tb == NULL) {
23762375
base_tb = Py_None;
23772376
Py_INCREF(Py_None);
23782377
}
2379-
PyException_SetTraceback(value, base_tb);
2378+
PyException_SetTraceback(exc, base_tb);
23802379
done:
2381-
_PyErr_Restore(tstate, exception, value, base_tb);
2380+
Py_XDECREF(base_tb);
2381+
_PyErr_SetRaisedException(tstate, exc);
23822382
}
23832383

23842384

Python/pythonrun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "pycore_interp.h" // PyInterpreterState.importlib
1919
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
2020
#include "pycore_parser.h" // _PyParser_ASTFromString()
21-
#include "pycore_pyerrors.h" // _PyErr_Fetch, _Py_Offer_Suggestions
21+
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException, _Py_Offer_Suggestions
2222
#include "pycore_pylifecycle.h" // _Py_UnhandledKeyboardInterrupt
2323
#include "pycore_pystate.h" // _PyInterpreterState_GET()
2424
#include "pycore_sysmodule.h" // _PySys_Audit()

0 commit comments

Comments
 (0)