From 21208bbbe4d823c547c2b4c007e8fd44643eab3e Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 31 May 2023 13:52:57 +0100 Subject: [PATCH 1/3] gh-105140: remove unused arg of _PyErr_ChainStackItem --- Include/internal/pycore_pyerrors.h | 3 +-- Objects/genobject.c | 2 +- Python/errors.c | 38 ++++++------------------------ 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/Include/internal/pycore_pyerrors.h b/Include/internal/pycore_pyerrors.h index 4620a269644917..d75bef0c3b5d8f 100644 --- a/Include/internal/pycore_pyerrors.h +++ b/Include/internal/pycore_pyerrors.h @@ -61,8 +61,7 @@ PyAPI_FUNC(void) _PyErr_SetObject( PyObject *type, PyObject *value); -PyAPI_FUNC(void) _PyErr_ChainStackItem( - _PyErr_StackItem *exc_info); +PyAPI_FUNC(void) _PyErr_ChainStackItem(void); PyAPI_FUNC(void) _PyErr_Clear(PyThreadState *tstate); diff --git a/Objects/genobject.c b/Objects/genobject.c index b40cf41a60027d..7a295dc239d632 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -222,7 +222,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, if (exc) { assert(_PyErr_Occurred(tstate)); - _PyErr_ChainStackItem(NULL); + _PyErr_ChainStackItem(); } gen->gi_frame_state = FRAME_EXECUTING; diff --git a/Python/errors.c b/Python/errors.c index a8000ac94918db..eab6503046b5cc 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -703,52 +703,28 @@ _PyErr_ChainExceptions1(PyObject *exc) } } -/* Set the currently set exception's context to the given exception. - - If the provided exc_info is NULL, then the current Python thread state's - exc_info will be used for the context instead. +/* If the current thread is handling an exception (exc_info is ), set this + exception as the context of the current raised exception. This function can only be called when _PyErr_Occurred() is true. Also, this function won't create any cycles in the exception context chain to the extent that _PyErr_SetObject ensures this. */ void -_PyErr_ChainStackItem(_PyErr_StackItem *exc_info) +_PyErr_ChainStackItem(void) { PyThreadState *tstate = _PyThreadState_GET(); assert(_PyErr_Occurred(tstate)); - int exc_info_given; - if (exc_info == NULL) { - exc_info_given = 0; - exc_info = tstate->exc_info; - } else { - exc_info_given = 1; - } - + _PyErr_StackItem *exc_info = tstate->exc_info; if (exc_info->exc_value == NULL || exc_info->exc_value == Py_None) { return; } - _PyErr_StackItem *saved_exc_info; - if (exc_info_given) { - /* Temporarily set the thread state's exc_info since this is what - _PyErr_SetObject uses for implicit exception chaining. */ - saved_exc_info = tstate->exc_info; - tstate->exc_info = exc_info; - } - - PyObject *typ, *val, *tb; - _PyErr_Fetch(tstate, &typ, &val, &tb); + PyObject *exc = _PyErr_GetRaisedException(tstate); /* _PyErr_SetObject sets the context from PyThreadState. */ - _PyErr_SetObject(tstate, typ, val); - Py_DECREF(typ); // since _PyErr_Occurred was true - Py_XDECREF(val); - Py_XDECREF(tb); - - if (exc_info_given) { - tstate->exc_info = saved_exc_info; - } + _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(exc), exc); + Py_DECREF(exc); // since _PyErr_Occurred was true } static PyObject * From be7bcd51c50ed017563454f214cff8d73a7ac617 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 31 May 2023 13:54:48 +0100 Subject: [PATCH 2/3] add news --- .../2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst new file mode 100644 index 00000000000000..3d862be0200094 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst @@ -0,0 +1 @@ +Remove unused, untested and undocumented arg of ``_PyErr_ChainStackItem``. From 364617cae017983dd182e8d4b386838b6ba26c98 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Thu, 1 Jun 2023 21:24:22 +0100 Subject: [PATCH 3/3] Delete 2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst --- .../2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst deleted file mode 100644 index 3d862be0200094..00000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2023-05-31-13-54-41.gh-issue-105140.JmPdPc.rst +++ /dev/null @@ -1 +0,0 @@ -Remove unused, untested and undocumented arg of ``_PyErr_ChainStackItem``.