@@ -122,7 +122,7 @@ set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
122122static int set_table_resize (PySetObject * , Py_ssize_t );
123123
124124static int
125- set_add_entry (PySetObject * so , PyObject * key , Py_hash_t hash )
125+ set_add_entry_takeref (PySetObject * so , PyObject * key , Py_hash_t hash )
126126{
127127 setentry * table ;
128128 setentry * freeslot ;
@@ -133,12 +133,6 @@ set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
133133 int probes ;
134134 int cmp ;
135135
136- _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (so );
137-
138- /* Pre-increment is necessary to prevent arbitrary code in the rich
139- comparison from deallocating the key just before the insertion. */
140- Py_INCREF (key );
141-
142136 restart :
143137
144138 mask = so -> mask ;
@@ -209,6 +203,27 @@ set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
209203 return -1 ;
210204}
211205
206+ static int
207+ set_add_entry (PySetObject * so , PyObject * key , Py_hash_t hash )
208+ {
209+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (so );
210+
211+ return set_add_entry_takeref (so , Py_NewRef (key ), hash );
212+ }
213+
214+ int
215+ _PySet_AddTakeRef (PySetObject * so , PyObject * key )
216+ {
217+ Py_hash_t hash = _PyObject_HashFast (key );
218+ if (hash == -1 ) {
219+ Py_DECREF (key );
220+ return -1 ;
221+ }
222+ // We don't pre-increment here, the caller holds a strong
223+ // reference to the object which we are stealing.
224+ return set_add_entry_takeref (so , key , hash );
225+ }
226+
212227/*
213228Internal routine used by set_table_resize() to insert an item which is
214229known to be absent from the set. Besides the performance benefit,
0 commit comments