From e317c72249e5c497b69a7972dcf21ed6ce870440 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 17 Apr 2025 21:35:15 +0200 Subject: [PATCH 1/2] Avoid locking in frozenset.__contains__ --- Objects/clinic/setobject.c.h | 24 +++++++++++++++++++++++- Objects/setobject.c | 24 +++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Objects/clinic/setobject.c.h b/Objects/clinic/setobject.c.h index 96c70d0ae95a46..488be4435f81d7 100644 --- a/Objects/clinic/setobject.c.h +++ b/Objects/clinic/setobject.c.h @@ -432,6 +432,28 @@ set___contains__(PyObject *so, PyObject *key) return return_value; } +PyDoc_STRVAR(frozenset___contains____doc__, +"__contains__($self, object, /)\n" +"--\n" +"\n" +"x.__contains__(y) <==> y in x."); + +#define FROZENSET___CONTAINS___METHODDEF \ + {"__contains__", (PyCFunction)frozenset___contains__, METH_O|METH_COEXIST, frozenset___contains____doc__}, + +static PyObject * +frozenset___contains___impl(PySetObject *so, PyObject *key); + +static PyObject * +frozenset___contains__(PyObject *so, PyObject *key) +{ + PyObject *return_value = NULL; + + return_value = frozenset___contains___impl((PySetObject *)so, key); + + return return_value; +} + PyDoc_STRVAR(set_remove__doc__, "remove($self, object, /)\n" "--\n" @@ -532,4 +554,4 @@ set___sizeof__(PyObject *so, PyObject *Py_UNUSED(ignored)) return return_value; } -/*[clinic end generated code: output=e2f1470de062d661 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=7f7fe845ca165078 input=a9049054013a1b77]*/ diff --git a/Objects/setobject.c b/Objects/setobject.c index acbb53aafc0a26..724375f7a1c19c 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1276,6 +1276,28 @@ set_copy_impl(PySetObject *so) return copy; } +/*[clinic input] +@coexist +frozenset.__contains__ + so: setobject + object as key: object + / + +x.__contains__(y) <==> y in x. +[clinic start generated code]*/ + +static PyObject * +frozenset___contains___impl(PySetObject *so, PyObject *key) +/*[clinic end generated code: output=2301ed91bc3a6dd5 input=2f04922a98d8bab7]*/ +{ + long result; + + result = set_contains_lock_held(so, key); + if (result < 0) + return NULL; + return PyBool_FromLong(result); +} + /*[clinic input] @critical_section frozenset.copy @@ -2555,7 +2577,7 @@ PyTypeObject PySet_Type = { static PyMethodDef frozenset_methods[] = { - SET___CONTAINS___METHODDEF + FROZENSET___CONTAINS___METHODDEF FROZENSET_COPY_METHODDEF SET_DIFFERENCE_MULTI_METHODDEF SET_INTERSECTION_MULTI_METHODDEF From 6903e01c3f9c8d168cec8f5dd38bc135d3ed33c0 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 17 Apr 2025 21:48:46 +0200 Subject: [PATCH 2/2] fix build --- Objects/setobject.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Objects/setobject.c b/Objects/setobject.c index 724375f7a1c19c..347888389b8dcd 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1276,28 +1276,6 @@ set_copy_impl(PySetObject *so) return copy; } -/*[clinic input] -@coexist -frozenset.__contains__ - so: setobject - object as key: object - / - -x.__contains__(y) <==> y in x. -[clinic start generated code]*/ - -static PyObject * -frozenset___contains___impl(PySetObject *so, PyObject *key) -/*[clinic end generated code: output=2301ed91bc3a6dd5 input=2f04922a98d8bab7]*/ -{ - long result; - - result = set_contains_lock_held(so, key); - if (result < 0) - return NULL; - return PyBool_FromLong(result); -} - /*[clinic input] @critical_section frozenset.copy @@ -2275,6 +2253,28 @@ set___contains___impl(PySetObject *so, PyObject *key) return PyBool_FromLong(result); } +/*[clinic input] +@coexist +frozenset.__contains__ + so: setobject + object as key: object + / + +x.__contains__(y) <==> y in x. +[clinic start generated code]*/ + +static PyObject * +frozenset___contains___impl(PySetObject *so, PyObject *key) +/*[clinic end generated code: output=2301ed91bc3a6dd5 input=2f04922a98d8bab7]*/ +{ + long result; + + result = set_contains_lock_held(so, key); + if (result < 0) + return NULL; + return PyBool_FromLong(result); +} + /*[clinic input] @critical_section set.remove