@@ -186,7 +186,23 @@ _DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw)
186186{
187187 DictRemoverObject * self = _DictRemoverObject_CAST (myself );
188188 if (self -> key && self -> dict ) {
189- if (-1 == PyDict_DelItem (self -> dict , self -> key )) {
189+ /* Only remove the entry if it is still the proxy that died: the
190+ key may have been reused for a new object in the meantime. */
191+ PyObject * proxy = PyTuple_GET_SIZE (args ) == 1
192+ ? PyTuple_GET_ITEM (args , 0 ) : NULL ;
193+ PyObject * current ;
194+ int rc ;
195+ Py_BEGIN_CRITICAL_SECTION (self -> dict );
196+ rc = PyDict_GetItemRef (self -> dict , self -> key , & current );
197+ if (rc == 1 ) {
198+ rc = 0 ;
199+ if (current == proxy ) {
200+ rc = PyDict_DelItem (self -> dict , self -> key );
201+ }
202+ Py_DECREF (current );
203+ }
204+ Py_END_CRITICAL_SECTION ();
205+ if (rc < 0 ) {
190206 PyErr_FormatUnraisable ("Exception ignored while "
191207 "calling _ctypes.DictRemover" );
192208 }
@@ -215,32 +231,72 @@ static PyType_Spec dictremover_spec = {
215231 .slots = dictremover_slots ,
216232};
217233
218- int
219- PyDict_SetItemProxy (ctypes_state * st , PyObject * dict , PyObject * key , PyObject * item )
234+ /* Cache `item` in `dict` under `key` through a weakref proxy, unless a live
235+ entry already exists. On success *presult is a new reference to whatever
236+ is cached for `key` afterwards: `item` itself, or the object another
237+ thread cached first.
238+ Returns 0 on success, -1 with an exception set on failure. */
239+ static int
240+ _PyDict_SetDefaultItemProxy (ctypes_state * st , PyObject * dict , PyObject * key ,
241+ PyObject * item , PyObject * * presult )
220242{
221- PyObject * obj ;
222- DictRemoverObject * remover ;
223- PyObject * proxy ;
224- int result ;
225-
226- obj = _PyObject_CallNoArgs ((PyObject * )st -> DictRemover_Type );
227- if (obj == NULL )
243+ PyObject * obj = _PyObject_CallNoArgs ((PyObject * )st -> DictRemover_Type );
244+ if (obj == NULL ) {
228245 return -1 ;
246+ }
229247
230- remover = (DictRemoverObject * )obj ;
248+ DictRemoverObject * remover = (DictRemoverObject * )obj ;
231249 assert (remover -> key == NULL );
232250 assert (remover -> dict == NULL );
233251 remover -> key = Py_NewRef (key );
234252 remover -> dict = Py_NewRef (dict );
235253
236- proxy = PyWeakref_NewProxy (item , obj );
254+ PyObject * proxy = PyWeakref_NewProxy (item , obj );
237255 Py_DECREF (obj );
238- if (proxy == NULL )
256+ if (proxy == NULL ) {
257+ return -1 ;
258+ }
259+
260+ PyObject * stored ;
261+ int present = PyDict_SetDefaultRef (dict , key , proxy , & stored );
262+ if (present < 0 ) {
263+ Py_DECREF (proxy );
239264 return -1 ;
265+ }
266+ if (present == 0 ) {
267+ /* Our proxy was inserted; `item` is the canonical object. */
268+ Py_DECREF (stored );
269+ Py_DECREF (proxy );
270+ * presult = Py_NewRef (item );
271+ return 0 ;
272+ }
240273
241- result = PyDict_SetItem (dict , key , proxy );
274+ /* Another thread cached an object for this key first. Prefer it, so that
275+ every caller gets the same object. */
276+ PyObject * existing = NULL ;
277+ int rc = 0 ;
278+ if (PyWeakref_CheckProxy (stored )) {
279+ rc = PyWeakref_GetRef (stored , & existing );
280+ }
281+ Py_DECREF (stored );
282+ if (rc < 0 ) {
283+ Py_DECREF (proxy );
284+ return -1 ;
285+ }
286+ if (existing != NULL ) {
287+ Py_DECREF (proxy );
288+ * presult = existing ;
289+ return 0 ;
290+ }
291+
292+ /* The cached entry had already been cleared; install ours instead. */
293+ rc = PyDict_SetItem (dict , key , proxy );
242294 Py_DECREF (proxy );
243- return result ;
295+ if (rc < 0 ) {
296+ return -1 ;
297+ }
298+ * presult = Py_NewRef (item );
299+ return 0 ;
244300}
245301
246302static int
@@ -5434,13 +5490,20 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
54345490 Py_DECREF (key );
54355491 return NULL ;
54365492 }
5437- if (PyDict_SetItemProxy (st , st -> array_cache , key , result ) < 0 ) {
5493+ PyObject * cached ;
5494+ int rc ;
5495+ Py_BEGIN_CRITICAL_SECTION (st -> array_cache );
5496+ rc = _PyDict_SetDefaultItemProxy (st , st -> array_cache , key , result ,
5497+ & cached );
5498+ Py_END_CRITICAL_SECTION ();
5499+ if (rc < 0 ) {
54385500 Py_DECREF (key );
54395501 Py_DECREF (result );
54405502 return NULL ;
54415503 }
54425504 Py_DECREF (key );
5443- return result ;
5505+ Py_DECREF (result );
5506+ return cached ;
54445507}
54455508
54465509
0 commit comments