@@ -145,11 +145,14 @@ weakref_hash(PyWeakReference *self)
145
145
{
146
146
if (self -> hash != -1 )
147
147
return self -> hash ;
148
- if (PyWeakref_GET_OBJECT (self ) == Py_None ) {
148
+ PyObject * obj = PyWeakref_GET_OBJECT (self );
149
+ if (obj == Py_None ) {
149
150
PyErr_SetString (PyExc_TypeError , "weak object has gone away" );
150
151
return -1 ;
151
152
}
152
- self -> hash = PyObject_Hash (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
@@ -159,28 +162,33 @@ weakref_repr(PyWeakReference *self)
159
162
{
160
163
PyObject * name , * repr ;
161
164
_Py_IDENTIFIER (__name__ );
165
+ PyObject * obj = PyWeakref_GET_OBJECT (self );
162
166
163
- if (PyWeakref_GET_OBJECT ( self ) == Py_None )
167
+ if (obj == Py_None ) {
164
168
return PyUnicode_FromFormat ("<weakref at %p; dead>" , self );
169
+ }
165
170
166
- if (_PyObject_LookupAttrId (PyWeakref_GET_OBJECT (self ), & PyId___name__ , & name ) < 0 ) {
171
+ Py_INCREF (obj );
172
+ if (_PyObject_LookupAttrId (obj , & PyId___name__ , & name ) < 0 ) {
173
+ Py_DECREF (obj );
167
174
return NULL ;
168
175
}
169
176
if (name == NULL || !PyUnicode_Check (name )) {
170
177
repr = PyUnicode_FromFormat (
171
178
"<weakref at %p; to '%s' at %p>" ,
172
179
self ,
173
180
Py_TYPE (PyWeakref_GET_OBJECT (self ))-> tp_name ,
174
- PyWeakref_GET_OBJECT ( self ) );
181
+ obj );
175
182
}
176
183
else {
177
184
repr = PyUnicode_FromFormat (
178
185
"<weakref at %p; to '%s' at %p (%U)>" ,
179
186
self ,
180
187
Py_TYPE (PyWeakref_GET_OBJECT (self ))-> tp_name ,
181
- PyWeakref_GET_OBJECT ( self ) ,
188
+ obj ,
182
189
name );
183
190
}
191
+ Py_DECREF (obj );
184
192
Py_XDECREF (name );
185
193
return repr ;
186
194
}
@@ -207,8 +215,14 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
207
215
else
208
216
Py_RETURN_FALSE ;
209
217
}
210
- return PyObject_RichCompare (PyWeakref_GET_OBJECT (self ),
211
- PyWeakref_GET_OBJECT (other ), op );
218
+ PyObject * obj = PyWeakref_GET_OBJECT (self );
219
+ PyObject * other_obj = PyWeakref_GET_OBJECT (other );
220
+ Py_INCREF (obj );
221
+ Py_INCREF (other_obj );
222
+ PyObject * res = PyObject_RichCompare (obj , other_obj , op );
223
+ Py_DECREF (obj );
224
+ Py_DECREF (other_obj );
225
+ return res ;
212
226
}
213
227
214
228
/* Given the head of an object's list of weak references, extract the
@@ -415,26 +429,27 @@ proxy_checkref(PyWeakReference *proxy)
415
429
o = PyWeakref_GET_OBJECT(o); \
416
430
}
417
431
418
- #define UNWRAP_I (o ) \
419
- if (PyWeakref_CheckProxy(o)) { \
420
- if (!proxy_checkref((PyWeakReference *)o)) \
421
- return -1; \
422
- o = PyWeakref_GET_OBJECT(o); \
423
- }
424
-
425
432
#define WRAP_UNARY (method , generic ) \
426
433
static PyObject * \
427
434
method(PyObject *proxy) { \
428
435
UNWRAP(proxy); \
429
- return generic(proxy); \
436
+ Py_INCREF(proxy); \
437
+ PyObject* res = generic(proxy); \
438
+ Py_DECREF(proxy); \
439
+ return res; \
430
440
}
431
441
432
442
#define WRAP_BINARY (method , generic ) \
433
443
static PyObject * \
434
444
method(PyObject *x, PyObject *y) { \
435
445
UNWRAP(x); \
436
446
UNWRAP(y); \
437
- return generic(x, y); \
447
+ Py_INCREF(x); \
448
+ Py_INCREF(y); \
449
+ PyObject* res = generic(x, y); \
450
+ Py_DECREF(x); \
451
+ Py_DECREF(y); \
452
+ return res; \
438
453
}
439
454
440
455
/* Note that the third arg needs to be checked for NULL since the tp_call
@@ -447,15 +462,25 @@ proxy_checkref(PyWeakReference *proxy)
447
462
UNWRAP(v); \
448
463
if (w != NULL) \
449
464
UNWRAP(w); \
450
- return generic(proxy, v, w); \
465
+ Py_INCREF(proxy); \
466
+ Py_INCREF(v); \
467
+ Py_XINCREF(w); \
468
+ PyObject* res = generic(proxy, v, w); \
469
+ Py_DECREF(proxy); \
470
+ Py_DECREF(v); \
471
+ Py_XDECREF(w); \
472
+ return res; \
451
473
}
452
474
453
475
#define WRAP_METHOD (method , special ) \
454
476
static PyObject * \
455
477
method(PyObject *proxy, PyObject *Py_UNUSED(ignored)) { \
456
478
_Py_IDENTIFIER(special); \
457
479
UNWRAP(proxy); \
458
- return _PyObject_CallMethodIdNoArgs(proxy, &PyId_##special); \
480
+ Py_INCREF(proxy); \
481
+ PyObject* res = _PyObject_CallMethodIdNoArgs(proxy, &PyId_##special); \
482
+ Py_DECREF(proxy); \
483
+ return res; \
459
484
}
460
485
461
486
@@ -481,7 +506,11 @@ proxy_setattr(PyWeakReference *proxy, PyObject *name, PyObject *value)
481
506
{
482
507
if (!proxy_checkref (proxy ))
483
508
return -1 ;
484
- return PyObject_SetAttr (PyWeakref_GET_OBJECT (proxy ), name , value );
509
+ PyObject * obj = PyWeakref_GET_OBJECT (proxy );
510
+ Py_INCREF (obj );
511
+ int res = PyObject_SetAttr (obj , name , value );
512
+ Py_DECREF (obj );
513
+ return res ;
485
514
}
486
515
487
516
static PyObject *
@@ -532,9 +561,13 @@ static int
532
561
proxy_bool (PyWeakReference * proxy )
533
562
{
534
563
PyObject * o = PyWeakref_GET_OBJECT (proxy );
535
- if (!proxy_checkref (proxy ))
564
+ if (!proxy_checkref (proxy )) {
536
565
return -1 ;
537
- return PyObject_IsTrue (o );
566
+ }
567
+ Py_INCREF (o );
568
+ int res = PyObject_IsTrue (o );
569
+ Py_DECREF (o );
570
+ return res ;
538
571
}
539
572
540
573
static void
@@ -553,9 +586,13 @@ proxy_contains(PyWeakReference *proxy, PyObject *value)
553
586
{
554
587
if (!proxy_checkref (proxy ))
555
588
return -1 ;
556
- return PySequence_Contains (PyWeakref_GET_OBJECT (proxy ), value );
557
- }
558
589
590
+ PyObject * obj = PyWeakref_GET_OBJECT (proxy );
591
+ Py_INCREF (obj );
592
+ int res = PySequence_Contains (obj , value );
593
+ Py_DECREF (obj );
594
+ return res ;
595
+ }
559
596
560
597
/* mapping slots */
561
598
@@ -564,7 +601,12 @@ proxy_length(PyWeakReference *proxy)
564
601
{
565
602
if (!proxy_checkref (proxy ))
566
603
return -1 ;
567
- return PyObject_Length (PyWeakref_GET_OBJECT (proxy ));
604
+
605
+ PyObject * obj = PyWeakref_GET_OBJECT (proxy );
606
+ Py_INCREF (obj );
607
+ Py_ssize_t res = PyObject_Length (obj );
608
+ Py_DECREF (obj );
609
+ return res ;
568
610
}
569
611
570
612
WRAP_BINARY (proxy_getitem , PyObject_GetItem )
@@ -575,10 +617,16 @@ proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)
575
617
if (!proxy_checkref (proxy ))
576
618
return -1 ;
577
619
578
- if (value == NULL )
579
- return PyObject_DelItem (PyWeakref_GET_OBJECT (proxy ), key );
580
- else
581
- return PyObject_SetItem (PyWeakref_GET_OBJECT (proxy ), key , value );
620
+ PyObject * obj = PyWeakref_GET_OBJECT (proxy );
621
+ Py_INCREF (obj );
622
+ int res ;
623
+ if (value == NULL ) {
624
+ res = PyObject_DelItem (obj , key );
625
+ } else {
626
+ res = PyObject_SetItem (obj , key , value );
627
+ }
628
+ Py_DECREF (obj );
629
+ return res ;
582
630
}
583
631
584
632
/* iterator slots */
@@ -588,15 +636,24 @@ proxy_iter(PyWeakReference *proxy)
588
636
{
589
637
if (!proxy_checkref (proxy ))
590
638
return NULL ;
591
- return PyObject_GetIter (PyWeakref_GET_OBJECT (proxy ));
639
+ PyObject * obj = PyWeakref_GET_OBJECT (proxy );
640
+ Py_INCREF (obj );
641
+ PyObject * res = PyObject_GetIter (obj );
642
+ Py_DECREF (obj );
643
+ return res ;
592
644
}
593
645
594
646
static PyObject *
595
647
proxy_iternext (PyWeakReference * proxy )
596
648
{
597
649
if (!proxy_checkref (proxy ))
598
650
return NULL ;
599
- return PyIter_Next (PyWeakref_GET_OBJECT (proxy ));
651
+
652
+ PyObject * obj = PyWeakref_GET_OBJECT (proxy );
653
+ Py_INCREF (obj );
654
+ PyObject * res = PyIter_Next (obj );
655
+ Py_DECREF (obj );
656
+ return res ;
600
657
}
601
658
602
659
0 commit comments