From de98c0876e090898b225d9f5be2c6bc7155a7387 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 22 Mar 2023 20:42:41 +0000 Subject: [PATCH 1/7] gh-102192: remove unused _PyErr_ChainExceptions --- Include/cpython/pyerrors.h | 1 - Python/errors.c | 37 ------------------------------------- 2 files changed, 38 deletions(-) diff --git a/Include/cpython/pyerrors.h b/Include/cpython/pyerrors.h index 65bdc942f5067a..c491222a4c437c 100644 --- a/Include/cpython/pyerrors.h +++ b/Include/cpython/pyerrors.h @@ -98,7 +98,6 @@ PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, Py /* Context manipulation (PEP 3134) */ -PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *); PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *); /* Like PyErr_Format(), but saves current exception as __context__ and diff --git a/Python/errors.c b/Python/errors.c index ab14770c6e810c..08988e39c4f3d7 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -644,43 +644,6 @@ _PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info) } -/* Like PyErr_Restore(), but if an exception is already set, - set the context associated with it. - - The caller is responsible for ensuring that this call won't create - any cycles in the exception context chain. */ -void -_PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb) -{ - if (typ == NULL) - return; - - PyThreadState *tstate = _PyThreadState_GET(); - - if (!PyExceptionClass_Check(typ)) { - _PyErr_Format(tstate, PyExc_SystemError, - "_PyErr_ChainExceptions: " - "exception %R is not a BaseException subclass", - typ); - return; - } - - if (_PyErr_Occurred(tstate)) { - _PyErr_NormalizeException(tstate, &typ, &val, &tb); - if (tb != NULL) { - PyException_SetTraceback(val, tb); - Py_DECREF(tb); - } - Py_DECREF(typ); - PyObject *exc2 = _PyErr_GetRaisedException(tstate); - PyException_SetContext(exc2, val); - _PyErr_SetRaisedException(tstate, exc2); - } - else { - _PyErr_Restore(tstate, typ, val, tb); - } -} - /* Like PyErr_SetRaisedException(), but if an exception is already set, set the context associated with it. From c1261c7c33df58e42ff69a35629a3c88758bb51a Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 31 Mar 2023 11:37:05 +0100 Subject: [PATCH 2/7] Revert "gh-102192: remove unused _PyErr_ChainExceptions" This reverts commit de98c0876e090898b225d9f5be2c6bc7155a7387. --- Include/cpython/pyerrors.h | 1 + Python/errors.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/Include/cpython/pyerrors.h b/Include/cpython/pyerrors.h index c491222a4c437c..65bdc942f5067a 100644 --- a/Include/cpython/pyerrors.h +++ b/Include/cpython/pyerrors.h @@ -98,6 +98,7 @@ PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, Py /* Context manipulation (PEP 3134) */ +PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *); PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *); /* Like PyErr_Format(), but saves current exception as __context__ and diff --git a/Python/errors.c b/Python/errors.c index 08988e39c4f3d7..ab14770c6e810c 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -644,6 +644,43 @@ _PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info) } +/* Like PyErr_Restore(), but if an exception is already set, + set the context associated with it. + + The caller is responsible for ensuring that this call won't create + any cycles in the exception context chain. */ +void +_PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb) +{ + if (typ == NULL) + return; + + PyThreadState *tstate = _PyThreadState_GET(); + + if (!PyExceptionClass_Check(typ)) { + _PyErr_Format(tstate, PyExc_SystemError, + "_PyErr_ChainExceptions: " + "exception %R is not a BaseException subclass", + typ); + return; + } + + if (_PyErr_Occurred(tstate)) { + _PyErr_NormalizeException(tstate, &typ, &val, &tb); + if (tb != NULL) { + PyException_SetTraceback(val, tb); + Py_DECREF(tb); + } + Py_DECREF(typ); + PyObject *exc2 = _PyErr_GetRaisedException(tstate); + PyException_SetContext(exc2, val); + _PyErr_SetRaisedException(tstate, exc2); + } + else { + _PyErr_Restore(tstate, typ, val, tb); + } +} + /* Like PyErr_SetRaisedException(), but if an exception is already set, set the context associated with it. From ca61240c14a781f1af5fa9833ca4b3c60ef8e041 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 31 Mar 2023 12:07:00 +0100 Subject: [PATCH 3/7] deprecate it --- Doc/whatsnew/3.12.rst | 7 +++++++ Include/cpython/pyerrors.h | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 06ea416d751354..f71f2c73e31b90 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -971,6 +971,10 @@ New Features This is less error prone and a bit more efficient. (Contributed by Mark Shannon in :gh:`101578`.) +* Add :c:func:`_PyErr_ChainExceptions1`, which takes an exception instance, + to replace the legacy-api :c:func:`_PyErr_ChainExceptions`, which is now + deprecated. (Contributed by Mark Shannon in :gh:`101578`.) + * Add :c:func:`PyException_GetArgs` and :c:func:`PyException_SetArgs` as convenience functions for retrieving and modifying the :attr:`~BaseException.args` passed to the exception's constructor. @@ -980,6 +984,7 @@ New Features to replace the legacy-api :c:func:`PyErr_Display`. (Contributed by Irit Katriel in :gh:`102755`). + Porting to Python 3.12 ---------------------- @@ -1121,6 +1126,8 @@ Deprecated * :c:func:`PyErr_Display` is deprecated. Use :c:func:`PyErr_DisplayException` instead. (Contributed by Irit Katriel in :gh:`102755`). +* :c:func:`_PyErr_ChainExceptions` is deprecated. Use :c:func:`_PyErr_ChainExceptions1` + instead. (Contributed by Irit Katriel in :gh:`102192`.) Removed ------- diff --git a/Include/cpython/pyerrors.h b/Include/cpython/pyerrors.h index 65bdc942f5067a..758804ade2baa7 100644 --- a/Include/cpython/pyerrors.h +++ b/Include/cpython/pyerrors.h @@ -98,7 +98,7 @@ PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, Py /* Context manipulation (PEP 3134) */ -PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *); +Py_DEPRECATED(3.12) PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *); PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *); /* Like PyErr_Format(), but saves current exception as __context__ and From 4fc222b7cbf1af118bdc091bacd03e0a90d1957f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 31 Mar 2023 12:22:41 +0100 Subject: [PATCH 4/7] add news --- .../2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst b/Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst new file mode 100644 index 00000000000000..6423335d7a53f7 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst @@ -0,0 +1,2 @@ +Deprecated :c:func:`_PyErr_ChainExceptions` infavour of +:c:func:`_PyErr_ChainExceptions1`. From 3cbe6bee3124ab5d515db771bf3e8cdb16b3fa3d Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 31 Mar 2023 12:23:30 +0100 Subject: [PATCH 5/7] whitespace --- Doc/whatsnew/3.12.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index f71f2c73e31b90..50f6d2a577a4f4 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -984,7 +984,6 @@ New Features to replace the legacy-api :c:func:`PyErr_Display`. (Contributed by Irit Katriel in :gh:`102755`). - Porting to Python 3.12 ---------------------- From 5765ba2985d38a09f314e761e127fc99cd89c06e Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 31 Mar 2023 12:54:42 +0100 Subject: [PATCH 6/7] hopefully fix news build --- Doc/whatsnew/3.12.rst | 6 +++--- .../2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 50f6d2a577a4f4..b520af8c83615a 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -971,8 +971,8 @@ New Features This is less error prone and a bit more efficient. (Contributed by Mark Shannon in :gh:`101578`.) -* Add :c:func:`_PyErr_ChainExceptions1`, which takes an exception instance, - to replace the legacy-api :c:func:`_PyErr_ChainExceptions`, which is now +* Add ``_PyErr_ChainExceptions1``, which takes an exception instance, + to replace the legacy-api ``_PyErr_ChainExceptions``, which is now deprecated. (Contributed by Mark Shannon in :gh:`101578`.) * Add :c:func:`PyException_GetArgs` and :c:func:`PyException_SetArgs` @@ -1125,7 +1125,7 @@ Deprecated * :c:func:`PyErr_Display` is deprecated. Use :c:func:`PyErr_DisplayException` instead. (Contributed by Irit Katriel in :gh:`102755`). -* :c:func:`_PyErr_ChainExceptions` is deprecated. Use :c:func:`_PyErr_ChainExceptions1` +* ``_PyErr_ChainExceptions`` is deprecated. Use ``_PyErr_ChainExceptions1`` instead. (Contributed by Irit Katriel in :gh:`102192`.) Removed diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst b/Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst index 6423335d7a53f7..10dd72b1abc422 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-03-31-12-22-25.gh-issue-102192.gYxJP_.rst @@ -1,2 +1,2 @@ -Deprecated :c:func:`_PyErr_ChainExceptions` infavour of -:c:func:`_PyErr_ChainExceptions1`. +Deprecated ``_PyErr_ChainExceptions`` in favour of +``_PyErr_ChainExceptions1``. From 914555576aaaca780214f40feceeac6a61264d5f Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Sat, 1 Apr 2023 19:48:30 +0100 Subject: [PATCH 7/7] api->API --- Doc/whatsnew/3.12.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index b520af8c83615a..65468d031518fc 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -972,7 +972,7 @@ New Features (Contributed by Mark Shannon in :gh:`101578`.) * Add ``_PyErr_ChainExceptions1``, which takes an exception instance, - to replace the legacy-api ``_PyErr_ChainExceptions``, which is now + to replace the legacy-API ``_PyErr_ChainExceptions``, which is now deprecated. (Contributed by Mark Shannon in :gh:`101578`.) * Add :c:func:`PyException_GetArgs` and :c:func:`PyException_SetArgs`