@@ -19,7 +19,7 @@ _PyWeakref_GetWeakrefCount(PyWeakReference *head)
19
19
return count ;
20
20
}
21
21
22
- static PyObject * weakref_vectorcall (PyWeakReference * self , PyObject * const * args , size_t nargsf , PyObject * kwnames );
22
+ static PyObject * weakref_vectorcall (PyObject * self , PyObject * const * args , size_t nargsf , PyObject * kwnames );
23
23
24
24
static void
25
25
init_weakref (PyWeakReference * self , PyObject * ob , PyObject * callback )
@@ -29,7 +29,7 @@ init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
29
29
self -> wr_prev = NULL ;
30
30
self -> wr_next = NULL ;
31
31
self -> wr_callback = Py_XNewRef (callback );
32
- self -> vectorcall = ( vectorcallfunc ) weakref_vectorcall ;
32
+ self -> vectorcall = weakref_vectorcall ;
33
33
}
34
34
35
35
static PyWeakReference *
@@ -129,7 +129,7 @@ gc_clear(PyWeakReference *self)
129
129
130
130
131
131
static PyObject *
132
- weakref_vectorcall (PyWeakReference * self , PyObject * const * args ,
132
+ weakref_vectorcall (PyObject * self , PyObject * const * args ,
133
133
size_t nargsf , PyObject * kwnames )
134
134
{
135
135
if (!_PyArg_NoKwnames ("weakref" , kwnames )) {
@@ -160,7 +160,7 @@ weakref_hash(PyWeakReference *self)
160
160
161
161
162
162
static PyObject *
163
- weakref_repr (PyWeakReference * self )
163
+ weakref_repr (PyObject * self )
164
164
{
165
165
PyObject * name , * repr ;
166
166
PyObject * obj = PyWeakref_GET_OBJECT (self );
@@ -174,17 +174,12 @@ weakref_repr(PyWeakReference *self)
174
174
if (name == NULL || !PyUnicode_Check (name )) {
175
175
repr = PyUnicode_FromFormat (
176
176
"<weakref at %p; to '%s' at %p>" ,
177
- self ,
178
- Py_TYPE (PyWeakref_GET_OBJECT (self ))-> tp_name ,
179
- obj );
177
+ self , Py_TYPE (obj )-> tp_name , obj );
180
178
}
181
179
else {
182
180
repr = PyUnicode_FromFormat (
183
181
"<weakref at %p; to '%s' at %p (%U)>" ,
184
- self ,
185
- Py_TYPE (PyWeakref_GET_OBJECT (self ))-> tp_name ,
186
- obj ,
187
- name );
182
+ self , Py_TYPE (obj )-> tp_name , obj , name );
188
183
}
189
184
Py_DECREF (obj );
190
185
Py_XDECREF (name );
@@ -203,8 +198,9 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
203
198
!PyWeakref_Check (other )) {
204
199
Py_RETURN_NOTIMPLEMENTED ;
205
200
}
206
- if (PyWeakref_GET_OBJECT (self ) == Py_None
207
- || PyWeakref_GET_OBJECT (other ) == Py_None ) {
201
+ PyObject * obj = PyWeakref_GET_OBJECT (self );
202
+ PyObject * other_obj = PyWeakref_GET_OBJECT (other );
203
+ if (obj == Py_None || other_obj == Py_None ) {
208
204
int res = (self == other );
209
205
if (op == Py_NE )
210
206
res = !res ;
@@ -213,8 +209,6 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
213
209
else
214
210
Py_RETURN_FALSE ;
215
211
}
216
- PyObject * obj = PyWeakref_GET_OBJECT (self );
217
- PyObject * other_obj = PyWeakref_GET_OBJECT (other );
218
212
Py_INCREF (obj );
219
213
Py_INCREF (other_obj );
220
214
PyObject * res = PyObject_RichCompare (obj , other_obj , op );
@@ -372,7 +366,7 @@ _PyWeakref_RefType = {
372
366
.tp_dealloc = weakref_dealloc ,
373
367
.tp_vectorcall_offset = offsetof(PyWeakReference , vectorcall ),
374
368
.tp_call = PyVectorcall_Call ,
375
- .tp_repr = ( reprfunc ) weakref_repr ,
369
+ .tp_repr = weakref_repr ,
376
370
.tp_hash = (hashfunc )weakref_hash ,
377
371
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
378
372
Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_BASETYPE ,
@@ -388,15 +382,15 @@ _PyWeakref_RefType = {
388
382
};
389
383
390
384
391
- static int
392
- proxy_checkref ( PyWeakReference * proxy )
385
+ static bool
386
+ proxy_check_ref ( PyObject * obj )
393
387
{
394
- if (PyWeakref_GET_OBJECT ( proxy ) == Py_None ) {
388
+ if (obj == Py_None ) {
395
389
PyErr_SetString (PyExc_ReferenceError ,
396
390
"weakly-referenced object no longer exists" );
397
- return 0 ;
391
+ return false ;
398
392
}
399
- return 1 ;
393
+ return true ;
400
394
}
401
395
402
396
@@ -406,9 +400,9 @@ proxy_checkref(PyWeakReference *proxy)
406
400
*/
407
401
#define UNWRAP (o ) \
408
402
if (PyWeakref_CheckProxy(o)) { \
409
- if (!proxy_checkref((PyWeakReference *)o)) \
410
- return NULL; \
411
403
o = PyWeakref_GET_OBJECT(o); \
404
+ if (!proxy_check_ref(o)) \
405
+ return NULL; \
412
406
}
413
407
414
408
#define WRAP_UNARY (method , generic ) \
@@ -483,11 +477,12 @@ proxy_repr(PyWeakReference *proxy)
483
477
484
478
485
479
static int
486
- proxy_setattr (PyWeakReference * proxy , PyObject * name , PyObject * value )
480
+ proxy_setattr (PyObject * proxy , PyObject * name , PyObject * value )
487
481
{
488
- if (!proxy_checkref (proxy ))
489
- return -1 ;
490
482
PyObject * obj = PyWeakref_GET_OBJECT (proxy );
483
+ if (!proxy_check_ref (obj )) {
484
+ return -1 ;
485
+ }
491
486
Py_INCREF (obj );
492
487
int res = PyObject_SetAttr (obj , name , value );
493
488
Py_DECREF (obj );
@@ -539,10 +534,10 @@ WRAP_BINARY(proxy_matmul, PyNumber_MatrixMultiply)
539
534
WRAP_BINARY (proxy_imatmul , PyNumber_InPlaceMatrixMultiply )
540
535
541
536
static int
542
- proxy_bool (PyWeakReference * proxy )
537
+ proxy_bool (PyObject * proxy )
543
538
{
544
539
PyObject * o = PyWeakref_GET_OBJECT (proxy );
545
- if (!proxy_checkref ( proxy )) {
540
+ if (!proxy_check_ref ( o )) {
546
541
return -1 ;
547
542
}
548
543
Py_INCREF (o );
@@ -564,12 +559,12 @@ proxy_dealloc(PyWeakReference *self)
564
559
/* sequence slots */
565
560
566
561
static int
567
- proxy_contains (PyWeakReference * proxy , PyObject * value )
562
+ proxy_contains (PyObject * proxy , PyObject * value )
568
563
{
569
- if (!proxy_checkref (proxy ))
570
- return -1 ;
571
-
572
564
PyObject * obj = PyWeakref_GET_OBJECT (proxy );
565
+ if (!proxy_check_ref (obj )) {
566
+ return -1 ;
567
+ }
573
568
Py_INCREF (obj );
574
569
int res = PySequence_Contains (obj , value );
575
570
Py_DECREF (obj );
@@ -579,12 +574,12 @@ proxy_contains(PyWeakReference *proxy, PyObject *value)
579
574
/* mapping slots */
580
575
581
576
static Py_ssize_t
582
- proxy_length (PyWeakReference * proxy )
577
+ proxy_length (PyObject * proxy )
583
578
{
584
- if (!proxy_checkref (proxy ))
585
- return -1 ;
586
-
587
579
PyObject * obj = PyWeakref_GET_OBJECT (proxy );
580
+ if (!proxy_check_ref (obj )) {
581
+ return -1 ;
582
+ }
588
583
Py_INCREF (obj );
589
584
Py_ssize_t res = PyObject_Length (obj );
590
585
Py_DECREF (obj );
@@ -594,12 +589,12 @@ proxy_length(PyWeakReference *proxy)
594
589
WRAP_BINARY (proxy_getitem , PyObject_GetItem )
595
590
596
591
static int
597
- proxy_setitem (PyWeakReference * proxy , PyObject * key , PyObject * value )
592
+ proxy_setitem (PyObject * proxy , PyObject * key , PyObject * value )
598
593
{
599
- if (!proxy_checkref (proxy ))
600
- return -1 ;
601
-
602
594
PyObject * obj = PyWeakref_GET_OBJECT (proxy );
595
+ if (!proxy_check_ref (obj )) {
596
+ return -1 ;
597
+ }
603
598
Py_INCREF (obj );
604
599
int res ;
605
600
if (value == NULL ) {
@@ -614,24 +609,25 @@ proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)
614
609
/* iterator slots */
615
610
616
611
static PyObject *
617
- proxy_iter (PyWeakReference * proxy )
612
+ proxy_iter (PyObject * proxy )
618
613
{
619
- if (!proxy_checkref (proxy ))
620
- return NULL ;
621
614
PyObject * obj = PyWeakref_GET_OBJECT (proxy );
615
+ if (!proxy_check_ref (obj )) {
616
+ return NULL ;
617
+ }
622
618
Py_INCREF (obj );
623
619
PyObject * res = PyObject_GetIter (obj );
624
620
Py_DECREF (obj );
625
621
return res ;
626
622
}
627
623
628
624
static PyObject *
629
- proxy_iternext (PyWeakReference * proxy )
625
+ proxy_iternext (PyObject * proxy )
630
626
{
631
- if (!proxy_checkref (proxy ))
632
- return NULL ;
633
-
634
627
PyObject * obj = PyWeakref_GET_OBJECT (proxy );
628
+ if (!proxy_check_ref (obj )) {
629
+ return NULL ;
630
+ }
635
631
if (!PyIter_Check (obj )) {
636
632
PyErr_Format (PyExc_TypeError ,
637
633
"Weakref proxy referenced a non-iterator '%.200s' object" ,
@@ -666,7 +662,7 @@ static PyNumberMethods proxy_as_number = {
666
662
proxy_neg , /*nb_negative*/
667
663
proxy_pos , /*nb_positive*/
668
664
proxy_abs , /*nb_absolute*/
669
- ( inquiry ) proxy_bool , /*nb_bool*/
665
+ proxy_bool , /*nb_bool*/
670
666
proxy_invert , /*nb_invert*/
671
667
proxy_lshift , /*nb_lshift*/
672
668
proxy_rshift , /*nb_rshift*/
@@ -696,20 +692,20 @@ static PyNumberMethods proxy_as_number = {
696
692
};
697
693
698
694
static PySequenceMethods proxy_as_sequence = {
699
- ( lenfunc ) proxy_length , /*sq_length*/
695
+ proxy_length , /*sq_length*/
700
696
0 , /*sq_concat*/
701
697
0 , /*sq_repeat*/
702
698
0 , /*sq_item*/
703
699
0 , /*sq_slice*/
704
700
0 , /*sq_ass_item*/
705
- 0 , /*sq_ass_slice*/
706
- ( objobjproc ) proxy_contains , /* sq_contains */
701
+ 0 , /*sq_ass_slice*/
702
+ proxy_contains , /* sq_contains */
707
703
};
708
704
709
705
static PyMappingMethods proxy_as_mapping = {
710
- ( lenfunc ) proxy_length , /*mp_length*/
706
+ proxy_length , /*mp_length*/
711
707
proxy_getitem , /*mp_subscript*/
712
- ( objobjargproc ) proxy_setitem , /*mp_ass_subscript*/
708
+ proxy_setitem , /*mp_ass_subscript*/
713
709
};
714
710
715
711
@@ -734,17 +730,17 @@ _PyWeakref_ProxyType = {
734
730
0 , /* tp_call */
735
731
proxy_str , /* tp_str */
736
732
proxy_getattr , /* tp_getattro */
737
- ( setattrofunc ) proxy_setattr , /* tp_setattro */
733
+ proxy_setattr , /* tp_setattro */
738
734
0 , /* tp_as_buffer */
739
735
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC , /* tp_flags */
740
736
0 , /* tp_doc */
741
737
(traverseproc )gc_traverse , /* tp_traverse */
742
738
(inquiry )gc_clear , /* tp_clear */
743
739
proxy_richcompare , /* tp_richcompare */
744
740
0 , /* tp_weaklistoffset */
745
- ( getiterfunc ) proxy_iter , /* tp_iter */
746
- ( iternextfunc ) proxy_iternext , /* tp_iternext */
747
- proxy_methods , /* tp_methods */
741
+ proxy_iter , /* tp_iter */
742
+ proxy_iternext , /* tp_iternext */
743
+ proxy_methods , /* tp_methods */
748
744
};
749
745
750
746
@@ -768,16 +764,16 @@ _PyWeakref_CallableProxyType = {
768
764
proxy_call , /* tp_call */
769
765
proxy_str , /* tp_str */
770
766
proxy_getattr , /* tp_getattro */
771
- ( setattrofunc ) proxy_setattr , /* tp_setattro */
767
+ proxy_setattr , /* tp_setattro */
772
768
0 , /* tp_as_buffer */
773
769
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC , /* tp_flags */
774
770
0 , /* tp_doc */
775
771
(traverseproc )gc_traverse , /* tp_traverse */
776
772
(inquiry )gc_clear , /* tp_clear */
777
773
proxy_richcompare , /* tp_richcompare */
778
774
0 , /* tp_weaklistoffset */
779
- ( getiterfunc ) proxy_iter , /* tp_iter */
780
- ( iternextfunc ) proxy_iternext , /* tp_iternext */
775
+ proxy_iter , /* tp_iter */
776
+ proxy_iternext , /* tp_iternext */
781
777
};
782
778
783
779
0 commit comments