@@ -149,7 +149,10 @@ weakref_hash(PyWeakReference *self)
149
149
PyErr_SetString (PyExc_TypeError , "weak object has gone away" );
150
150
return -1 ;
151
151
}
152
- self -> hash = PyObject_Hash (PyWeakref_GET_OBJECT (self ));
152
+ PyObject * obj = PyWeakref_GET_OBJECT (self );
153
+ Py_INCREF (obj );
154
+ self -> hash = PyObject_Hash (obj );
155
+ Py_DECREF (obj );
153
156
return self -> hash ;
154
157
}
155
158
@@ -207,8 +210,14 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
207
210
else
208
211
Py_RETURN_FALSE ;
209
212
}
210
- return PyObject_RichCompare (PyWeakref_GET_OBJECT (self ),
211
- PyWeakref_GET_OBJECT (other ), op );
213
+ PyObject * obj = PyWeakref_GET_OBJECT (self );
214
+ PyObject * other_obj = PyWeakref_GET_OBJECT (other );
215
+ Py_INCREF (obj );
216
+ Py_INCREF (other );
217
+ PyObject * res = PyObject_RichCompare (obj , other_obj , op );
218
+ Py_DECREF (obj );
219
+ Py_DECREF (other );
220
+ return res ;
212
221
}
213
222
214
223
/* Given the head of an object's list of weak references, extract the
@@ -536,9 +545,13 @@ static int
536
545
proxy_bool (PyWeakReference * proxy )
537
546
{
538
547
PyObject * o = PyWeakref_GET_OBJECT (proxy );
539
- if (!proxy_checkref (proxy ))
548
+ if (!proxy_checkref (proxy )) {
540
549
return -1 ;
541
- return PyObject_IsTrue (o );
550
+ }
551
+ Py_INCREF (o );
552
+ int res = PyObject_IsTrue (o );
553
+ Py_DECREF (o );
554
+ return res ;
542
555
}
543
556
544
557
static void
@@ -585,12 +598,12 @@ WRAP_BINARY(proxy_getitem, PyObject_GetItem)
585
598
static int
586
599
proxy_setitem (PyWeakReference * proxy , PyObject * key , PyObject * value )
587
600
{
588
- int res = 0 ;
589
601
if (!proxy_checkref (proxy ))
590
602
return -1 ;
591
603
592
604
PyObject * obj = PyWeakref_GET_OBJECT (proxy );
593
605
Py_INCREF (obj );
606
+ int res ;
594
607
if (value == NULL ) {
595
608
res = PyObject_DelItem (obj , key );
596
609
} else {
0 commit comments