annotate Objects/weakrefobject.c @ 34161:ca2ca1aeb080 2.4

Check in missing png version of pyfav.gif.
author Johannes Gijsbers <jlg@dds.nl>
date Mon, 13 Dec 2004 13:11:42 +0000
parents facb5a2f4b53
children 51ba8ce8d92a
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
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
22 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
23 init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
24 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
25 self->hash = -1;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
26 self->wr_object = ob;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
27 Py_XINCREF(callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
28 self->wr_callback = callback;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
29 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
30
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 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
32 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
33 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 PyWeakReference *result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35
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
36 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
37 if (result) {
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
38 init_weakref(result, ob, callback);
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
39 PyObject_GC_Track(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 return result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 }
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 /* 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
46 * 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
47 * 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
48 * 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
49 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 clear_weakref(PyWeakReference *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 PyObject *callback = self->wr_callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 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
56 PyWeakReference **list = GET_WEAKREFS_LISTPTR(
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 PyWeakref_GET_OBJECT(self));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 if (*list == self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 *list = self->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 self->wr_object = Py_None;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 if (self->wr_prev != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 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
64 if (self->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 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
66 self->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 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
68 }
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
69 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
70 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
71 self->wr_callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
75 /* 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
76 * 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
77 * 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
78 * 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
79 * 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
80 * 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
81 * 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
82 * 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
83 * 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
84 * 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
85 */
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
86 void
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
87 _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
88 {
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
89 PyObject *callback;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
90
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
91 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
92 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
93 /* 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
94 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
95 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
96 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
97 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
98 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 static void
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
101 weakref_dealloc(PyObject *self)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 {
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
103 PyObject_GC_UnTrack(self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
104 clear_weakref((PyWeakReference *) self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
105 self->ob_type->tp_free(self);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 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
111 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 if (self->wr_callback != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 return visit(self->wr_callback, arg);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116
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 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 gc_clear(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 clear_weakref(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 }
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 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
128 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 static char *argnames[] = {NULL};
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131 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
132 PyObject *object = PyWeakref_GET_OBJECT(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133 Py_INCREF(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 return (object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140 static long
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141 weakref_hash(PyWeakReference *self)
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 if (self->hash != -1)
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 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
146 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
147 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
148 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 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
150 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
151 }
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
152
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
153
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
154 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
155 weakref_repr(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
156 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
157 char buffer[256];
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158 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
159 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
160 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
161 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
162 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
163 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
164 "__name__");
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
165 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
166 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
167 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
168 name = PyString_AS_STRING(nameobj);
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
169 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
170 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
171 : "<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
172 self,
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
173 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
174 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
175 name);
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
176 Py_XDECREF(nameobj);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178 return PyString_FromString(buffer);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 /* 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
182 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
183 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
184
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 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
187 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 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
189 Py_INCREF(Py_NotImplemented);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190 return Py_NotImplemented;
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 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
193 || PyWeakref_GET_OBJECT(other) == Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 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
195 Py_INCREF(res);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 return res;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 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
199 PyWeakref_GET_OBJECT(other), op);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
200 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
201
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
202 /* Given the head of an object's list of weak references, extract the
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
203 * two callback-less refs (ref and proxy). Used to determine if the
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
204 * shared references exist and to determine the back link for newly
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
205 * inserted references.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
206 */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
207 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
208 get_basic_refs(PyWeakReference *head,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
209 PyWeakReference **refp, PyWeakReference **proxyp)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
210 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
211 *refp = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
212 *proxyp = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
213
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
214 if (head != NULL && head->wr_callback == NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
215 /* We need to be careful that the "basic refs" aren't
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
216 subclasses of the main types. That complicates this a
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
217 little. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
218 if (PyWeakref_CheckRefExact(head)) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
219 *refp = head;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
220 head = head->wr_next;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
221 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
222 if (head != NULL
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
223 && head->wr_callback == NULL
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
224 && PyWeakref_CheckProxy(head)) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
225 *proxyp = head;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
226 /* head = head->wr_next; */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
227 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
228 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
229 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
230
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
231 /* Insert 'newref' in the list after 'prev'. Both must be non-NULL. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
232 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
233 insert_after(PyWeakReference *newref, PyWeakReference *prev)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
234 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
235 newref->wr_prev = prev;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
236 newref->wr_next = prev->wr_next;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
237 if (prev->wr_next != NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
238 prev->wr_next->wr_prev = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
239 prev->wr_next = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
240 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
241
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
242 /* Insert 'newref' at the head of the list; 'list' points to the variable
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
243 * that stores the head.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
244 */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
245 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
246 insert_head(PyWeakReference *newref, PyWeakReference **list)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
247 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
248 PyWeakReference *next = *list;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
249
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
250 newref->wr_prev = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
251 newref->wr_next = next;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
252 if (next != NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
253 next->wr_prev = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
254 *list = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
255 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
256
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
257 static int
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
258 parse_weakref_init_args(char *funcname, PyObject *args, PyObject *kwargs,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
259 PyObject **obp, PyObject **callbackp)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
260 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
261 /* XXX Should check that kwargs == NULL or is empty. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
262 return PyArg_UnpackTuple(args, funcname, 1, 2, obp, callbackp);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
263 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
264
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
265 static PyObject *
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
266 weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
267 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
268 PyWeakReference *self = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
269 PyObject *ob, *callback = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
270
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
271 if (parse_weakref_init_args("__new__", args, kwargs, &ob, &callback)) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
272 PyWeakReference *ref, *proxy;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
273 PyWeakReference **list;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
274
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
275 if (!PyType_SUPPORTS_WEAKREFS(ob->ob_type)) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
276 PyErr_Format(PyExc_TypeError,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
277 "cannot create weak reference to '%s' object",
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
278 ob->ob_type->tp_name);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
279 return NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
280 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
281 if (callback == Py_None)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
282 callback = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
283 list = GET_WEAKREFS_LISTPTR(ob);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
284 get_basic_refs(*list, &ref, &proxy);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
285 if (callback == NULL && type == &_PyWeakref_RefType) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
286 if (ref != NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
287 /* We can re-use an existing reference. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
288 Py_INCREF(ref);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
289 return (PyObject *)ref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
290 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
291 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
292 /* We have to create a new reference. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
293 /* Note: the tp_alloc() can trigger cyclic GC, so the weakref
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
294 list on ob can be mutated. This means that the ref and
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
295 proxy pointers we got back earlier may have been collected,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
296 so we need to compute these values again before we use
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
297 them. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
298 self = (PyWeakReference *) (type->tp_alloc(type, 0));
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
299 if (self != NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
300 init_weakref(self, ob, callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
301 if (callback == NULL && type == &_PyWeakref_RefType) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
302 insert_head(self, list);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
303 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
304 else {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
305 PyWeakReference *prev;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
306
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
307 get_basic_refs(*list, &ref, &proxy);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
308 prev = (proxy == NULL) ? ref : proxy;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
309 if (prev == NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
310 insert_head(self, list);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
311 else
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
312 insert_after(self, prev);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
313 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
314 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
315 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
316 return (PyObject *)self;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
317 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
318
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
319 static int
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
320 weakref___init__(PyObject *self, PyObject *args, PyObject *kwargs)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
321 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
322 PyObject *tmp;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
323
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
324 if (parse_weakref_init_args("__init__", args, kwargs, &tmp, &tmp))
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
325 return 0;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
326 else
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
327 return 1;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
328 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
329
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
330
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
331 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
332 _PyWeakref_RefType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
333 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
334 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
335 "weakref",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
336 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
337 0,
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
338 weakref_dealloc, /*tp_dealloc*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339 0, /*tp_print*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340 0, /*tp_getattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
341 0, /*tp_setattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
342 0, /*tp_compare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
343 (reprfunc)weakref_repr, /*tp_repr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
344 0, /*tp_as_number*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
345 0, /*tp_as_sequence*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346 0, /*tp_as_mapping*/
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
347 (hashfunc)weakref_hash, /*tp_hash*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
348 (ternaryfunc)weakref_call, /*tp_call*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349 0, /*tp_str*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
350 0, /*tp_getattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
351 0, /*tp_setattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352 0, /*tp_as_buffer*/
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
353 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_RICHCOMPARE
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
354 | Py_TPFLAGS_BASETYPE, /*tp_flags*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
355 0, /*tp_doc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
356 (traverseproc)gc_traverse, /*tp_traverse*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
357 (inquiry)gc_clear, /*tp_clear*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358 (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
359 0, /*tp_weaklistoffset*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
360 0, /*tp_iter*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
361 0, /*tp_iternext*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
362 0, /*tp_methods*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
363 0, /*tp_members*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
364 0, /*tp_getset*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
365 0, /*tp_base*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
366 0, /*tp_dict*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
367 0, /*tp_descr_get*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
368 0, /*tp_descr_set*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
369 0, /*tp_dictoffset*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
370 (initproc)weakref___init__, /*tp_init*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
371 PyType_GenericAlloc, /*tp_alloc*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
372 weakref___new__, /*tp_new*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
373 PyObject_GC_Del, /*tp_free*/
20280
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
377 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
378 proxy_checkref(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
380 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
381 PyErr_SetString(PyExc_ReferenceError,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382 "weakly-referenced object no longer exists");
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
383 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
384 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
385 return 1;
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
388
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
389 /* 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
390 * 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
391 * 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
392 */
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
393 #define UNWRAP(o) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
394 if (PyWeakref_CheckProxy(o)) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
395 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
396 return NULL; \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
397 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
398 }
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
399
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
400 #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
401 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
402 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
403 return -1; \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
404 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
405 }
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
406
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
407 #define WRAP_UNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
408 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
409 method(PyObject *proxy) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
410 UNWRAP(proxy); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
411 return generic(proxy); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
412 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
413
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
414 #define WRAP_BINARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
415 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
416 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
417 UNWRAP(x); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
418 UNWRAP(y); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
419 return generic(x, y); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
420 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
421
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
422 /* 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
423 * 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
424 */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
425 #define WRAP_TERNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
426 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
427 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
428 UNWRAP(proxy); \
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
429 UNWRAP(v); \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
430 if (w != NULL) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
431 UNWRAP(w); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
432 return generic(proxy, v, w); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
433 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
434
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 /* direct slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
437
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
438 WRAP_BINARY(proxy_getattr, PyObject_GetAttr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
439 WRAP_UNARY(proxy_str, PyObject_Str)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
440 WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
441
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
442 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
443 proxy_repr(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
444 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
445 char buf[160];
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
446 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
447 "<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
448 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
449 PyWeakref_GET_OBJECT(proxy));
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
450 return PyString_FromString(buf);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
451 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
452
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
453
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
454 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
455 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
456 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
457 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
458 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
459 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
460 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
461
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
462 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
463 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
464 {
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
465 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
466 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
467 return PyObject_Compare(proxy, v);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
468 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
469
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
470 /* number slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
471 WRAP_BINARY(proxy_add, PyNumber_Add)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
472 WRAP_BINARY(proxy_sub, PyNumber_Subtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
473 WRAP_BINARY(proxy_mul, PyNumber_Multiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
474 WRAP_BINARY(proxy_div, PyNumber_Divide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
475 WRAP_BINARY(proxy_mod, PyNumber_Remainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
476 WRAP_BINARY(proxy_divmod, PyNumber_Divmod)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
477 WRAP_TERNARY(proxy_pow, PyNumber_Power)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
478 WRAP_UNARY(proxy_neg, PyNumber_Negative)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
479 WRAP_UNARY(proxy_pos, PyNumber_Positive)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
480 WRAP_UNARY(proxy_abs, PyNumber_Absolute)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
481 WRAP_UNARY(proxy_invert, PyNumber_Invert)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
482 WRAP_BINARY(proxy_lshift, PyNumber_Lshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
483 WRAP_BINARY(proxy_rshift, PyNumber_Rshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
484 WRAP_BINARY(proxy_and, PyNumber_And)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
485 WRAP_BINARY(proxy_xor, PyNumber_Xor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
486 WRAP_BINARY(proxy_or, PyNumber_Or)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
487 WRAP_UNARY(proxy_int, PyNumber_Int)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
488 WRAP_UNARY(proxy_long, PyNumber_Long)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
489 WRAP_UNARY(proxy_float, PyNumber_Float)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
490 WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
491 WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
492 WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
493 WRAP_BINARY(proxy_idiv, PyNumber_InPlaceDivide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
494 WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
495 WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
496 WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
497 WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
498 WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
499 WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
500 WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
501
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
502 static int
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
503 proxy_nonzero(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
504 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
505 PyObject *o = PyWeakref_GET_OBJECT(proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
506 if (!proxy_checkref(proxy))
32530
2451c9572edf SF bug #978308, Spurious errors taking bool of dead pro
Neal Norwitz <nnorwitz@gmail.com>
parents: 32472
diff changeset
507 return -1;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
508 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
509 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
510 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
511 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
512 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
513 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
514
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
515 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
516 proxy_dealloc(PyWeakReference *self)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
517 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
518 if (self->wr_callback != NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
519 PyObject_GC_UnTrack((PyObject *)self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
520 clear_weakref(self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
521 PyObject_GC_Del(self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
522 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
523
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
524 /* sequence slots */
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 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
527 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
528 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
529 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
530 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
531 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
532 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
533
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
534 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
535 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
536 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
537 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
538 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
539 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
540 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
541
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
542 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
543 proxy_contains(PyWeakReference *proxy, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
544 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
545 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
546 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
547 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
548 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
549
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
550
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
551 /* mapping slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
552
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
553 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
554 proxy_length(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
555 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
556 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
557 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
558 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
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 WRAP_BINARY(proxy_getitem, PyObject_GetItem)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
562
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
563 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
564 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
565 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
566 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
567 return -1;
29697
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
568
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
569 if (value == NULL)
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
570 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
571 else
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
572 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
573 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
574
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
575 /* iterator slots */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
576
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
577 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
578 proxy_iter(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
579 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
580 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
581 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
582 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
583 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
584
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
585 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
586 proxy_iternext(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
587 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
588 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
589 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
590 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
591 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
592
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
593
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
594 static PyNumberMethods proxy_as_number = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
595 (binaryfunc)proxy_add, /*nb_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
596 (binaryfunc)proxy_sub, /*nb_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
597 (binaryfunc)proxy_mul, /*nb_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
598 (binaryfunc)proxy_div, /*nb_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
599 (binaryfunc)proxy_mod, /*nb_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
600 (binaryfunc)proxy_divmod, /*nb_divmod*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
601 (ternaryfunc)proxy_pow, /*nb_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
602 (unaryfunc)proxy_neg, /*nb_negative*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
603 (unaryfunc)proxy_pos, /*nb_positive*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
604 (unaryfunc)proxy_abs, /*nb_absolute*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
605 (inquiry)proxy_nonzero, /*nb_nonzero*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
606 (unaryfunc)proxy_invert, /*nb_invert*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
607 (binaryfunc)proxy_lshift, /*nb_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
608 (binaryfunc)proxy_rshift, /*nb_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
609 (binaryfunc)proxy_and, /*nb_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
610 (binaryfunc)proxy_xor, /*nb_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
611 (binaryfunc)proxy_or, /*nb_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
612 (coercion)0, /*nb_coerce*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
613 (unaryfunc)proxy_int, /*nb_int*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
614 (unaryfunc)proxy_long, /*nb_long*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
615 (unaryfunc)proxy_float, /*nb_float*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
616 (unaryfunc)0, /*nb_oct*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
617 (unaryfunc)0, /*nb_hex*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
618 (binaryfunc)proxy_iadd, /*nb_inplace_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
619 (binaryfunc)proxy_isub, /*nb_inplace_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
620 (binaryfunc)proxy_imul, /*nb_inplace_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
621 (binaryfunc)proxy_idiv, /*nb_inplace_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
622 (binaryfunc)proxy_imod, /*nb_inplace_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
623 (ternaryfunc)proxy_ipow, /*nb_inplace_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
624 (binaryfunc)proxy_ilshift, /*nb_inplace_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
625 (binaryfunc)proxy_irshift, /*nb_inplace_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
626 (binaryfunc)proxy_iand, /*nb_inplace_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
627 (binaryfunc)proxy_ixor, /*nb_inplace_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
628 (binaryfunc)proxy_ior, /*nb_inplace_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
629 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
630
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
631 static PySequenceMethods proxy_as_sequence = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
632 (inquiry)proxy_length, /*sq_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
633 0, /*sq_concat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
634 0, /*sq_repeat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
635 0, /*sq_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
636 (intintargfunc)proxy_slice, /*sq_slice*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
637 0, /*sq_ass_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
638 (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
639 (objobjproc)proxy_contains, /* sq_contains */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
640 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
641
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
642 static PyMappingMethods proxy_as_mapping = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
643 (inquiry)proxy_length, /*mp_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
644 (binaryfunc)proxy_getitem, /*mp_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
645 (objobjargproc)proxy_setitem, /*mp_ass_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
646 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
647
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
648
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
649 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
650 _PyWeakref_ProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
651 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
652 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
653 "weakproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
654 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
655 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
656 /* methods */
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
657 (destructor)proxy_dealloc, /* tp_dealloc */
29863
98c59bbdd8ec Remove proxy_print(), since that caused an inconsistency between
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
658 0, /* tp_print */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
659 0, /* tp_getattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
660 0, /* tp_setattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
661 proxy_compare, /* tp_compare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
662 (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
663 &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
664 &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
665 &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
666 0, /* tp_hash */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
667 (ternaryfunc)0, /* tp_call */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
668 (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
669 (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
670 (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
671 0, /* tp_as_buffer */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
672 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
673 | 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
674 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
675 (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
676 (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
677 0, /* tp_richcompare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
678 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
679 (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
680 (iternextfunc)proxy_iternext, /* tp_iternext */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
681 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
682
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 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
685 _PyWeakref_CallableProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
686 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
687 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
688 "weakcallableproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
689 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
690 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
691 /* methods */
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
692 (destructor)proxy_dealloc, /* tp_dealloc */
29863
98c59bbdd8ec Remove proxy_print(), since that caused an inconsistency between
Fred Drake <fdrake@acm.org>
parents: 29697
diff changeset
693 0, /* tp_print */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
694 0, /* tp_getattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
695 0, /* tp_setattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
696 proxy_compare, /* tp_compare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
697 (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
698 &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
699 &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
700 &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
701 0, /* tp_hash */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
702 (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
703 (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
704 (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
705 (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
706 0, /* tp_as_buffer */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
707 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
708 | 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
709 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
710 (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
711 (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
712 0, /* tp_richcompare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
713 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
714 (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
715 (iternextfunc)proxy_iternext, /* tp_iternext */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
716 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
717
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
718
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
719
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
720 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
721 PyWeakref_NewRef(PyObject *ob, PyObject *callback)
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 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
724 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
725 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
726
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
727 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
728 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
729 "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
730 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
731 return NULL;
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 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
734 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
735 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
736 callback = NULL;
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
737 if (callback == NULL)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
738 /* 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
739 result = ref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
740 if (result != NULL)
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
741 Py_INCREF(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
742 else {
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
743 /* Note: new_weakref() can trigger cyclic GC, so the weakref
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
744 list on ob can be mutated. This means that the ref and
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
745 proxy pointers we got back earlier may have been collected,
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
746 so we need to compute these values again before we use
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
747 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
748 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
749 if (result != NULL) {
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
750 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
751 if (callback == NULL) {
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
752 if (ref == NULL)
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
753 insert_head(result, list);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
754 else {
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
755 /* Someone else added a ref without a callback
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
756 during GC. Return that one instead of this one
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
757 to avoid violating the invariants of the list
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
758 of weakrefs for ob. */
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
759 Py_DECREF(result);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
760 Py_INCREF(ref);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
761 result = ref;
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
762 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
763 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
764 else {
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
765 PyWeakReference *prev;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
766
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
767 prev = (proxy == NULL) ? ref : proxy;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
768 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
769 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
770 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
771 insert_after(result, prev);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
772 }
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 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
775 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
776 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
777
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
778
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
779 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
780 PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
781 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
782 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
783 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
784 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
785
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
786 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
787 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
788 "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
789 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
790 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
791 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
792 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
793 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
794 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
795 callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
796 if (callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
797 /* 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
798 result = proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
799 if (result != NULL)
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
800 Py_INCREF(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
801 else {
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
802 /* Note: new_weakref() can trigger cyclic GC, so the weakref
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
803 list on ob can be mutated. This means that the ref and
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
804 proxy pointers we got back earlier may have been collected,
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
805 so we need to compute these values again before we use
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
806 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
807 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
808 if (result != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
809 PyWeakReference *prev;
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 if (PyCallable_Check(ob))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
812 result->ob_type = &_PyWeakref_CallableProxyType;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
813 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
814 result->ob_type = &_PyWeakref_ProxyType;
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
815 get_basic_refs(*list, &ref, &proxy);
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
816 if (callback == NULL) {
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
817 if (proxy != NULL) {
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
818 /* Someone else added a proxy without a callback
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
819 during GC. Return that one instead of this one
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
820 to avoid violating the invariants of the list
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
821 of weakrefs for ob. */
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
822 Py_DECREF(result);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
823 Py_INCREF(result = proxy);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
824 goto skip_insert;
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
825 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
826 prev = ref;
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
827 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
828 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
829 prev = (proxy == NULL) ? ref : proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
830
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
831 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
832 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
833 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
834 insert_after(result, prev);
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
835 skip_insert:
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
836 ;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
837 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
838 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
839 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
840 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
841
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
842
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
843 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
844 PyWeakref_GetObject(PyObject *ref)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
845 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
846 if (ref == NULL || !PyWeakref_Check(ref)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
847 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
848 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
849 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
850 return PyWeakref_GET_OBJECT(ref);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
851 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
852
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 32932
diff changeset
853 /* Note that there's an inlined copy-paste of handle_callback() in gcmodule.c's
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 32932
diff changeset
854 * handle_weakrefs().
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 32932
diff changeset
855 */
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
856 static void
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
857 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
858 {
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
859 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
860
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
861 if (cbresult == NULL)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
862 PyErr_WriteUnraisable(callback);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
863 else
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
864 Py_DECREF(cbresult);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
865 }
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
866
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
867 /* 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
868 *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
869 * 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
870 * 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
871 * been attempted.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
872 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
873 void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
874 PyObject_ClearWeakRefs(PyObject *object)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
875 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
876 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
877
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
878 if (object == NULL
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
879 || !PyType_SUPPORTS_WEAKREFS(object->ob_type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
880 || object->ob_refcnt != 0) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
881 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
882 return;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
883 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
884 list = GET_WEAKREFS_LISTPTR(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
885 /* 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
886 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
887 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
888 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
889 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
890 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
891 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
892 PyWeakReference *current = *list;
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
893 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
894 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
895 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
896
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
897 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
898 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
899 if (count == 1) {
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
900 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
901
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
902 current->wr_callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
903 clear_weakref(current);
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
904 if (callback != NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
905 handle_callback(current, callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
906 Py_DECREF(callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
907 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
908 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
909 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
910 PyObject *tuple = PyTuple_New(count * 2);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
911 int i = 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
912
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
913 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
914 PyWeakReference *next = current->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
915
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
916 Py_INCREF(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
917 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
918 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
919 current->wr_callback = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
920 clear_weakref(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
921 current = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
922 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
923 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
924 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
925
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
926 if (callback != NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
927 PyObject *current = PyTuple_GET_ITEM(tuple, i * 2);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
928 handle_callback((PyWeakReference *)current, callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
929 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
930 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
931 Py_DECREF(tuple);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
932 }
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
933 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
934 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
935 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
936 }