Skip to content

Commit 4f63d2d

Browse files
author
fdrake
committed
Add support for the iterator protocol to weakref proxy objects.
Part of fixing SF bug #591704. git-svn-id: http://svn.python.org/projects/python/trunk@28122 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 41c1511 commit 4f63d2d

1 file changed

Lines changed: 64 additions & 38 deletions

File tree

Objects/weakrefobject.c

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)
383383
return PyObject_SetItem(PyWeakref_GET_OBJECT(proxy), key, value);
384384
}
385385

386+
/* iterator slots */
387+
388+
static PyObject *
389+
proxy_iter(PyWeakReference *proxy)
390+
{
391+
if (!proxy_checkref(proxy))
392+
return NULL;
393+
return PyObject_GetIter(PyWeakref_GET_OBJECT(proxy));
394+
}
395+
396+
static PyObject *
397+
proxy_iternext(PyWeakReference *proxy)
398+
{
399+
if (!proxy_checkref(proxy))
400+
return NULL;
401+
return PyIter_Next(PyWeakref_GET_OBJECT(proxy));
402+
}
403+
386404

387405
static PyNumberMethods proxy_as_number = {
388406
(binaryfunc)proxy_add, /*nb_add*/
@@ -447,26 +465,30 @@ _PyWeakref_ProxyType = {
447465
sizeof(PyWeakReference),
448466
0,
449467
/* methods */
450-
(destructor)weakref_dealloc,/*tp_dealloc*/
451-
(printfunc)proxy_print, /*tp_print*/
452-
0, /*tp_getattr*/
453-
0, /*tp_setattr*/
454-
proxy_compare, /*tp_compare*/
455-
(unaryfunc)proxy_repr, /*tp_repr*/
456-
&proxy_as_number, /*tp_as_number*/
457-
&proxy_as_sequence, /*tp_as_sequence*/
458-
&proxy_as_mapping, /*tp_as_mapping*/
459-
0, /*tp_hash*/
460-
(ternaryfunc)0, /*tp_call*/
461-
(unaryfunc)proxy_str, /*tp_str*/
462-
(getattrofunc)proxy_getattr,/*tp_getattro*/
463-
(setattrofunc)proxy_setattr,/*tp_setattro*/
464-
0, /*tp_as_buffer*/
468+
(destructor)weakref_dealloc, /* tp_dealloc */
469+
(printfunc)proxy_print, /* tp_print */
470+
0, /* tp_getattr */
471+
0, /* tp_setattr */
472+
proxy_compare, /* tp_compare */
473+
(unaryfunc)proxy_repr, /* tp_repr */
474+
&proxy_as_number, /* tp_as_number */
475+
&proxy_as_sequence, /* tp_as_sequence */
476+
&proxy_as_mapping, /* tp_as_mapping */
477+
0, /* tp_hash */
478+
(ternaryfunc)0, /* tp_call */
479+
(unaryfunc)proxy_str, /* tp_str */
480+
(getattrofunc)proxy_getattr, /* tp_getattro */
481+
(setattrofunc)proxy_setattr, /* tp_setattro */
482+
0, /* tp_as_buffer */
465483
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
466-
|Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
467-
0, /*tp_doc*/
468-
(traverseproc)gc_traverse, /*tp_traverse*/
469-
(inquiry)gc_clear, /*tp_clear*/
484+
| Py_TPFLAGS_CHECKTYPES, /* tp_flags */
485+
0, /* tp_doc */
486+
(traverseproc)gc_traverse, /* tp_traverse */
487+
(inquiry)gc_clear, /* tp_clear */
488+
0, /* tp_richcompare */
489+
0, /* tp_weaklistoffset */
490+
(getiterfunc)proxy_iter, /* tp_iter */
491+
(iternextfunc)proxy_iternext, /* tp_iternext */
470492
};
471493

472494

@@ -478,26 +500,30 @@ _PyWeakref_CallableProxyType = {
478500
sizeof(PyWeakReference),
479501
0,
480502
/* methods */
481-
(destructor)weakref_dealloc,/*tp_dealloc*/
482-
(printfunc)proxy_print, /*tp_print*/
483-
0, /*tp_getattr*/
484-
0, /*tp_setattr*/
485-
proxy_compare, /*tp_compare*/
486-
(unaryfunc)proxy_repr, /*tp_repr*/
487-
&proxy_as_number, /*tp_as_number*/
488-
&proxy_as_sequence, /*tp_as_sequence*/
489-
&proxy_as_mapping, /*tp_as_mapping*/
490-
0, /*tp_hash*/
491-
(ternaryfunc)proxy_call, /*tp_call*/
492-
(unaryfunc)proxy_str, /*tp_str*/
493-
(getattrofunc)proxy_getattr,/*tp_getattro*/
494-
(setattrofunc)proxy_setattr,/*tp_setattro*/
495-
0, /*tp_as_buffer*/
503+
(destructor)weakref_dealloc, /* tp_dealloc */
504+
(printfunc)proxy_print, /* tp_print */
505+
0, /* tp_getattr */
506+
0, /* tp_setattr */
507+
proxy_compare, /* tp_compare */
508+
(unaryfunc)proxy_repr, /* tp_repr */
509+
&proxy_as_number, /* tp_as_number */
510+
&proxy_as_sequence, /* tp_as_sequence */
511+
&proxy_as_mapping, /* tp_as_mapping */
512+
0, /* tp_hash */
513+
(ternaryfunc)proxy_call, /* tp_call */
514+
(unaryfunc)proxy_str, /* tp_str */
515+
(getattrofunc)proxy_getattr, /* tp_getattro */
516+
(setattrofunc)proxy_setattr, /* tp_setattro */
517+
0, /* tp_as_buffer */
496518
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
497-
|Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
498-
0, /*tp_doc*/
499-
(traverseproc)gc_traverse, /*tp_traverse*/
500-
(inquiry)gc_clear, /*tp_clear*/
519+
| Py_TPFLAGS_CHECKTYPES, /* tp_flags */
520+
0, /* tp_doc */
521+
(traverseproc)gc_traverse, /* tp_traverse */
522+
(inquiry)gc_clear, /* tp_clear */
523+
0, /* tp_richcompare */
524+
0, /* tp_weaklistoffset */
525+
(getiterfunc)proxy_iter, /* tp_iter */
526+
(iternextfunc)proxy_iternext, /* tp_iternext */
501527
};
502528

503529

0 commit comments

Comments
 (0)