File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2773,17 +2773,23 @@ PySet_Discard(PyObject *set, PyObject *key)
27732773int
27742774PySet_Add (PyObject * anyset , PyObject * key )
27752775{
2776- if (!PySet_Check (anyset ) &&
2777- (!PyFrozenSet_Check (anyset ) || !_PyObject_IsUniquelyReferenced (anyset ))) {
2778- PyErr_BadInternalCall ();
2779- return -1 ;
2776+ if (_PyObject_IsUniquelyReferenced (anyset ) && PyAnySet_Check (anyset )) {
2777+ // In free-threading, if the set or frozenset is uniquely referenced,
2778+ // no critical section is needed since only the owner thread is
2779+ // populating it.
2780+ return set_add_key ((PySetObject * )anyset , key );
27802781 }
27812782
2782- int rv ;
2783- Py_BEGIN_CRITICAL_SECTION (anyset );
2784- rv = set_add_key ((PySetObject * )anyset , key );
2785- Py_END_CRITICAL_SECTION ();
2786- return rv ;
2783+ if (PySet_Check (anyset )) {
2784+ int rv ;
2785+ Py_BEGIN_CRITICAL_SECTION (anyset );
2786+ rv = set_add_key ((PySetObject * )anyset , key );
2787+ Py_END_CRITICAL_SECTION ();
2788+ return rv ;
2789+ }
2790+
2791+ PyErr_BadInternalCall ();
2792+ return -1 ;
27872793}
27882794
27892795int
You can’t perform that action at this time.
0 commit comments