Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd46932e

Browse files
committed
gh-105927: Refactor weakrefobject.c
* Rename proxy_checkref() to proxy_check_ref().* proxy_check_ref() now checks the object, not the proxy.* Most functions take PyObject* instead of PyWeakReference*.* Remove redundant calls to PyWeakref_GET_OBJECT().
1 parent33f0a85 commitd46932e

File tree

1 file changed

+56
-60
lines changed

1 file changed

+56
-60
lines changed

‎Objects/weakrefobject.c

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ _PyWeakref_GetWeakrefCount(PyWeakReference *head)
1919
returncount;
2020
}
2121

22-
staticPyObject*weakref_vectorcall(PyWeakReference*self,PyObject*const*args,size_tnargsf,PyObject*kwnames);
22+
staticPyObject*weakref_vectorcall(PyObject*self,PyObject*const*args,size_tnargsf,PyObject*kwnames);
2323

2424
staticvoid
2525
init_weakref(PyWeakReference*self,PyObject*ob,PyObject*callback)
@@ -29,7 +29,7 @@ init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
2929
self->wr_prev=NULL;
3030
self->wr_next=NULL;
3131
self->wr_callback=Py_XNewRef(callback);
32-
self->vectorcall=(vectorcallfunc)weakref_vectorcall;
32+
self->vectorcall=weakref_vectorcall;
3333
}
3434

3535
staticPyWeakReference*
@@ -129,7 +129,7 @@ gc_clear(PyWeakReference *self)
129129

130130

