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