summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-03-13 15:56:24 (GMT)
committerGitHub <noreply@github.com>2023-03-13 15:56:24 (GMT)
commitca01cae1e91446c70fa569d4502c7cbba381e7ff (patch)
treefa1f611798d3938213274904ca9a980554074b93 /Python
parent9a8b66b58c74236959a01d579e2c156d9c2e7cb3 (diff)
downloadcpython-ca01cae1e91446c70fa569d4502c7cbba381e7ff.zip
cpython-ca01cae1e91446c70fa569d4502c7cbba381e7ff.tar.gz
cpython-ca01cae1e91446c70fa569d4502c7cbba381e7ff.tar.bz2
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (#102631)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c42
1 files changed, 19 insertions, 23 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index b422d0e..9223551 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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
@@ -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;
}
@@ -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);
}
}