131131
staticPyObject*
132-
weakref_vectorcall(PyWeakReference*self,PyObject*const*args,
132+
weakref_vectorcall(PyObject*self,PyObject*const*args,
133133
size_tnargsf,PyObject*kwnames)
134134
{
135135
if (!_PyArg_NoKwnames("weakref",kwnames)) {
@@ -160,7 +160,7 @@ weakref_hash(PyWeakReference *self)
160160

161161

162162
staticPyObject*
163-
weakref_repr(PyWeakReference*self)
163+
weakref_repr(PyObject*self)
164164
{
165165
PyObject*name,*repr;
166166
PyObject*obj=PyWeakref_GET_OBJECT(self);
@@ -174,17 +174,12 @@ weakref_repr(PyWeakReference *self)
174174
if (name==NULL|| !PyUnicode_Check(name)) {
175175
repr=PyUnicode_FromFormat(
176176
"<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);
180178
}
181179
else {
182180
repr=PyUnicode_FromFormat(
183181
"<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);
188183
}
189184
Py_DECREF(obj);
190185
Py_XDECREF(name);
@@ -203,8 +198,9 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
203198
!PyWeakref_Check(other)) {
204199
Py_RETURN_NOTIMPLEMENTED;
205200
}
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) {
208204
intres= (self==other);
209205
if (op==Py_NE)
210206
res= !res;
@@ -213,8 +209,6 @@ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
213209
else
214210
Py_RETURN_FALSE;
215211
}
216-
PyObject*obj=PyWeakref_GET_OBJECT(self);
217-
PyObject*other_obj=PyWeakref_GET_OBJECT(other);
218212
Py_INCREF(obj);
219213
Py_INCREF(other_obj);
220214
PyObject*res=PyObject_RichCompare(obj,other_obj,op);
@@ -372,7 +366,7 @@ _PyWeakref_RefType = {
372366
.tp_dealloc=weakref_dealloc,
373367
.tp_vectorcall_offset= offsetof(PyWeakReference,vectorcall),
374368
.tp_call=PyVectorcall_Call,
375-
.tp_repr=(reprfunc)weakref_repr,
369+
.tp_repr=weakref_repr,
376370
.tp_hash= (hashfunc)weakref_hash,
377371
.tp_flags=Py_TPFLAGS_DEFAULT |Py_TPFLAGS_HAVE_GC |
378372
Py_TPFLAGS_HAVE_VECTORCALL |Py_TPFLAGS_BASETYPE,
@@ -388,15 +382,15 @@ _PyWeakref_RefType = {
388382
};
389383

390384

391-
staticint
392-
proxy_checkref(PyWeakReference*proxy)
385+
staticbool
386+
proxy_check_ref(PyObject*obj)
393387
{
394-
if (PyWeakref_GET_OBJECT(proxy)==Py_None) {
388+
if (obj==Py_None) {
395389
PyErr_SetString(PyExc_ReferenceError,
396390
"weakly-referenced object no longer exists");
397-
return0;
391+
returnfalse;
398392
}
399-
return1;
393+
returntrue;
400394
}
401395

402396

@@ -406,9 +400,9 @@ proxy_checkref(PyWeakReference *proxy)
406400
*/
407401
#defineUNWRAP(o) \
408402
if (PyWeakref_CheckProxy(o)) { \
409-
if (!proxy_checkref((PyWeakReference *)o)) \
410-
return NULL; \
411403
o = PyWeakref_GET_OBJECT(o); \
404+
if (!proxy_check_ref(o)) \
405+
return NULL; \
412406
}
413407

414408
#defineWRAP_UNARY(method,generic) \
@@ -483,11 +477,12 @@ proxy_repr(PyWeakReference *proxy)
483477

484478

485479
staticint
486-
proxy_setattr(PyWeakReference*proxy,PyObject*name,PyObject*value)
480+
proxy_setattr(PyObject*proxy,PyObject*name,PyObject*value)
487481
{
488-
if (!proxy_checkref(proxy))
489-
return-1;
490482
PyObject*obj=PyWeakref_GET_OBJECT(proxy);
483+
if (!proxy_check_ref(obj)) {
484+
return-1;
485+
}
491486
Py_INCREF(obj);
492487
intres=PyObject_SetAttr(obj,name,value);
493488
Py_DECREF(obj);
@@ -539,10 +534,10 @@ WRAP_BINARY(proxy_matmul, PyNumber_MatrixMultiply)
539534
WRAP_BINARY(proxy_imatmul,PyNumber_InPlaceMatrixMultiply)
540535

541536
staticint
542-
proxy_bool(PyWeakReference*proxy)
537+
proxy_bool(PyObject*proxy)
543538
{
544539
PyObject*o=PyWeakref_GET_OBJECT(proxy);
545-
if (!proxy_checkref(proxy)) {
540+
if (!proxy_check_ref(o)) {
546541
return-1;
547542
}
548543
Py_INCREF(o);
@@ -564,12 +559,12 @@ proxy_dealloc(PyWeakReference *self)
564559
/* sequence slots */
565560

566561
staticint
567-
proxy_contains(PyWeakReference*proxy,PyObject*value)
562+
proxy_contains(PyObject*proxy,PyObject*value)
568563
{
569-
if (!proxy_checkref(proxy))
570-
return-1;
571-
572564
PyObject*obj=PyWeakref_GET_OBJECT(proxy);
565+
if (!proxy_check_ref(obj)) {
566+
return-1;
567+
}
573568
Py_INCREF(obj);
574569
intres=PySequence_Contains(obj,value);
575570
Py_DECREF(obj);
@@ -579,12 +574,12 @@ proxy_contains(PyWeakReference *proxy, PyObject *value)
579574
/* mapping slots */
580575

581576
staticPy_ssize_t
582-
proxy_length(PyWeakReference*proxy)
577+
proxy_length(PyObject*proxy)
583578
{
584-
if (!proxy_checkref(proxy))
585-
return-1;
586-
587579
PyObject*obj=PyWeakref_GET_OBJECT(proxy);
580+
if (!proxy_check_ref(obj)) {
581+
return-1;
582+
}
588583
Py_INCREF(obj);
589584
Py_ssize_tres=PyObject_Length(obj);
590585
Py_DECREF(obj);
@@ -594,12 +589,12 @@ proxy_length(PyWeakReference *proxy)
594589
WRAP_BINARY(proxy_getitem,PyObject_GetItem)
595590

596591
staticint
597-
proxy_setitem(PyWeakReference*proxy,PyObject*key,PyObject*value)
592+
proxy_setitem(PyObject*proxy,PyObject*key,PyObject*value)
598593
{
599-
if (!proxy_checkref(proxy))
600-
return-1;
601-
602594
PyObject*obj=PyWeakref_GET_OBJECT(proxy);
595+
if (!proxy_check_ref(obj)) {
596+
return-1;
597+
}
603598
Py_INCREF(obj);
604599
intres;
605600
if (value==NULL) {
@@ -614,24 +609,25 @@ proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)
614609
/* iterator slots */
615610

616611
staticPyObject*
617-
proxy_iter(PyWeakReference*proxy)
612+
proxy_iter(PyObject*proxy)
618613
{
619-
if (!proxy_checkref(proxy))
620-
returnNULL;
621614
PyObject*obj=PyWeakref_GET_OBJECT(proxy);
615+
if (!proxy_check_ref(obj)) {
616+
returnNULL;
617+
}
622618
Py_INCREF(obj);
623619
PyObject*res=PyObject_GetIter(obj);
624620
Py_DECREF(obj);
625621
returnres;
626622
}
627623

628624
staticPyObject*
629-
proxy_iternext(PyWeakReference*proxy)
625+
proxy_iternext(PyObject*proxy)
630626
{
631-
if (!proxy_checkref(proxy))
632-
returnNULL;
633-
634627
PyObject*obj=PyWeakref_GET_OBJECT(proxy);
628+
if (!proxy_check_ref(obj)) {
629+
returnNULL;
630+
}
635631
if (!PyIter_Check(obj)) {
636632
PyErr_Format(PyExc_TypeError,
637633
"Weakref proxy referenced a non-iterator '%.200s' object",
@@ -666,7 +662,7 @@ static PyNumberMethods proxy_as_number = {
666662
proxy_neg,/*nb_negative*/
667663
proxy_pos,/*nb_positive*/
668664
proxy_abs,/*nb_absolute*/
669-
(inquiry)proxy_bool,/*nb_bool*/
665+
proxy_bool,/*nb_bool*/
670666
proxy_invert,/*nb_invert*/
671667
proxy_lshift,/*nb_lshift*/
672668
proxy_rshift,/*nb_rshift*/
@@ -696,20 +692,20 @@ static PyNumberMethods proxy_as_number = {
696692
};
697693

698694
staticPySequenceMethodsproxy_as_sequence= {
699-
(lenfunc)proxy_length,/*sq_length*/
695+
proxy_length,/*sq_length*/
700696
0,/*sq_concat*/
701697
0,/*sq_repeat*/
702698
0,/*sq_item*/
703699
0,/*sq_slice*/
704700
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 */
707703
};
708704

709705
staticPyMappingMethodsproxy_as_mapping= {
710-
(lenfunc)proxy_length,/*mp_length*/
706+
proxy_length,/*mp_length*/
711707
proxy_getitem,/*mp_subscript*/
712-
(objobjargproc)proxy_setitem,/*mp_ass_subscript*/
708+
proxy_setitem,/*mp_ass_subscript*/
713709
};
714710

715711

@@ -734,17 +730,17 @@ _PyWeakref_ProxyType = {
734730
0,/* tp_call */
735731
proxy_str,/* tp_str */
736732
proxy_getattr,/* tp_getattro */
737-
(setattrofunc)proxy_setattr,/* tp_setattro */
733+
proxy_setattr,/* tp_setattro */
738734
0,/* tp_as_buffer */
739735
Py_TPFLAGS_DEFAULT |Py_TPFLAGS_HAVE_GC,/* tp_flags */
740736
0,/* tp_doc */
741737
(traverseproc)gc_traverse,/* tp_traverse */
742738
(inquiry)gc_clear,/* tp_clear */
743739
proxy_richcompare,/* tp_richcompare */
744740
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 */
748744
};
749745

750746

@@ -768,16 +764,16 @@ _PyWeakref_CallableProxyType = {
768764
proxy_call,/* tp_call */
769765
proxy_str,/* tp_str */
770766
proxy_getattr,/* tp_getattro */
771-
(setattrofunc)proxy_setattr,/* tp_setattro */
767+
proxy_setattr,/* tp_setattro */
772768
0,/* tp_as_buffer */
773769
Py_TPFLAGS_DEFAULT |Py_TPFLAGS_HAVE_GC,/* tp_flags */
774770
0,/* tp_doc */
775771
(traverseproc)gc_traverse,/* tp_traverse */
776772
(inquiry)gc_clear,/* tp_clear */
777773
proxy_richcompare,/* tp_richcompare */
778774
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 */
781777
};
782778

783779

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp