annotate Objects/weakrefobject.c @ 32931:ec71ae3bc2dd 2.3

Be more careful about maintaining the invariants; it was actually possible that the callback-less flavors of the ref or proxy could have been added during GC, so we don't want to replace them.
author Fred Drake <fdrake@acm.org>
date Tue, 03 Aug 2004 14:46:57 +0000
parents 3cc3cb79043f
children b585bccfb250
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 #include "Python.h"
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 #include "structmember.h"
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 #define GET_WEAKREFS_LISTPTR(o) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 long
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 _PyWeakref_GetWeakrefCount(PyWeakReference *head)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 long count = 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 while (head != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 ++count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 return count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 static PyWeakReference *
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
23 new_weakref(PyObject *ob, PyObject *callback)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 PyWeakReference *result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
27 result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
28 if (result) {
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
29 result->hash = -1;
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
30 result->wr_object = ob;
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
31 Py_XINCREF(callback);
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
32 result->wr_callback = callback;
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
33 PyObject_GC_Track(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 return result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 /* This function clears the passed-in reference and removes it from the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 * list of weak references for the referent. This is the only code that
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 * removes an item from the doubly-linked list of weak references for an
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 * object; it is also responsible for clearing the callback slot.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 clear_weakref(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 PyObject *callback = self->wr_callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 if (PyWeakref_GET_OBJECT(self) != Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 PyWeakReference **list = GET_WEAKREFS_LISTPTR(
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 PyWeakref_GET_OBJECT(self));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 if (*list == self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 *list = self->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 self->wr_object = Py_None;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 if (self->wr_prev != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 self->wr_prev->wr_next = self->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 if (self->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 self->wr_next->wr_prev = self->wr_prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 self->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 self->wr_next = NULL;
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
62 }
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
63 if (callback != NULL) {
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
64 Py_DECREF(callback);
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
65 self->wr_callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
69 /* Cyclic gc uses this to *just* clear the passed-in reference, leaving
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
70 * the callback intact and uncalled. It must be possible to call self's
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
71 * tp_dealloc() after calling this, so self has to be left in a sane enough
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
72 * state for that to work. We expect tp_dealloc to decref the callback
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
73 * then. The reason for not letting clear_weakref() decref the callback
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
74 * right now is that if the callback goes away, that may in turn trigger
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
75 * another callback (if a weak reference to the callback exists) -- running
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
76 * arbitrary Python code in the middle of gc is a disaster. The convolution
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
77 * here allows gc to delay triggering such callbacks until the world is in
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
78 * a sane state again.
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
79 */
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
80 void
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
81 _PyWeakref_ClearRef(PyWeakReference *self)
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
82 {
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
83 PyObject *callback;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
84
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
85 assert(self != NULL);
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
86 assert(PyWeakref_Check(self));
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
87 /* Preserve and restore the callback around clear_weakref. */
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
88 callback = self->wr_callback;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
89 self->wr_callback = NULL;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
90 clear_weakref(self);
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
91 self->wr_callback = callback;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
92 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 weakref_dealloc(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 PyObject_GC_UnTrack((PyObject *)self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 clear_weakref(self);
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
99 PyObject_GC_Del(self);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 gc_traverse(PyWeakReference *self, visitproc visit, void *arg)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 if (self->wr_callback != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107 return visit(self->wr_callback, arg);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 gc_clear(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 clear_weakref(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 static char *argnames[] = {NULL};
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125 if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", argnames)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 PyObject *object = PyWeakref_GET_OBJECT(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 Py_INCREF(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 return (object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 static long
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 weakref_hash(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 if (self->hash != -1)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139 if (PyWeakref_GET_OBJECT(self) == Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140 PyErr_SetString(PyExc_TypeError, "weak object has gone away");
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
143 self->hash = PyObject_Hash(PyWeakref_GET_OBJECT(self));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
144 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145 }
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
146
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
148 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 weakref_repr(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
150 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
151 char buffer[256];
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152 if (PyWeakref_GET_OBJECT(self) == Py_None) {
28644
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
153 PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
154 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
155 else {
28644
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
156 char *name = NULL;
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
157 PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self),
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
158 "__name__");
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
159 if (nameobj == NULL)
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
160 PyErr_Clear();
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
161 else if (PyString_Check(nameobj))
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
162 name = PyString_AS_STRING(nameobj);
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
163 PyOS_snprintf(buffer, sizeof(buffer),
28644
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
164 name ? "<weakref at %p; to '%.50s' at %p (%s)>"
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
165 : "<weakref at %p; to '%.50s' at %p>",
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
166 self,
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
167 PyWeakref_GET_OBJECT(self)->ob_type->tp_name,
28644
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
168 PyWeakref_GET_OBJECT(self),
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
169 name);
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
170 Py_XDECREF(nameobj);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 return PyString_FromString(buffer);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
173 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
174
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
175 /* Weak references only support equality, not ordering. Two weak references
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176 are equal if the underlying objects are equal. If the underlying object has
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 gone away, they are equal if they are identical. */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180 weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182 if (op != Py_EQ || self->ob_type != other->ob_type) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 Py_INCREF(Py_NotImplemented);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 return Py_NotImplemented;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 if (PyWeakref_GET_OBJECT(self) == Py_None
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 || PyWeakref_GET_OBJECT(other) == Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 PyObject *res = self==other ? Py_True : Py_False;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
189 Py_INCREF(res);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190 return res;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
191 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
192 return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
193 PyWeakref_GET_OBJECT(other), op);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
195
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 _PyWeakref_RefType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
199 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
200 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
201 "weakref",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
202 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204 (destructor)weakref_dealloc,/*tp_dealloc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205 0, /*tp_print*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
206 0, /*tp_getattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
207 0, /*tp_setattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
208 0, /*tp_compare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
209 (reprfunc)weakref_repr, /*tp_repr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
210 0, /*tp_as_number*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211 0, /*tp_as_sequence*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
212 0, /*tp_as_mapping*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
213 (hashfunc)weakref_hash, /*tp_hash*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
214 (ternaryfunc)weakref_call, /*tp_call*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
215 0, /*tp_str*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
216 0, /*tp_getattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
217 0, /*tp_setattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
218 0, /*tp_as_buffer*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
219 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_RICHCOMPARE,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 0, /*tp_doc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221 (traverseproc)gc_traverse, /*tp_traverse*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
222 (inquiry)gc_clear, /*tp_clear*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
223 (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
224 0, /*tp_weaklistoffset*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
225 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
226
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
227
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
228 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
229 proxy_checkref(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
230 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
231 if (PyWeakref_GET_OBJECT(proxy) == Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
232 PyErr_SetString(PyExc_ReferenceError,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
233 "weakly-referenced object no longer exists");
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
234 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
235 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
236 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
238
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
239
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
240 /* If a parameter is a proxy, check that it is still "live" and wrap it,
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
241 * replacing the original value with the raw object. Raises ReferenceError
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
242 * if the param is a dead proxy.
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
243 */
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
244 #define UNWRAP(o) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
245 if (PyWeakref_CheckProxy(o)) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
246 if (!proxy_checkref((PyWeakReference *)o)) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
247 return NULL; \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
248 o = PyWeakref_GET_OBJECT(o); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
249 }
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
250
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
251 #define UNWRAP_I(o) \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
252 if (PyWeakref_CheckProxy(o)) { \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
253 if (!proxy_checkref((PyWeakReference *)o)) \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
254 return -1; \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
255 o = PyWeakref_GET_OBJECT(o); \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
256 }
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
257
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
258 #define WRAP_UNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
259 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
260 method(PyObject *proxy) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
261 UNWRAP(proxy); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
262 return generic(proxy); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
265 #define WRAP_BINARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
267 method(PyObject *x, PyObject *y) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
268 UNWRAP(x); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
269 UNWRAP(y); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
270 return generic(x, y); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
271 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
272
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
273 /* Note that the third arg needs to be checked for NULL since the tp_call
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
274 * slot can receive NULL for this arg.
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
275 */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
276 #define WRAP_TERNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
277 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
278 method(PyObject *proxy, PyObject *v, PyObject *w) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
279 UNWRAP(proxy); \
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
280 UNWRAP(v); \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
281 if (w != NULL) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
282 UNWRAP(w); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
283 return generic(proxy, v, w); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
284 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
285
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
286
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
287 /* direct slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
288
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
289 WRAP_BINARY(proxy_getattr, PyObject_GetAttr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
290 WRAP_UNARY(proxy_str, PyObject_Str)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
291 WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
292
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
293 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
294 proxy_repr(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
295 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
296 char buf[160];
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
297 PyOS_snprintf(buf, sizeof(buf),
28644
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
298 "<weakproxy at %p to %.100s at %p>", proxy,
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
299 PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name,
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
300 PyWeakref_GET_OBJECT(proxy));
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
301 return PyString_FromString(buf);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
302 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
303
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
304
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
305 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
306 proxy_setattr(PyWeakReference *proxy, PyObject *name, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
307 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
308 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
309 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
310 return PyObject_SetAttr(PyWeakref_GET_OBJECT(proxy), name, value);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
311 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
312
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
313 static int
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
314 proxy_compare(PyObject *proxy, PyObject *v)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
315 {
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
316 UNWRAP_I(proxy);
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
317 UNWRAP_I(v);
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
318 return PyObject_Compare(proxy, v);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
319 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
320
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
321 /* number slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
322 WRAP_BINARY(proxy_add, PyNumber_Add)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
323 WRAP_BINARY(proxy_sub, PyNumber_Subtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
324 WRAP_BINARY(proxy_mul, PyNumber_Multiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
325 WRAP_BINARY(proxy_div, PyNumber_Divide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
326 WRAP_BINARY(proxy_mod, PyNumber_Remainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
327 WRAP_BINARY(proxy_divmod, PyNumber_Divmod)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
328 WRAP_TERNARY(proxy_pow, PyNumber_Power)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
329 WRAP_UNARY(proxy_neg, PyNumber_Negative)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
330 WRAP_UNARY(proxy_pos, PyNumber_Positive)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
331 WRAP_UNARY(proxy_abs, PyNumber_Absolute)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
332 WRAP_UNARY(proxy_invert, PyNumber_Invert)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
333 WRAP_BINARY(proxy_lshift, PyNumber_Lshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
334 WRAP_BINARY(proxy_rshift, PyNumber_Rshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
335 WRAP_BINARY(proxy_and, PyNumber_And)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
336 WRAP_BINARY(proxy_xor, PyNumber_Xor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
337 WRAP_BINARY(proxy_or, PyNumber_Or)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
338 WRAP_UNARY(proxy_int, PyNumber_Int)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339 WRAP_UNARY(proxy_long, PyNumber_Long)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340 WRAP_UNARY(proxy_float, PyNumber_Float)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
341 WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
342 WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
343 WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
344 WRAP_BINARY(proxy_idiv, PyNumber_InPlaceDivide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
345 WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346 WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
347 WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
348 WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349 WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
350 WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
351 WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
353 static int
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
354 proxy_nonzero(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
355 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
356 PyObject *o = PyWeakref_GET_OBJECT(proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
357 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
359 if (o->ob_type->tp_as_number &&
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
360 o->ob_type->tp_as_number->nb_nonzero)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
361 return (*o->ob_type->tp_as_number->nb_nonzero)(o);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
362 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
363 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
364 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
365
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
366 /* sequence slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
367
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
368 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
369 proxy_slice(PyWeakReference *proxy, int i, int j)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
370 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
371 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
372 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
373 return PySequence_GetSlice(PyWeakref_GET_OBJECT(proxy), i, j);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
374 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
375
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
376 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
377 proxy_ass_slice(PyWeakReference *proxy, int i, int j, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
378 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
380 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
381 return PySequence_SetSlice(PyWeakref_GET_OBJECT(proxy), i, j, value);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
383
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
384 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
385 proxy_contains(PyWeakReference *proxy, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
386 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
387 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
388 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
389 return PySequence_Contains(PyWeakref_GET_OBJECT(proxy), value);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
390 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
391
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
392
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
393 /* mapping slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
394
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
395 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
396 proxy_length(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
397 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
398 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
399 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
400 return PyObject_Length(PyWeakref_GET_OBJECT(proxy));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
401 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
402
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
403 WRAP_BINARY(proxy_getitem, PyObject_GetItem)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
404
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
405 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
406 proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
407 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
408 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
409 return -1;
29697
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
410
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
411 if (value == NULL)
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
412 return PyObject_DelItem(PyWeakref_GET_OBJECT(proxy), key);
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
413 else
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
414 return PyObject_SetItem(PyWeakref_GET_OBJECT(proxy), key, value);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
415 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
416
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
417 /* iterator slots */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
418
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
419 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
420 proxy_iter(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
421 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
422 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
423 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
424 return PyObject_GetIter(PyWeakref_GET_OBJECT(proxy));
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
425 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
426
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
427 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
428 proxy_iternext(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
429 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
430 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
431 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
432 return PyIter_Next(PyWeakref_GET_OBJECT(proxy));
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
433 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
434
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
435
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
436 static PyNumberMethods proxy_as_number = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
437 (binaryfunc)proxy_add, /*nb_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
438 (binaryfunc)proxy_sub, /*nb_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
439 (binaryfunc)proxy_mul, /*nb_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
440 (binaryfunc)proxy_div, /*nb_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
441 (binaryfunc)proxy_mod, /*nb_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
442 (binaryfunc)proxy_divmod, /*nb_divmod*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
443 (ternaryfunc)proxy_pow, /*nb_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
444 (unaryfunc)proxy_neg, /*nb_negative*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
445 (unaryfunc)proxy_pos, /*nb_positive*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
446 (unaryfunc)proxy_abs, /*nb_absolute*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
447 (inquiry)proxy_nonzero, /*nb_nonzero*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
448 (unaryfunc)proxy_invert, /*nb_invert*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
449 (binaryfunc)proxy_lshift, /*nb_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
450 (binaryfunc)proxy_rshift, /*nb_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
451 (binaryfunc)proxy_and, /*nb_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
452 (binaryfunc)proxy_xor, /*nb_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
453 (binaryfunc)proxy_or, /*nb_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
454 (coercion)0, /*nb_coerce*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
455 (unaryfunc)proxy_int, /*nb_int*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
456 (unaryfunc)proxy_long, /*nb_long*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
457 (unaryfunc)proxy_float, /*nb_float*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
458 (unaryfunc)0, /*nb_oct*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
459 (unaryfunc)0, /*nb_hex*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
460 (binaryfunc)proxy_iadd, /*nb_inplace_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
461 (binaryfunc)proxy_isub, /*nb_inplace_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
462 (binaryfunc)proxy_imul, /*nb_inplace_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
463 (binaryfunc)proxy_idiv, /*nb_inplace_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
464 (binaryfunc)proxy_imod, /*nb_inplace_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
465 (ternaryfunc)proxy_ipow, /*nb_inplace_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
466 (binaryfunc)proxy_ilshift, /*nb_inplace_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
467 (binaryfunc)proxy_irshift, /*nb_inplace_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
468 (binaryfunc)proxy_iand, /*nb_inplace_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
469 (binaryfunc)proxy_ixor, /*nb_inplace_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
470 (binaryfunc)proxy_ior, /*nb_inplace_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
471 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
472
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
473 static PySequenceMethods proxy_as_sequence = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
474 (inquiry)proxy_length, /*sq_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
475 0, /*sq_concat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
476 0, /*sq_repeat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
477 0, /*sq_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
478 (intintargfunc)proxy_slice, /*sq_slice*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
479 0, /*sq_ass_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
480 (intintobjargproc)proxy_ass_slice, /*sq_ass_slice*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
481 (objobjproc)proxy_contains, /* sq_contains */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
482 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
483
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
484 static PyMappingMethods proxy_as_mapping = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
485 (inquiry)proxy_length, /*mp_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
486 (binaryfunc)proxy_getitem, /*mp_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
487 (objobjargproc)proxy_setitem, /*mp_ass_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
488 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
489
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
490
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
491 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
492 _PyWeakref_ProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
493 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
494 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
495 "weakproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
496 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
497 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
498 /* methods */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
499 (destructor)weakref_dealloc, /* tp_dealloc */
29863
98c59bbdd8ec Remove proxy_print(), since that caused an inconsistency between
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
500 0, /* tp_print */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
501 0, /* tp_getattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
502 0, /* tp_setattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
503 proxy_compare, /* tp_compare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
504 (unaryfunc)proxy_repr, /* tp_repr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
505 &proxy_as_number, /* tp_as_number */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
506 &proxy_as_sequence, /* tp_as_sequence */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
507 &proxy_as_mapping, /* tp_as_mapping */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
508 0, /* tp_hash */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
509 (ternaryfunc)0, /* tp_call */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
510 (unaryfunc)proxy_str, /* tp_str */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
511 (getattrofunc)proxy_getattr, /* tp_getattro */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
512 (setattrofunc)proxy_setattr, /* tp_setattro */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
513 0, /* tp_as_buffer */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
514 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
515 | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
516 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
517 (traverseproc)gc_traverse, /* tp_traverse */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
518 (inquiry)gc_clear, /* tp_clear */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
519 0, /* tp_richcompare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
520 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
521 (getiterfunc)proxy_iter, /* tp_iter */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
522 (iternextfunc)proxy_iternext, /* tp_iternext */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
523 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
524
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
525
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
526 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
527 _PyWeakref_CallableProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
528 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
529 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
530 "weakcallableproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
531 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
532 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
533 /* methods */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
534 (destructor)weakref_dealloc, /* tp_dealloc */
29863
98c59bbdd8ec Remove proxy_print(), since that caused an inconsistency between
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
535 0, /* tp_print */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
536 0, /* tp_getattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
537 0, /* tp_setattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
538 proxy_compare, /* tp_compare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
539 (unaryfunc)proxy_repr, /* tp_repr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
540 &proxy_as_number, /* tp_as_number */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
541 &proxy_as_sequence, /* tp_as_sequence */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
542 &proxy_as_mapping, /* tp_as_mapping */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
543 0, /* tp_hash */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
544 (ternaryfunc)proxy_call, /* tp_call */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
545 (unaryfunc)proxy_str, /* tp_str */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
546 (getattrofunc)proxy_getattr, /* tp_getattro */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
547 (setattrofunc)proxy_setattr, /* tp_setattro */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
548 0, /* tp_as_buffer */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
549 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
550 | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
551 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
552 (traverseproc)gc_traverse, /* tp_traverse */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
553 (inquiry)gc_clear, /* tp_clear */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
554 0, /* tp_richcompare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
555 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
556 (getiterfunc)proxy_iter, /* tp_iter */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
557 (iternextfunc)proxy_iternext, /* tp_iternext */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
558 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
559
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
560
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
561 /* Given the head of an object's list of weak references, extract the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
562 * two callback-less refs (ref and proxy). Used to determine if the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
563 * shared references exist and to determine the back link for newly
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
564 * inserted references.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
565 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
566 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
567 get_basic_refs(PyWeakReference *head,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
568 PyWeakReference **refp, PyWeakReference **proxyp)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
569 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
570 *refp = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
571 *proxyp = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
572
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
573 if (head != NULL && head->wr_callback == NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
574 if (head->ob_type == &_PyWeakref_RefType) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
575 *refp = head;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
576 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
577 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
578 if (head != NULL && head->wr_callback == NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
579 *proxyp = head;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
580 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
581 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
582 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
583 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
584
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
585 /* Insert 'newref' in the list after 'prev'. Both must be non-NULL. */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
586 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
587 insert_after(PyWeakReference *newref, PyWeakReference *prev)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
588 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
589 newref->wr_prev = prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
590 newref->wr_next = prev->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
591 if (prev->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
592 prev->wr_next->wr_prev = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
593 prev->wr_next = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
594 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
595
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
596 /* Insert 'newref' at the head of the list; 'list' points to the variable
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
597 * that stores the head.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
598 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
599 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
600 insert_head(PyWeakReference *newref, PyWeakReference **list)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
601 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
602 PyWeakReference *next = *list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
603
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
604 newref->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
605 newref->wr_next = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
606 if (next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
607 next->wr_prev = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
608 *list = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
609 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
610
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
611
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
612 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
613 PyWeakref_NewRef(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
614 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
615 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
616 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
617 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
618
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
619 if (!PyType_SUPPORTS_WEAKREFS(ob->ob_type)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
620 PyErr_Format(PyExc_TypeError,
20613
bf4b37518c14 Referencable is not a word, so don't use it in an error message <wink>.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20532
diff changeset
621 "cannot create weak reference to '%s' object",
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
622 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
623 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
624 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
625 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
626 get_basic_refs(*list, &ref, &proxy);
31449
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
627 if (callback == Py_None)
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
628 callback = NULL;
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
629 if (callback == NULL)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
630 /* return existing weak reference if it exists */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
631 result = ref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
632 if (result != NULL)
31453
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
633 Py_INCREF(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
634 else {
31453
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
635 /* Note: new_weakref() can trigger cyclic GC, so the weakref
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
636 list on ob can be mutated. This means that the ref and
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
637 proxy pointers we got back earlier may have been collected,
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
638 so we need to compute these values again before we use
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
639 them. */
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
640 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
641 if (result != NULL) {
32931
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
642 get_basic_refs(*list, &ref, &proxy);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
643 if (callback == NULL) {
32931
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
644 if (ref == NULL)
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
645 insert_head(result, list);
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
646 else {
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
647 /* Someone else added a ref without a callback
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
648 during GC. Return that one instead of this one
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
649 to avoid violating the invariants of the list
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
650 of weakrefs for ob. */
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
651 Py_DECREF(result);
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
652 Py_INCREF(ref);
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
653 result = ref;
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
654 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
655 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
656 else {
31453
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
657 PyWeakReference *prev;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
658
31453
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
659 prev = (proxy == NULL) ? ref : proxy;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
660 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
661 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
662 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
663 insert_after(result, prev);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
664 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
665 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
666 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
667 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
668 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
669
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
670
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
671 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
672 PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
673 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
674 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
675 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
676 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
677
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
678 if (!PyType_SUPPORTS_WEAKREFS(ob->ob_type)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
679 PyErr_Format(PyExc_TypeError,
20613
bf4b37518c14 Referencable is not a word, so don't use it in an error message <wink>.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20532
diff changeset
680 "cannot create weak reference to '%s' object",
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
681 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
682 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
683 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
684 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
685 get_basic_refs(*list, &ref, &proxy);
31449
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
686 if (callback == Py_None)
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
687 callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
688 if (callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
689 /* attempt to return an existing weak reference if it exists */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
690 result = proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
691 if (result != NULL)
31453
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
692 Py_INCREF(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
693 else {
31453
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
694 /* Note: new_weakref() can trigger cyclic GC, so the weakref
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
695 list on ob can be mutated. This means that the ref and
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
696 proxy pointers we got back earlier may have been collected,
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
697 so we need to compute these values again before we use
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
698 them. */
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
699 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
700 if (result != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
701 PyWeakReference *prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
702
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
703 if (PyCallable_Check(ob))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
704 result->ob_type = &_PyWeakref_CallableProxyType;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
705 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
706 result->ob_type = &_PyWeakref_ProxyType;
31453
3cc3cb79043f Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
707 get_basic_refs(*list, &ref, &proxy);
32931
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
708 if (callback == NULL) {
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
709 if (proxy != NULL) {
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
710 /* Someone else added a proxy without a callback
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
711 during GC. Return that one instead of this one
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
712 to avoid violating the invariants of the list
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
713 of weakrefs for ob. */
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
714 Py_DECREF(result);
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
715 Py_INCREF(result = proxy);
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
716 goto skip_insert;
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
717 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
718 prev = ref;
32931
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
719 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
720 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
721 prev = (proxy == NULL) ? ref : proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
722
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
723 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
724 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
725 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
726 insert_after(result, prev);
32931
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
727 skip_insert:
ec71ae3bc2dd Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 31453
diff changeset
728 ;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
729 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
730 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
731 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
732 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
733
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
734
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
735 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
736 PyWeakref_GetObject(PyObject *ref)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
737 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
738 if (ref == NULL || !PyWeakref_Check(ref)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
739 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
740 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
741 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
742 return PyWeakref_GET_OBJECT(ref);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
743 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
744
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
745
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
746 static void
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
747 handle_callback(PyWeakReference *ref, PyObject *callback)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
748 {
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
749 PyObject *cbresult = PyObject_CallFunction(callback, "O", ref);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
750
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
751 if (cbresult == NULL)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
752 PyErr_WriteUnraisable(callback);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
753 else
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
754 Py_DECREF(cbresult);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
755 }
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
756
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
757 /* This function is called by the tp_dealloc handler to clear weak references.
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
758 *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
759 * This iterates through the weak references for 'object' and calls callbacks
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
760 * for those references which have one. It returns when all callbacks have
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
761 * been attempted.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
762 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
763 void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
764 PyObject_ClearWeakRefs(PyObject *object)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
765 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
766 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
767
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
768 if (object == NULL
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
769 || !PyType_SUPPORTS_WEAKREFS(object->ob_type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
770 || object->ob_refcnt != 0) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
771 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
772 return;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
773 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
774 list = GET_WEAKREFS_LISTPTR(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
775 /* Remove the callback-less basic and proxy references */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
776 if (*list != NULL && (*list)->wr_callback == NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
777 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
778 if (*list != NULL && (*list)->wr_callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
779 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
780 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
781 if (*list != NULL) {
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
782 PyWeakReference *current = *list;
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
783 int count = _PyWeakref_GetWeakrefCount(current);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
784 int restore_error = PyErr_Occurred() ? 1 : 0;
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
785 PyObject *err_type, *err_value, *err_tb;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
786
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
787 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
788 PyErr_Fetch(&err_type, &err_value, &err_tb);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
789 if (count == 1) {
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
790 PyObject *callback = current->wr_callback;
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
791
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
792 current->wr_callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
793 clear_weakref(current);
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
794 handle_callback(current, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
795 Py_DECREF(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
796 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
797 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
798 PyObject *tuple = PyTuple_New(count * 2);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
799 int i = 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
800
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
801 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
802 PyWeakReference *next = current->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
803
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
804 Py_INCREF(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
805 PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
806 PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
807 current->wr_callback = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
808 clear_weakref(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
809 current = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
810 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
811 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
812 PyObject *current = PyTuple_GET_ITEM(tuple, i * 2);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
813 PyObject *callback = PyTuple_GET_ITEM(tuple, i * 2 + 1);
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
814
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
815 handle_callback((PyWeakReference *)current, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
816 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
817 Py_DECREF(tuple);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
818 }
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
819 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
820 PyErr_Restore(err_type, err_value, err_tb);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
821 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
822 }