annotate Objects/weakrefobject.c @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents 1d3d980a3186
children 8e541b078edb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 #include "Python.h"
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 #include "structmember.h"
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 #define GET_WEAKREFS_LISTPTR(o) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 long
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 _PyWeakref_GetWeakrefCount(PyWeakReference *head)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 long count = 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 while (head != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 ++count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 return count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 static PyWeakReference *
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
23 new_weakref(PyObject *ob, PyObject *callback)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 PyWeakReference *result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
27 result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
28 if (result) {
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
29 result->hash = -1;
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
30 result->wr_object = ob;
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
31 Py_XINCREF(callback);
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
32 result->wr_callback = callback;
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
33 PyObject_GC_Track(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 return result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 /* This function clears the passed-in reference and removes it from the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 * list of weak references for the referent. This is the only code that
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 * removes an item from the doubly-linked list of weak references for an
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 * object; it is also responsible for clearing the callback slot.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 clear_weakref(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 PyObject *callback = self->wr_callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 if (PyWeakref_GET_OBJECT(self) != Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 PyWeakReference **list = GET_WEAKREFS_LISTPTR(
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 PyWeakref_GET_OBJECT(self));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 if (*list == self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 *list = self->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 self->wr_object = Py_None;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 self->wr_callback = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 if (self->wr_prev != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 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
59 if (self->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 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
61 self->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 self->wr_next = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 Py_XDECREF(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 weakref_dealloc(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71 PyObject_GC_UnTrack((PyObject *)self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 clear_weakref(self);
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
73 PyObject_GC_Del(self);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 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
79 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80 if (self->wr_callback != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 return visit(self->wr_callback, arg);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 gc_clear(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 clear_weakref(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 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
96 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 static char *argnames[] = {NULL};
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99 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
100 PyObject *object = PyWeakref_GET_OBJECT(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101 Py_INCREF(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 return (object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106
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 static long
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 weakref_hash(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111 if (self->hash != -1)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 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
114 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
115 return -1;
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 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
118 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 weakref_repr(PyWeakReference *self)
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 char buffer[256];
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 if (PyWeakref_GET_OBJECT(self) == Py_None) {
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
127 PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %lx; dead>",
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
128 (long)(self));
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 else {
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
131 PyOS_snprintf(buffer, sizeof(buffer),
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
132 "<weakref at %#lx; to '%.50s' at %#lx>",
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
133 (long)(self),
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
134 PyWeakref_GET_OBJECT(self)->ob_type->tp_name,
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
135 (long)(PyWeakref_GET_OBJECT(self)));
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 return PyString_FromString(buffer);
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 /* 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
141 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
142 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
143
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
144 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145 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
146 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147 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
148 Py_INCREF(Py_NotImplemented);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 return Py_NotImplemented;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
150 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
151 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
152 || PyWeakref_GET_OBJECT(other) == Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
153 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
154 Py_INCREF(res);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
155 return res;
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 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
158 PyWeakref_GET_OBJECT(other), op);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
159 }
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
162 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
163 _PyWeakref_RefType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
164 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
165 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166 "weakref",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 (destructor)weakref_dealloc,/*tp_dealloc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170 0, /*tp_print*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171 0, /*tp_getattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 0, /*tp_setattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
173 0, /*tp_compare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
174 (reprfunc)weakref_repr, /*tp_repr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
175 0, /*tp_as_number*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176 0, /*tp_as_sequence*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 0, /*tp_as_mapping*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178 (hashfunc)weakref_hash, /*tp_hash*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179 (ternaryfunc)weakref_call, /*tp_call*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180 0, /*tp_str*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 0, /*tp_getattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182 0, /*tp_setattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 0, /*tp_as_buffer*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_RICHCOMPARE,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 0, /*tp_doc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 (traverseproc)gc_traverse, /*tp_traverse*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 (inquiry)gc_clear, /*tp_clear*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
189 0, /*tp_weaklistoffset*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190 };
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
193 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 proxy_checkref(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
195 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 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
197 PyErr_SetString(PyExc_ReferenceError,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 "weakly-referenced object no longer exists");
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
199 return 0;
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 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
202 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
205 /* 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
206 * 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
207 * 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
208 */
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
209 #define UNWRAP(o) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
210 if (PyWeakref_CheckProxy(o)) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
211 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
212 return NULL; \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
213 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
214 }
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
215
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
216 #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
217 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
218 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
219 return -1; \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
220 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
221 }
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
222
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
223 #define WRAP_UNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
224 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
225 method(PyObject *proxy) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
226 UNWRAP(proxy); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
227 return generic(proxy); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
228 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
229
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
230 #define WRAP_BINARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
231 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
232 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
233 UNWRAP(x); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
234 UNWRAP(y); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
235 return generic(x, y); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
236 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
238 /* 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
239 * 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
240 */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
241 #define WRAP_TERNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
242 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
243 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
244 UNWRAP(proxy); \
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
245 UNWRAP(v); \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
246 if (w != NULL) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
247 UNWRAP(w); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
248 return generic(proxy, v, w); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
249 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
250
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
251
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
252 /* direct slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
253
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
254 WRAP_BINARY(proxy_getattr, PyObject_GetAttr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
255 WRAP_UNARY(proxy_str, PyObject_Str)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
256 WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
257
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
258 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
259 proxy_print(PyWeakReference *proxy, FILE *fp, int flags)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
260 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
261 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 return PyObject_Print(PyWeakref_GET_OBJECT(proxy), fp, flags);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
265
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
267 proxy_repr(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
268 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
269 char buf[160];
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
270 PyOS_snprintf(buf, sizeof(buf),
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
271 "<weakref at %p to %.100s at %p>", proxy,
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
272 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
273 PyWeakref_GET_OBJECT(proxy));
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
274 return PyString_FromString(buf);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
275 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
276
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
277
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
278 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
279 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
280 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
281 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
282 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
283 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
284 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
285
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
286 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
287 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
288 {
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
289 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
290 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
291 return PyObject_Compare(proxy, v);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
292 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
293
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
294 /* number slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
295 WRAP_BINARY(proxy_add, PyNumber_Add)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
296 WRAP_BINARY(proxy_sub, PyNumber_Subtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
297 WRAP_BINARY(proxy_mul, PyNumber_Multiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
298 WRAP_BINARY(proxy_div, PyNumber_Divide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
299 WRAP_BINARY(proxy_mod, PyNumber_Remainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
300 WRAP_BINARY(proxy_divmod, PyNumber_Divmod)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
301 WRAP_TERNARY(proxy_pow, PyNumber_Power)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
302 WRAP_UNARY(proxy_neg, PyNumber_Negative)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
303 WRAP_UNARY(proxy_pos, PyNumber_Positive)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
304 WRAP_UNARY(proxy_abs, PyNumber_Absolute)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
305 WRAP_UNARY(proxy_invert, PyNumber_Invert)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
306 WRAP_BINARY(proxy_lshift, PyNumber_Lshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
307 WRAP_BINARY(proxy_rshift, PyNumber_Rshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
308 WRAP_BINARY(proxy_and, PyNumber_And)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
309 WRAP_BINARY(proxy_xor, PyNumber_Xor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
310 WRAP_BINARY(proxy_or, PyNumber_Or)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
311 WRAP_UNARY(proxy_int, PyNumber_Int)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
312 WRAP_UNARY(proxy_long, PyNumber_Long)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
313 WRAP_UNARY(proxy_float, PyNumber_Float)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
314 WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
315 WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
316 WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
317 WRAP_BINARY(proxy_idiv, PyNumber_InPlaceDivide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
318 WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
319 WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
320 WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
321 WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
322 WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
323 WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
324 WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
325
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
326 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
327 proxy_nonzero(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
328 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
329 PyObject *o = PyWeakref_GET_OBJECT(proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
330 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
331 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
332 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
333 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
334 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
335 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
336 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
337 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
338
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339 /* sequence slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
341 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
342 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
343 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
344 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
345 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346 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
347 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
348
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
350 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
351 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
353 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
354 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
355 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
356
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
357 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358 proxy_contains(PyWeakReference *proxy, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
359 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
360 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
361 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
362 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
363 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
364
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
365
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
366 /* mapping slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
367
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
368 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
369 proxy_length(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
370 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
371 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
372 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
373 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
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 WRAP_BINARY(proxy_getitem, PyObject_GetItem)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
377
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
378 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379 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
380 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
381 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
383 return PyObject_SetItem(PyWeakref_GET_OBJECT(proxy), key, value);
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
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
386 /* iterator slots */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
387
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
388 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
389 proxy_iter(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
390 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
391 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
392 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
393 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
394 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
395
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
396 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
397 proxy_iternext(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
398 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
399 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
400 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
401 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
402 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
403
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
404
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
405 static PyNumberMethods proxy_as_number = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
406 (binaryfunc)proxy_add, /*nb_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
407 (binaryfunc)proxy_sub, /*nb_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
408 (binaryfunc)proxy_mul, /*nb_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
409 (binaryfunc)proxy_div, /*nb_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
410 (binaryfunc)proxy_mod, /*nb_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
411 (binaryfunc)proxy_divmod, /*nb_divmod*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
412 (ternaryfunc)proxy_pow, /*nb_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
413 (unaryfunc)proxy_neg, /*nb_negative*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
414 (unaryfunc)proxy_pos, /*nb_positive*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
415 (unaryfunc)proxy_abs, /*nb_absolute*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
416 (inquiry)proxy_nonzero, /*nb_nonzero*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
417 (unaryfunc)proxy_invert, /*nb_invert*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
418 (binaryfunc)proxy_lshift, /*nb_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
419 (binaryfunc)proxy_rshift, /*nb_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
420 (binaryfunc)proxy_and, /*nb_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
421 (binaryfunc)proxy_xor, /*nb_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
422 (binaryfunc)proxy_or, /*nb_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
423 (coercion)0, /*nb_coerce*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
424 (unaryfunc)proxy_int, /*nb_int*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
425 (unaryfunc)proxy_long, /*nb_long*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
426 (unaryfunc)proxy_float, /*nb_float*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
427 (unaryfunc)0, /*nb_oct*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
428 (unaryfunc)0, /*nb_hex*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
429 (binaryfunc)proxy_iadd, /*nb_inplace_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
430 (binaryfunc)proxy_isub, /*nb_inplace_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
431 (binaryfunc)proxy_imul, /*nb_inplace_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
432 (binaryfunc)proxy_idiv, /*nb_inplace_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
433 (binaryfunc)proxy_imod, /*nb_inplace_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
434 (ternaryfunc)proxy_ipow, /*nb_inplace_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
435 (binaryfunc)proxy_ilshift, /*nb_inplace_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
436 (binaryfunc)proxy_irshift, /*nb_inplace_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
437 (binaryfunc)proxy_iand, /*nb_inplace_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
438 (binaryfunc)proxy_ixor, /*nb_inplace_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
439 (binaryfunc)proxy_ior, /*nb_inplace_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
440 };
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 PySequenceMethods proxy_as_sequence = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
443 (inquiry)proxy_length, /*sq_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
444 0, /*sq_concat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
445 0, /*sq_repeat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
446 0, /*sq_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
447 (intintargfunc)proxy_slice, /*sq_slice*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
448 0, /*sq_ass_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
449 (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
450 (objobjproc)proxy_contains, /* sq_contains */
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 static PyMappingMethods proxy_as_mapping = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
454 (inquiry)proxy_length, /*mp_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
455 (binaryfunc)proxy_getitem, /*mp_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
456 (objobjargproc)proxy_setitem, /*mp_ass_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
457 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
458
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
459
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
460 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
461 _PyWeakref_ProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
462 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
463 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
464 "weakproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
465 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
466 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
467 /* methods */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
468 (destructor)weakref_dealloc, /* tp_dealloc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
469 (printfunc)proxy_print, /* tp_print */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
470 0, /* tp_getattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
471 0, /* tp_setattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
472 proxy_compare, /* tp_compare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
473 (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
474 &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
475 &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
476 &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
477 0, /* tp_hash */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
478 (ternaryfunc)0, /* tp_call */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
479 (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
480 (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
481 (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
482 0, /* tp_as_buffer */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
483 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
484 | 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
485 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
486 (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
487 (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
488 0, /* tp_richcompare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
489 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
490 (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
491 (iternextfunc)proxy_iternext, /* tp_iternext */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
492 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
493
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
494
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
495 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
496 _PyWeakref_CallableProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
497 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
498 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
499 "weakcallableproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
500 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
501 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
502 /* methods */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
503 (destructor)weakref_dealloc, /* tp_dealloc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
504 (printfunc)proxy_print, /* tp_print */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
505 0, /* tp_getattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
506 0, /* tp_setattr */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
507 proxy_compare, /* tp_compare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
508 (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
509 &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
510 &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
511 &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
512 0, /* tp_hash */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
513 (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
514 (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
515 (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
516 (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
517 0, /* tp_as_buffer */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
518 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
519 | 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
520 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
521 (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
522 (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
523 0, /* tp_richcompare */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
524 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
525 (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
526 (iternextfunc)proxy_iternext, /* tp_iternext */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
527 };
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
530 /* Given the head of an object's list of weak references, extract the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
531 * two callback-less refs (ref and proxy). Used to determine if the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
532 * shared references exist and to determine the back link for newly
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
533 * inserted references.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
534 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
535 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
536 get_basic_refs(PyWeakReference *head,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
537 PyWeakReference **refp, PyWeakReference **proxyp)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
538 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
539 *refp = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
540 *proxyp = NULL;
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 if (head != NULL && head->wr_callback == NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
543 if (head->ob_type == &_PyWeakref_RefType) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
544 *refp = head;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
545 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
546 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
547 if (head != NULL && head->wr_callback == NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
548 *proxyp = head;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
549 head = head->wr_next;
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 }
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
554 /* Insert 'newref' in the list after 'prev'. Both must be non-NULL. */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
555 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
556 insert_after(PyWeakReference *newref, PyWeakReference *prev)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
557 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
558 newref->wr_prev = prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
559 newref->wr_next = prev->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
560 if (prev->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
561 prev->wr_next->wr_prev = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
562 prev->wr_next = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
563 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
564
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
565 /* Insert 'newref' at the head of the list; 'list' points to the variable
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
566 * that stores the head.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
567 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
568 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
569 insert_head(PyWeakReference *newref, PyWeakReference **list)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
570 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
571 PyWeakReference *next = *list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
572
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
573 newref->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
574 newref->wr_next = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
575 if (next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
576 next->wr_prev = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
577 *list = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
578 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
579
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
580
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
581 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
582 PyWeakref_NewRef(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
583 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
584 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
585 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
586 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
587
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
588 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
589 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
590 "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
591 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
592 return NULL;
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 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
595 get_basic_refs(*list, &ref, &proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
596 if (callback == NULL || callback == Py_None)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
597 /* 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
598 result = ref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
599 if (result != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
600 Py_XINCREF(result);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
601 else {
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
602 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
603 if (result != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
604 if (callback == NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
605 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
606 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
607 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
608 PyWeakReference *prev = (proxy == NULL) ? ref : proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
609
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
610 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
611 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
612 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
613 insert_after(result, prev);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
614 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
615 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
616 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
617 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
618 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
619
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
620
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
621 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
622 PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
623 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
624 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
625 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
626 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
627
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
628 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
629 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
630 "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
631 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
632 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
633 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
634 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
635 get_basic_refs(*list, &ref, &proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
636 if (callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
637 /* 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
638 result = proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
639 if (result != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
640 Py_XINCREF(result);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
641 else {
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
642 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
643 if (result != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
644 PyWeakReference *prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
645
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
646 if (PyCallable_Check(ob))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
647 result->ob_type = &_PyWeakref_CallableProxyType;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
648 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
649 result->ob_type = &_PyWeakref_ProxyType;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
650 if (callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
651 prev = ref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
652 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
653 prev = (proxy == NULL) ? ref : proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
654
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
655 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
656 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
657 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
658 insert_after(result, prev);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
659 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
660 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
661 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
662 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
663
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
664
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
665 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
666 PyWeakref_GetObject(PyObject *ref)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
667 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
668 if (ref == NULL || !PyWeakref_Check(ref)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
669 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
670 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
671 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
672 return PyWeakref_GET_OBJECT(ref);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
673 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
674
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
675
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
676 static void
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
677 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
678 {
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
679 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
680
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
681 if (cbresult == NULL)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
682 PyErr_WriteUnraisable(callback);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
683 else
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
684 Py_DECREF(cbresult);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
685 }
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
686
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
687 /* 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
688 *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
689 * 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
690 * 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
691 * been attempted.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
692 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
693 void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
694 PyObject_ClearWeakRefs(PyObject *object)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
695 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
696 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
697
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
698 if (object == NULL
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
699 || !PyType_SUPPORTS_WEAKREFS(object->ob_type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
700 || object->ob_refcnt != 0) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
701 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
702 return;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
703 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
704 list = GET_WEAKREFS_LISTPTR(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
705 /* 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
706 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
707 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
708 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
709 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
710 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
711 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
712 PyWeakReference *current = *list;
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
713 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
714 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
715 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
716
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
717 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
718 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
719 if (count == 1) {
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
720 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
721
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
722 current->wr_callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
723 clear_weakref(current);
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
724 handle_callback(current, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
725 Py_DECREF(callback);
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 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
728 PyObject *tuple = PyTuple_New(count * 2);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
729 int i = 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
730
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
731 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
732 PyWeakReference *next = current->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
733
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
734 Py_INCREF(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
735 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
736 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
737 current->wr_callback = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
738 clear_weakref(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
739 current = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
740 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
741 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
742 PyObject *current = PyTuple_GET_ITEM(tuple, i * 2);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
743 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
744
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
745 handle_callback((PyWeakReference *)current, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
746 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
747 Py_DECREF(tuple);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
748 }
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
749 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
750 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
751 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
752 }