diff --git a/Objects/clinic/setobject.c.h b/Objects/clinic/setobject.c.h index 3853ce3bce685b..f3c96995ede60d 100644 --- a/Objects/clinic/setobject.c.h +++ b/Objects/clinic/setobject.c.h @@ -2,7 +2,6 @@ preserve [clinic start generated code]*/ -#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_CheckPositional() PyDoc_STRVAR(set_pop__doc__, @@ -22,13 +21,7 @@ set_pop_impl(PySetObject *so); static PyObject * set_pop(PySetObject *so, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set_pop_impl(so); - Py_END_CRITICAL_SECTION(); - - return return_value; + return set_pop_impl(so); } PyDoc_STRVAR(set_update__doc__, @@ -81,13 +74,7 @@ set_copy_impl(PySetObject *so); static PyObject * set_copy(PySetObject *so, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set_copy_impl(so); - Py_END_CRITICAL_SECTION(); - - return return_value; + return set_copy_impl(so); } PyDoc_STRVAR(frozenset_copy__doc__, @@ -105,13 +92,7 @@ frozenset_copy_impl(PySetObject *so); static PyObject * frozenset_copy(PySetObject *so, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = frozenset_copy_impl(so); - Py_END_CRITICAL_SECTION(); - - return return_value; + return frozenset_copy_impl(so); } PyDoc_STRVAR(set_clear__doc__, @@ -129,13 +110,7 @@ set_clear_impl(PySetObject *so); static PyObject * set_clear(PySetObject *so, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set_clear_impl(so); - Py_END_CRITICAL_SECTION(); - - return return_value; + return set_clear_impl(so); } PyDoc_STRVAR(set_union__doc__, @@ -252,21 +227,6 @@ PyDoc_STRVAR(set_isdisjoint__doc__, #define SET_ISDISJOINT_METHODDEF \ {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, set_isdisjoint__doc__}, -static PyObject * -set_isdisjoint_impl(PySetObject *so, PyObject *other); - -static PyObject * -set_isdisjoint(PySetObject *so, PyObject *other) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION2(so, other); - return_value = set_isdisjoint_impl(so, other); - Py_END_CRITICAL_SECTION2(); - - return return_value; -} - PyDoc_STRVAR(set_difference_update__doc__, "difference_update($self, /, *others)\n" "--\n" @@ -355,21 +315,6 @@ PyDoc_STRVAR(set_symmetric_difference__doc__, #define SET_SYMMETRIC_DIFFERENCE_METHODDEF \ {"symmetric_difference", (PyCFunction)set_symmetric_difference, METH_O, set_symmetric_difference__doc__}, -static PyObject * -set_symmetric_difference_impl(PySetObject *so, PyObject *other); - -static PyObject * -set_symmetric_difference(PySetObject *so, PyObject *other) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION2(so, other); - return_value = set_symmetric_difference_impl(so, other); - Py_END_CRITICAL_SECTION2(); - - return return_value; -} - PyDoc_STRVAR(set_issubset__doc__, "issubset($self, other, /)\n" "--\n" @@ -379,21 +324,6 @@ PyDoc_STRVAR(set_issubset__doc__, #define SET_ISSUBSET_METHODDEF \ {"issubset", (PyCFunction)set_issubset, METH_O, set_issubset__doc__}, -static PyObject * -set_issubset_impl(PySetObject *so, PyObject *other); - -static PyObject * -set_issubset(PySetObject *so, PyObject *other) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION2(so, other); - return_value = set_issubset_impl(so, other); - Py_END_CRITICAL_SECTION2(); - - return return_value; -} - PyDoc_STRVAR(set_issuperset__doc__, "issuperset($self, other, /)\n" "--\n" @@ -403,21 +333,6 @@ PyDoc_STRVAR(set_issuperset__doc__, #define SET_ISSUPERSET_METHODDEF \ {"issuperset", (PyCFunction)set_issuperset, METH_O, set_issuperset__doc__}, -static PyObject * -set_issuperset_impl(PySetObject *so, PyObject *other); - -static PyObject * -set_issuperset(PySetObject *so, PyObject *other) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION2(so, other); - return_value = set_issuperset_impl(so, other); - Py_END_CRITICAL_SECTION2(); - - return return_value; -} - PyDoc_STRVAR(set_add__doc__, "add($self, object, /)\n" "--\n" @@ -429,21 +344,6 @@ PyDoc_STRVAR(set_add__doc__, #define SET_ADD_METHODDEF \ {"add", (PyCFunction)set_add, METH_O, set_add__doc__}, -static PyObject * -set_add_impl(PySetObject *so, PyObject *key); - -static PyObject * -set_add(PySetObject *so, PyObject *key) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set_add_impl(so, key); - Py_END_CRITICAL_SECTION(); - - return return_value; -} - PyDoc_STRVAR(set___contains____doc__, "__contains__($self, object, /)\n" "--\n" @@ -453,21 +353,6 @@ PyDoc_STRVAR(set___contains____doc__, #define SET___CONTAINS___METHODDEF \ {"__contains__", (PyCFunction)set___contains__, METH_O|METH_COEXIST, set___contains____doc__}, -static PyObject * -set___contains___impl(PySetObject *so, PyObject *key); - -static PyObject * -set___contains__(PySetObject *so, PyObject *key) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set___contains___impl(so, key); - Py_END_CRITICAL_SECTION(); - - return return_value; -} - PyDoc_STRVAR(set_remove__doc__, "remove($self, object, /)\n" "--\n" @@ -479,21 +364,6 @@ PyDoc_STRVAR(set_remove__doc__, #define SET_REMOVE_METHODDEF \ {"remove", (PyCFunction)set_remove, METH_O, set_remove__doc__}, -static PyObject * -set_remove_impl(PySetObject *so, PyObject *key); - -static PyObject * -set_remove(PySetObject *so, PyObject *key) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set_remove_impl(so, key); - Py_END_CRITICAL_SECTION(); - - return return_value; -} - PyDoc_STRVAR(set_discard__doc__, "discard($self, object, /)\n" "--\n" @@ -506,21 +376,6 @@ PyDoc_STRVAR(set_discard__doc__, #define SET_DISCARD_METHODDEF \ {"discard", (PyCFunction)set_discard, METH_O, set_discard__doc__}, -static PyObject * -set_discard_impl(PySetObject *so, PyObject *key); - -static PyObject * -set_discard(PySetObject *so, PyObject *key) -{ - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set_discard_impl(so, key); - Py_END_CRITICAL_SECTION(); - - return return_value; -} - PyDoc_STRVAR(set___reduce____doc__, "__reduce__($self, /)\n" "--\n" @@ -536,13 +391,7 @@ set___reduce___impl(PySetObject *so); static PyObject * set___reduce__(PySetObject *so, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set___reduce___impl(so); - Py_END_CRITICAL_SECTION(); - - return return_value; + return set___reduce___impl(so); } PyDoc_STRVAR(set___sizeof____doc__, @@ -560,12 +409,6 @@ set___sizeof___impl(PySetObject *so); static PyObject * set___sizeof__(PySetObject *so, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(so); - return_value = set___sizeof___impl(so); - Py_END_CRITICAL_SECTION(); - - return return_value; + return set___sizeof___impl(so); } -/*[clinic end generated code: output=de4ee725bd29f758 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=34a30591148da884 input=a9049054013a1b77]*/ diff --git a/Objects/setobject.c b/Objects/setobject.c index 592711f305cbaf..b4d803c19e1e94 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -37,7 +37,6 @@ #include "pycore_dict.h" // _PyDict_Contains_KnownHash() #include "pycore_modsupport.h" // _PyArg_NoKwnames() #include "pycore_object.h" // _PyObject_GC_UNTRACK() -#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_SSIZE_RELAXED() #include "pycore_pyerrors.h" // _PyErr_SetKeyError() #include "pycore_setobject.h" // _PySet_NextEntry() definition #include // offsetof() @@ -131,8 +130,6 @@ set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash) int probes; int cmp; - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); - /* Pre-increment is necessary to prevent arbitrary code in the rich comparison from deallocating the key just before the insertion. */ Py_INCREF(key); @@ -526,7 +523,7 @@ set_dealloc(PySetObject *so) } static PyObject * -set_repr_lock_held(PySetObject *so) +set_repr(PySetObject *so) { PyObject *result=NULL, *keys, *listrepr, *tmp; int status = Py_ReprEnter((PyObject*)so); @@ -570,24 +567,14 @@ set_repr_lock_held(PySetObject *so) return result; } -static PyObject * -set_repr(PySetObject *so) -{ - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(so); - result = set_repr_lock_held(so); - Py_END_CRITICAL_SECTION(); - return result; -} - static Py_ssize_t -set_len(PySetObject *so) +set_len(PyObject *so) { - return FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used); + return ((PySetObject *)so)->used; } static int -set_merge_lock_held(PySetObject *so, PyObject *otherset) +set_merge(PySetObject *so, PyObject *otherset) { PySetObject *other; PyObject *key; @@ -597,8 +584,6 @@ set_merge_lock_held(PySetObject *so, PyObject *otherset) assert (PyAnySet_Check(so)); assert (PyAnySet_Check(otherset)); - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(otherset); other = (PySetObject*)otherset; if (other == so || other->used == 0) @@ -660,7 +645,6 @@ set_merge_lock_held(PySetObject *so, PyObject *otherset) } /*[clinic input] -@critical_section set.pop so: setobject @@ -671,7 +655,7 @@ Raises KeyError if the set is empty. static PyObject * set_pop_impl(PySetObject *so) -/*[clinic end generated code: output=4d65180f1271871b input=9296c84921125060]*/ +/*[clinic end generated code: output=4d65180f1271871b input=4a3f5552e660a260]*/ { /* Make sure the search finger is in bounds */ setentry *entry = so->table + (so->finger & so->mask); @@ -905,60 +889,58 @@ PyTypeObject PySetIter_Type = { static PyObject * set_iter(PySetObject *so) { - Py_ssize_t size = set_len(so); setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type); if (si == NULL) return NULL; si->si_set = (PySetObject*)Py_NewRef(so); - si->si_used = size; + si->si_used = so->used; si->si_pos = 0; - si->len = size; + si->len = so->used; _PyObject_GC_TRACK(si); return (PyObject *)si; } static int -set_update_dict_lock_held(PySetObject *so, PyObject *other) +set_update_internal(PySetObject *so, PyObject *other) { - assert(PyDict_CheckExact(other)); + PyObject *key, *it; - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(other); + if (PyAnySet_Check(other)) + return set_merge(so, other); - /* Do one big resize at the start, rather than - * incrementally resizing as we insert new keys. Expect - * that there will be no (or few) overlapping keys. - */ - Py_ssize_t dictsize = PyDict_GET_SIZE(other); - if ((so->fill + dictsize)*5 >= so->mask*3) { - if (set_table_resize(so, (so->used + dictsize)*2) != 0) { - return -1; - } - } + if (PyDict_CheckExact(other)) { + PyObject *value; + Py_ssize_t pos = 0; + Py_hash_t hash; + Py_ssize_t dictsize = PyDict_GET_SIZE(other); - Py_ssize_t pos = 0; - PyObject *key; - PyObject *value; - Py_hash_t hash; - while (_PyDict_Next(other, &pos, &key, &value, &hash)) { - if (set_add_entry(so, key, hash)) { + /* Do one big resize at the start, rather than + * incrementally resizing as we insert new keys. Expect + * that there will be no (or few) overlapping keys. + */ + if (dictsize < 0) return -1; + if ((so->fill + dictsize)*5 >= so->mask*3) { + if (set_table_resize(so, (so->used + dictsize)*2) != 0) + return -1; } + int err = 0; + Py_BEGIN_CRITICAL_SECTION(other); + while (_PyDict_Next(other, &pos, &key, &value, &hash)) { + if (set_add_entry(so, key, hash)) { + err = -1; + goto exit; + } + } +exit: + Py_END_CRITICAL_SECTION(); + return err; } - return 0; -} - -static int -set_update_iterable_lock_held(PySetObject *so, PyObject *other) -{ - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); - PyObject *it = PyObject_GetIter(other); - if (it == NULL) { + it = PyObject_GetIter(other); + if (it == NULL) return -1; - } - PyObject *key; while ((key = PyIter_Next(it)) != NULL) { if (set_add_key(so, key)) { Py_DECREF(it); @@ -973,69 +955,6 @@ set_update_iterable_lock_held(PySetObject *so, PyObject *other) return 0; } -static int -set_update_lock_held(PySetObject *so, PyObject *other) -{ - if (PyAnySet_Check(other)) { - return set_merge_lock_held(so, other); - } - else if (PyDict_CheckExact(other)) { - return set_update_dict_lock_held(so, other); - } - return set_update_iterable_lock_held(so, other); -} - -// set_update for a `so` that is only visible to the current thread -static int -set_update_local(PySetObject *so, PyObject *other) -{ - assert(Py_REFCNT(so) == 1); - if (PyAnySet_Check(other)) { - int rv; - Py_BEGIN_CRITICAL_SECTION(other); - rv = set_merge_lock_held(so, other); - Py_END_CRITICAL_SECTION(); - return rv; - } - else if (PyDict_CheckExact(other)) { - int rv; - Py_BEGIN_CRITICAL_SECTION(other); - rv = set_update_dict_lock_held(so, other); - Py_END_CRITICAL_SECTION(); - return rv; - } - return set_update_iterable_lock_held(so, other); -} - -static int -set_update_internal(PySetObject *so, PyObject *other) -{ - if (PyAnySet_Check(other)) { - if (Py_Is((PyObject *)so, other)) { - return 0; - } - int rv; - Py_BEGIN_CRITICAL_SECTION2(so, other); - rv = set_merge_lock_held(so, other); - Py_END_CRITICAL_SECTION2(); - return rv; - } - else if (PyDict_CheckExact(other)) { - int rv; - Py_BEGIN_CRITICAL_SECTION2(so, other); - rv = set_update_dict_lock_held(so, other); - Py_END_CRITICAL_SECTION2(); - return rv; - } - else { - int rv; - Py_BEGIN_CRITICAL_SECTION(so); - rv = set_update_iterable_lock_held(so, other); - Py_END_CRITICAL_SECTION(); - return rv; - } -} - /*[clinic input] set.update so: setobject @@ -1084,7 +1003,7 @@ make_new_set(PyTypeObject *type, PyObject *iterable) so->weakreflist = NULL; if (iterable != NULL) { - if (set_update_local(so, iterable)) { + if (set_update_internal(so, iterable)) { Py_DECREF(so); return NULL; } @@ -1207,7 +1126,6 @@ set_swap_bodies(PySetObject *a, PySetObject *b) } /*[clinic input] -@critical_section set.copy so: setobject @@ -1216,22 +1134,12 @@ Return a shallow copy of a set. static PyObject * set_copy_impl(PySetObject *so) -/*[clinic end generated code: output=c9223a1e1cc6b041 input=c169a4fbb8209257]*/ +/*[clinic end generated code: output=c9223a1e1cc6b041 input=2b80b288d47b8cf1]*/ { - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); - PyObject *copy = make_new_set_basetype(Py_TYPE(so), NULL); - if (copy == NULL) { - return NULL; - } - if (set_merge_lock_held((PySetObject *)copy, (PyObject *)so) < 0) { - Py_DECREF(copy); - return NULL; - } - return copy; + return make_new_set_basetype(Py_TYPE(so), (PyObject *)so); } /*[clinic input] -@critical_section frozenset.copy so: setobject @@ -1240,16 +1148,15 @@ Return a shallow copy of a set. static PyObject * frozenset_copy_impl(PySetObject *so) -/*[clinic end generated code: output=b356263526af9e70 input=fbf5bef131268dd7]*/ +/*[clinic end generated code: output=b356263526af9e70 input=3dc65577d344eff7]*/ { if (PyFrozenSet_CheckExact(so)) { return Py_NewRef(so); } - return set_copy_impl(so); + return set_copy(so, NULL); } /*[clinic input] -@critical_section set.clear so: setobject @@ -1258,7 +1165,7 @@ Remove all elements from this set. static PyObject * set_clear_impl(PySetObject *so) -/*[clinic end generated code: output=4e71d5a83904161a input=c6f831b366111950]*/ +/*[clinic end generated code: output=4e71d5a83904161a input=74ac19794da81a39]*/ { set_clear_internal(so); Py_RETURN_NONE; @@ -1289,7 +1196,7 @@ set_union_impl(PySetObject *so, PyObject *args) other = PyTuple_GET_ITEM(args, i); if ((PyObject *)so == other) continue; - if (set_update_local(result, other)) { + if (set_update_internal(result, other)) { Py_DECREF(result); return NULL; } @@ -1306,13 +1213,11 @@ set_or(PySetObject *so, PyObject *other) Py_RETURN_NOTIMPLEMENTED; result = (PySetObject *)set_copy(so, NULL); - if (result == NULL) { + if (result == NULL) return NULL; - } - if (Py_Is((PyObject *)so, other)) { + if ((PyObject *)so == other) return (PyObject *)result; - } - if (set_update_local(result, other)) { + if (set_update_internal(result, other)) { Py_DECREF(result); return NULL; } @@ -1325,9 +1230,8 @@ set_ior(PySetObject *so, PyObject *other) if (!PyAnySet_Check(other)) Py_RETURN_NOTIMPLEMENTED; - if (set_update_internal(so, other)) { + if (set_update_internal(so, other)) return NULL; - } return Py_NewRef(so); } @@ -1340,7 +1244,7 @@ set_intersection(PySetObject *so, PyObject *other) int rv; if ((PyObject *)so == other) - return set_copy_impl(so); + return set_copy(so, NULL); result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL); if (result == NULL) @@ -1429,17 +1333,13 @@ set_intersection_multi_impl(PySetObject *so, PyObject *args) { Py_ssize_t i; - if (PyTuple_GET_SIZE(args) == 0) { + if (PyTuple_GET_SIZE(args) == 0) return set_copy(so, NULL); - } PyObject *result = Py_NewRef(so); for (i=0 ; ikey); - Py_hash_t hash = entry->hash; - int rv = set_discard_entry(so, key, hash); - if (rv < 0) { - Py_DECREF(key); - return -1; + return NULL; } if (rv == DISCARD_NOTFOUND) { if (set_add_entry(so, key, hash)) { Py_DECREF(key); - return -1; + return NULL; } } Py_DECREF(key); } - return 0; + Py_RETURN_NONE; } /*[clinic input] @@ -1905,41 +1739,58 @@ static PyObject * set_symmetric_difference_update(PySetObject *so, PyObject *other) /*[clinic end generated code: output=fbb049c0806028de input=a50acf0365e1f0a5]*/ { - if (Py_Is((PyObject *)so, other)) { + PySetObject *otherset; + PyObject *key; + Py_ssize_t pos = 0; + Py_hash_t hash; + setentry *entry; + int rv; + + if ((PyObject *)so == other) return set_clear(so, NULL); - } - int rv; if (PyDict_CheckExact(other)) { - Py_BEGIN_CRITICAL_SECTION2(so, other); - rv = set_symmetric_difference_update_dict(so, other); - Py_END_CRITICAL_SECTION2(); - } - else if (PyAnySet_Check(other)) { - Py_BEGIN_CRITICAL_SECTION2(so, other); - rv = set_symmetric_difference_update_set(so, (PySetObject *)other); - Py_END_CRITICAL_SECTION2(); - } - else { - PySetObject *otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); - if (otherset == NULL) { - return NULL; - } + PyObject *res; - Py_BEGIN_CRITICAL_SECTION(so); - rv = set_symmetric_difference_update_set(so, otherset); + Py_BEGIN_CRITICAL_SECTION(other); + res = set_symmetric_difference_update_dict(so, other); Py_END_CRITICAL_SECTION(); - Py_DECREF(otherset); + return res; } - if (rv < 0) { - return NULL; + + if (PyAnySet_Check(other)) { + otherset = (PySetObject *)Py_NewRef(other); + } else { + otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); + if (otherset == NULL) + return NULL; + } + + while (set_next(otherset, &pos, &entry)) { + key = entry->key; + hash = entry->hash; + Py_INCREF(key); + rv = set_discard_entry(so, key, hash); + if (rv < 0) { + Py_DECREF(otherset); + Py_DECREF(key); + return NULL; + } + if (rv == DISCARD_NOTFOUND) { + if (set_add_entry(so, key, hash)) { + Py_DECREF(otherset); + Py_DECREF(key); + return NULL; + } + } + Py_DECREF(key); } + Py_DECREF(otherset); Py_RETURN_NONE; } /*[clinic input] -@critical_section so other set.symmetric_difference so: setobject other: object @@ -1949,22 +1800,22 @@ Return a new set with elements in either the set or other but not both. [clinic start generated code]*/ static PyObject * -set_symmetric_difference_impl(PySetObject *so, PyObject *other) -/*[clinic end generated code: output=270ee0b5d42b0797 input=624f6e7bbdf70db1]*/ +set_symmetric_difference(PySetObject *so, PyObject *other) +/*[clinic end generated code: output=f95364211b88775a input=f18af370ad72ebac]*/ { - PySetObject *result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL); - if (result == NULL) { - return NULL; - } - if (set_update_lock_held(result, other) < 0) { - Py_DECREF(result); + PyObject *rv; + PySetObject *otherset; + + otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); + if (otherset == NULL) return NULL; - } - if (set_symmetric_difference_update_set(result, so) < 0) { - Py_DECREF(result); + rv = set_symmetric_difference_update(otherset, (PyObject *)so); + if (rv == NULL) { + Py_DECREF(otherset); return NULL; } - return (PyObject *)result; + Py_DECREF(rv); + return (PyObject *)otherset; } static PyObject * @@ -1990,7 +1841,6 @@ set_ixor(PySetObject *so, PyObject *other) } /*[clinic input] -@critical_section so other set.issubset so: setobject other: object @@ -2000,8 +1850,8 @@ Report whether another set contains this set. [clinic start generated code]*/ static PyObject * -set_issubset_impl(PySetObject *so, PyObject *other) -/*[clinic end generated code: output=b2b59d5f314555ce input=f2a4fd0f2537758b]*/ +set_issubset(PySetObject *so, PyObject *other) +/*[clinic end generated code: output=78aef1f377aedef1 input=37fbc579b609db0c]*/ { setentry *entry; Py_ssize_t pos = 0; @@ -2035,7 +1885,6 @@ set_issubset_impl(PySetObject *so, PyObject *other) } /*[clinic input] -@critical_section so other set.issuperset so: setobject other: object @@ -2045,8 +1894,8 @@ Report whether this set contains another set. [clinic start generated code]*/ static PyObject * -set_issuperset_impl(PySetObject *so, PyObject *other) -/*[clinic end generated code: output=ecf00ce552c09461 input=5f2e1f262e6e4ccc]*/ +set_issuperset(PySetObject *so, PyObject *other) +/*[clinic end generated code: output=7d2b71dd714a7ec7 input=fd5dab052f2e9bb3]*/ { if (PyAnySet_Check(other)) { return set_issubset((PySetObject *)other, (PyObject *)so); @@ -2075,7 +1924,6 @@ set_issuperset_impl(PySetObject *so, PyObject *other) Py_RETURN_TRUE; } -// TODO: Make thread-safe in free-threaded builds static PyObject * set_richcompare(PySetObject *v, PyObject *w, int op) { @@ -2120,7 +1968,6 @@ set_richcompare(PySetObject *v, PyObject *w, int op) } /*[clinic input] -@critical_section set.add so: setobject object as key: object @@ -2132,16 +1979,16 @@ This has no effect if the element is already present. [clinic start generated code]*/ static PyObject * -set_add_impl(PySetObject *so, PyObject *key) -/*[clinic end generated code: output=4cc4a937f1425c96 input=03baf62cb0e66514]*/ +set_add(PySetObject *so, PyObject *key) +/*[clinic end generated code: output=cd9c2d5c2069c2ba input=96f1efe029e47972]*/ { if (set_add_key(so, key)) return NULL; Py_RETURN_NONE; } -static int -set_contains_lock_held(PySetObject *so, PyObject *key) +int +_PySet_Contains(PySetObject *so, PyObject *key) { PyObject *tmpkey; int rv; @@ -2160,18 +2007,7 @@ set_contains_lock_held(PySetObject *so, PyObject *key) return rv; } -int -_PySet_Contains(PySetObject *so, PyObject *key) -{ - int rv; - Py_BEGIN_CRITICAL_SECTION(so); - rv = set_contains_lock_held(so, key); - Py_END_CRITICAL_SECTION(); - return rv; -} - /*[clinic input] -@critical_section @coexist set.__contains__ so: setobject @@ -2182,19 +2018,18 @@ x.__contains__(y) <==> y in x. [clinic start generated code]*/ static PyObject * -set___contains___impl(PySetObject *so, PyObject *key) -/*[clinic end generated code: output=b44863d034b3c70e input=4a7d568459617f24]*/ +set___contains__(PySetObject *so, PyObject *key) +/*[clinic end generated code: output=b5948bc5c590d3ca input=cf4c72db704e4cf0]*/ { long result; - result = set_contains_lock_held(so, key); + result = _PySet_Contains(so, key); if (result < 0) return NULL; return PyBool_FromLong(result); } /*[clinic input] -@critical_section set.remove so: setobject object as key: object @@ -2206,8 +2041,8 @@ If the element is not a member, raise a KeyError. [clinic start generated code]*/ static PyObject * -set_remove_impl(PySetObject *so, PyObject *key) -/*[clinic end generated code: output=0b9134a2a2200363 input=893e1cb1df98227a]*/ +set_remove(PySetObject *so, PyObject *key) +/*[clinic end generated code: output=08ae496d0cd2b8c1 input=10132515dfe8ebd7]*/ { PyObject *tmpkey; int rv; @@ -2234,7 +2069,6 @@ set_remove_impl(PySetObject *so, PyObject *key) } /*[clinic input] -@critical_section set.discard so: setobject object as key: object @@ -2247,8 +2081,8 @@ an exception when an element is missing from the set. [clinic start generated code]*/ static PyObject * -set_discard_impl(PySetObject *so, PyObject *key) -/*[clinic end generated code: output=eec3b687bf32759e input=861cb7fb69b4def0]*/ +set_discard(PySetObject *so, PyObject *key) +/*[clinic end generated code: output=9181b60d7bb7d480 input=82a689eba94d5ad9]*/ { PyObject *tmpkey; int rv; @@ -2270,7 +2104,6 @@ set_discard_impl(PySetObject *so, PyObject *key) } /*[clinic input] -@critical_section set.__reduce__ so: setobject @@ -2279,7 +2112,7 @@ Return state information for pickling. static PyObject * set___reduce___impl(PySetObject *so) -/*[clinic end generated code: output=9af7d0e029df87ee input=59405a4249e82f71]*/ +/*[clinic end generated code: output=9af7d0e029df87ee input=531375e87a24a449]*/ { PyObject *keys=NULL, *args=NULL, *result=NULL, *state=NULL; @@ -2301,7 +2134,6 @@ set___reduce___impl(PySetObject *so) } /*[clinic input] -@critical_section set.__sizeof__ so: setobject @@ -2310,7 +2142,7 @@ S.__sizeof__() -> size of S in memory, in bytes. static PyObject * set___sizeof___impl(PySetObject *so) -/*[clinic end generated code: output=4bfa3df7bd38ed88 input=09e1a09f168eaa23]*/ +/*[clinic end generated code: output=4bfa3df7bd38ed88 input=0f214fc2225319fc]*/ { size_t res = _PyObject_SIZE(Py_TYPE(so)); if (so->table != so->smalltable) { @@ -2324,17 +2156,13 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds) { PyObject *iterable = NULL; - if (!_PyArg_NoKeywords("set", kwds)) + if (!_PyArg_NoKeywords("set", kwds)) return -1; if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable)) return -1; - - Py_BEGIN_CRITICAL_SECTION(self); if (self->fill) set_clear_internal(self); self->hash = -1; - Py_END_CRITICAL_SECTION(); - if (iterable == NULL) return 0; return set_update_internal(self, iterable); @@ -2363,7 +2191,7 @@ set_vectorcall(PyObject *type, PyObject * const*args, } static PySequenceMethods set_as_sequence = { - (lenfunc)set_len, /* sq_length */ + set_len, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ 0, /* sq_item */ @@ -2596,7 +2424,7 @@ PySet_Size(PyObject *anyset) PyErr_BadInternalCall(); return -1; } - return set_len((PySetObject *)anyset); + return PySet_GET_SIZE(anyset); } int @@ -2606,8 +2434,7 @@ PySet_Clear(PyObject *set) PyErr_BadInternalCall(); return -1; } - (void)set_clear((PySetObject *)set, NULL); - return 0; + return set_clear_internal((PySetObject *)set); } int @@ -2617,12 +2444,7 @@ PySet_Contains(PyObject *anyset, PyObject *key) PyErr_BadInternalCall(); return -1; } - - int rv; - Py_BEGIN_CRITICAL_SECTION(anyset); - rv = set_contains_key((PySetObject *)anyset, key); - Py_END_CRITICAL_SECTION(); - return rv; + return set_contains_key((PySetObject *)anyset, key); } int @@ -2632,12 +2454,7 @@ PySet_Discard(PyObject *set, PyObject *key) PyErr_BadInternalCall(); return -1; } - - int rv; - Py_BEGIN_CRITICAL_SECTION(set); - rv = set_discard_key((PySetObject *)set, key); - Py_END_CRITICAL_SECTION(); - return rv; + return set_discard_key((PySetObject *)set, key); } int @@ -2648,15 +2465,9 @@ PySet_Add(PyObject *anyset, PyObject *key) PyErr_BadInternalCall(); return -1; } - - int rv; - Py_BEGIN_CRITICAL_SECTION(anyset); - rv = set_add_key((PySetObject *)anyset, key); - Py_END_CRITICAL_SECTION(); - return rv; + return set_add_key((PySetObject *)anyset, key); } -// TODO: Make thread-safe in free-threaded builds int _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash) {