From 667ed8289288c26f6d01ac86ab7443a4f64f65a6 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 25 Jan 2025 16:03:35 +0000 Subject: [PATCH 1/3] make exception group thread safe --- Objects/clinic/exceptions.c.h | 74 ++++++++++++++++++++++++++++++++++- Objects/exceptions.c | 58 ++++++++++++++++++--------- 2 files changed, 113 insertions(+), 19 deletions(-) diff --git a/Objects/clinic/exceptions.c.h b/Objects/clinic/exceptions.c.h index 3bd9a8553ab2fc..8699df07495ad8 100644 --- a/Objects/clinic/exceptions.c.h +++ b/Objects/clinic/exceptions.c.h @@ -308,4 +308,76 @@ BaseException___cause___set(PyObject *self, PyObject *value, void *Py_UNUSED(con return return_value; } -/*[clinic end generated code: output=8be99f8a7e527ba4 input=a9049054013a1b77]*/ + +PyDoc_STRVAR(BaseExceptionGroup_derive__doc__, +"derive($self, excs, /)\n" +"--\n" +"\n"); + +#define BASEEXCEPTIONGROUP_DERIVE_METHODDEF \ + {"derive", (PyCFunction)BaseExceptionGroup_derive, METH_O, BaseExceptionGroup_derive__doc__}, + +static PyObject * +BaseExceptionGroup_derive_impl(PyBaseExceptionGroupObject *self, + PyObject *excs); + +static PyObject * +BaseExceptionGroup_derive(PyBaseExceptionGroupObject *self, PyObject *excs) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = BaseExceptionGroup_derive_impl((PyBaseExceptionGroupObject *)self, excs); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + +PyDoc_STRVAR(BaseExceptionGroup_split__doc__, +"split($self, matcher_value, /)\n" +"--\n" +"\n"); + +#define BASEEXCEPTIONGROUP_SPLIT_METHODDEF \ + {"split", (PyCFunction)BaseExceptionGroup_split, METH_O, BaseExceptionGroup_split__doc__}, + +static PyObject * +BaseExceptionGroup_split_impl(PyBaseExceptionGroupObject *self, + PyObject *matcher_value); + +static PyObject * +BaseExceptionGroup_split(PyBaseExceptionGroupObject *self, PyObject *matcher_value) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = BaseExceptionGroup_split_impl((PyBaseExceptionGroupObject *)self, matcher_value); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + +PyDoc_STRVAR(BaseExceptionGroup_subgroup__doc__, +"subgroup($self, matcher_value, /)\n" +"--\n" +"\n"); + +#define BASEEXCEPTIONGROUP_SUBGROUP_METHODDEF \ + {"subgroup", (PyCFunction)BaseExceptionGroup_subgroup, METH_O, BaseExceptionGroup_subgroup__doc__}, + +static PyObject * +BaseExceptionGroup_subgroup_impl(PyBaseExceptionGroupObject *self, + PyObject *matcher_value); + +static PyObject * +BaseExceptionGroup_subgroup(PyBaseExceptionGroupObject *self, PyObject *matcher_value) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = BaseExceptionGroup_subgroup_impl((PyBaseExceptionGroupObject *)self, matcher_value); + Py_END_CRITICAL_SECTION(); + + return return_value; +} +/*[clinic end generated code: output=19aed708dcaf7184 input=a9049054013a1b77]*/ diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 20fe55d2cc2955..fb1761d2e993a8 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -20,8 +20,9 @@ /*[clinic input] class BaseException "PyBaseExceptionObject *" "&PyExc_BaseException" +class BaseExceptionGroup "PyBaseExceptionGroupObject *" "&PyExc_BaseExceptionGroup" [clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=90558eb0fbf8a3d0]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7c45e78cff8edc3]*/ /* Compatibility aliases */ @@ -1034,18 +1035,21 @@ BaseExceptionGroup_str(PyBaseExceptionGroupObject *self) self->msg, num_excs, num_excs > 1 ? "s" : ""); } +/*[clinic input] +@critical_section +BaseExceptionGroup.derive + excs: object + / +[clinic start generated code]*/ + static PyObject * -BaseExceptionGroup_derive(PyObject *self_, PyObject *excs) +BaseExceptionGroup_derive_impl(PyBaseExceptionGroupObject *self, + PyObject *excs) +/*[clinic end generated code: output=4307564218dfbf06 input=f72009d38e98cec1]*/ { - PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(self_); - PyObject *init_args = PyTuple_Pack(2, self->msg, excs); - if (!init_args) { - return NULL; - } - PyObject *eg = PyObject_CallObject( - PyExc_BaseExceptionGroup, init_args); - Py_DECREF(init_args); - return eg; + PyObject *stack[] = {self->msg, excs}; + size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET; + return PyObject_Vectorcall(PyExc_BaseExceptionGroup, stack, nargsf, NULL); } static int @@ -1330,8 +1334,17 @@ exceptiongroup_split_recursive(PyObject *exc, return retval; } +/*[clinic input] +@critical_section +BaseExceptionGroup.split + matcher_value: object + / +[clinic start generated code]*/ + static PyObject * -BaseExceptionGroup_split(PyObject *self, PyObject *matcher_value) +BaseExceptionGroup_split_impl(PyBaseExceptionGroupObject *self, + PyObject *matcher_value) +/*[clinic end generated code: output=d74db579da4df6e2 input=0c5cfbfed57e0052]*/ { _exceptiongroup_split_matcher_type matcher_type; if (get_matcher_type(matcher_value, &matcher_type) < 0) { @@ -1341,7 +1354,7 @@ BaseExceptionGroup_split(PyObject *self, PyObject *matcher_value) _exceptiongroup_split_result split_result; bool construct_rest = true; if (exceptiongroup_split_recursive( - self, matcher_type, matcher_value, + (PyObject *)self, matcher_type, matcher_value, construct_rest, &split_result) < 0) { return NULL; } @@ -1356,8 +1369,17 @@ BaseExceptionGroup_split(PyObject *self, PyObject *matcher_value) return result; } +/*[clinic input] +@critical_section +BaseExceptionGroup.subgroup + matcher_value: object + / +[clinic start generated code]*/ + static PyObject * -BaseExceptionGroup_subgroup(PyObject *self, PyObject *matcher_value) +BaseExceptionGroup_subgroup_impl(PyBaseExceptionGroupObject *self, + PyObject *matcher_value) +/*[clinic end generated code: output=07dbec8f77d4dd8e input=988ffdd755a151ce]*/ { _exceptiongroup_split_matcher_type matcher_type; if (get_matcher_type(matcher_value, &matcher_type) < 0) { @@ -1367,7 +1389,7 @@ BaseExceptionGroup_subgroup(PyObject *self, PyObject *matcher_value) _exceptiongroup_split_result split_result; bool construct_rest = false; if (exceptiongroup_split_recursive( - self, matcher_type, matcher_value, + (PyObject *)self, matcher_type, matcher_value, construct_rest, &split_result) < 0) { return NULL; } @@ -1633,9 +1655,9 @@ static PyMemberDef BaseExceptionGroup_members[] = { static PyMethodDef BaseExceptionGroup_methods[] = { {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, - {"derive", (PyCFunction)BaseExceptionGroup_derive, METH_O}, - {"split", (PyCFunction)BaseExceptionGroup_split, METH_O}, - {"subgroup", (PyCFunction)BaseExceptionGroup_subgroup, METH_O}, + BASEEXCEPTIONGROUP_DERIVE_METHODDEF + BASEEXCEPTIONGROUP_SPLIT_METHODDEF + BASEEXCEPTIONGROUP_SUBGROUP_METHODDEF {NULL} }; From 86c7c208568996cf490c636cc6815bdcf3a99d54 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 25 Jan 2025 17:02:31 +0000 Subject: [PATCH 2/3] reduce diff --- Objects/exceptions.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index fb1761d2e993a8..49ca769f670ca7 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1047,9 +1047,15 @@ BaseExceptionGroup_derive_impl(PyBaseExceptionGroupObject *self, PyObject *excs) /*[clinic end generated code: output=4307564218dfbf06 input=f72009d38e98cec1]*/ { - PyObject *stack[] = {self->msg, excs}; - size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET; - return PyObject_Vectorcall(PyExc_BaseExceptionGroup, stack, nargsf, NULL); + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(self_); + PyObject *init_args = PyTuple_Pack(2, self->msg, excs); + if (!init_args) { + return NULL; + } + PyObject *eg = PyObject_CallObject( + PyExc_BaseExceptionGroup, init_args); + Py_DECREF(init_args); + return eg; } static int From 0b313399e0120f4e45ae3b19d5e668bae213b064 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 25 Jan 2025 22:34:55 +0530 Subject: [PATCH 3/3] Update Objects/exceptions.c --- Objects/exceptions.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 49ca769f670ca7..ea2733435fc3ec 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1047,7 +1047,6 @@ BaseExceptionGroup_derive_impl(PyBaseExceptionGroupObject *self, PyObject *excs) /*[clinic end generated code: output=4307564218dfbf06 input=f72009d38e98cec1]*/ { - PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(self_); PyObject *init_args = PyTuple_Pack(2, self->msg, excs); if (!init_args) { return NULL;