annotate Objects/weakrefobject.c @ 20354:fdc1914406e2 legacy-trunk

Updated, and added a very terse description of PyMac_SetConsoleHandler().
author Jack Jansen <jack.jansen@cwi.nl>
date Tue, 09 Oct 2001 23:14:06 +0000
parents 97cfa461d793
children cb87bd97f740
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 static PyWeakReference *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 free_list = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 _PyWeakref_GetWeakrefCount(PyWeakReference *head)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 long count = 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 while (head != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 ++count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 head = head->wr_next;
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 return count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23
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 static PyWeakReference *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 new_weakref(void)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 PyWeakReference *result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30 if (free_list != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 result = free_list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 free_list = result->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 result->ob_type = &_PyWeakref_RefType;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 _Py_NewReference((PyObject *)result);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);
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 if (result)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 result->hash = -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 return result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 /* This function clears the passed-in reference and removes it from the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 * list of weak references for the referent. This is the only code that
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 * removes an item from the doubly-linked list of weak references for an
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 * object; it is also responsible for clearing the callback slot.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 clear_weakref(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 PyObject *callback = self->wr_callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 if (PyWeakref_GET_OBJECT(self) != Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 PyWeakReference **list = GET_WEAKREFS_LISTPTR(
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 PyWeakref_GET_OBJECT(self));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 if (*list == self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 *list = self->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 self->wr_object = Py_None;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 self->wr_callback = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 if (self->wr_prev != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 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
65 if (self->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 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
67 self->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 self->wr_next = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 Py_XDECREF(callback);
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 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 weakref_dealloc(PyWeakReference *self)
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 PyObject_GC_UnTrack((PyObject *)self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 clear_weakref(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 self->wr_next = free_list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80 free_list = self;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82
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 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 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
86 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 if (self->wr_callback != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88 return visit(self->wr_callback, arg);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 }
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 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 gc_clear(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 clear_weakref(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 return 0;
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
101 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 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
103 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
104 static char *argnames[] = {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 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
107 PyObject *object = PyWeakref_GET_OBJECT(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
108 Py_INCREF(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 return (object);
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 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 static long
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 weakref_hash(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 if (self->hash != -1)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 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
121 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
122 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124 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
125 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 weakref_repr(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132 char buffer[256];
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
133 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
134 sprintf(buffer, "<weakref at %lx; dead>",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 (long)(self));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 sprintf(buffer, "<weakref at %#lx; to '%s' at %#lx>",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139 (long)(self), PyWeakref_GET_OBJECT(self)->ob_type->tp_name,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140 (long)(PyWeakref_GET_OBJECT(self)));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
142 return PyString_FromString(buffer);
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145 /* 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
146 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
147 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
148
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
149 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
150 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
151 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
152 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
153 Py_INCREF(Py_NotImplemented);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
154 return Py_NotImplemented;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
155 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
156 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
157 || PyWeakref_GET_OBJECT(other) == Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158 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
159 Py_INCREF(res);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
160 return res;
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 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
163 PyWeakref_GET_OBJECT(other), op);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
164 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
165
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 _PyWeakref_RefType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171 "weakref",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
173 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
174 (destructor)weakref_dealloc,/*tp_dealloc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
175 0, /*tp_print*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176 0, /*tp_getattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 0, /*tp_setattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178 0, /*tp_compare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179 (reprfunc)weakref_repr, /*tp_repr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180 0, /*tp_as_number*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 0, /*tp_as_sequence*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182 0, /*tp_as_mapping*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 (hashfunc)weakref_hash, /*tp_hash*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184 (ternaryfunc)weakref_call, /*tp_call*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 0, /*tp_str*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 0, /*tp_getattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 0, /*tp_setattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 0, /*tp_as_buffer*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
189 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
190 0, /*tp_doc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
191 (traverseproc)gc_traverse, /*tp_traverse*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
192 (inquiry)gc_clear, /*tp_clear*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
193 (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 0, /*tp_weaklistoffset*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
195 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
199 proxy_checkref(PyWeakReference *proxy)
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 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
202 PyErr_SetString(PyExc_ReferenceError,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203 "weakly-referenced object no longer exists");
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
206 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
207 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
208
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
209
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
210 #define WRAP_UNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211 static PyObject * \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
212 method(PyWeakReference *proxy) { \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
213 if (!proxy_checkref(proxy)) { \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
214 return NULL; \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
215 } \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
216 return generic(PyWeakref_GET_OBJECT(proxy)); \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
217 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
218
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
219 #define WRAP_BINARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
220 static PyObject * \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
221 method(PyWeakReference *proxy, PyObject *v) { \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
222 if (!proxy_checkref(proxy)) { \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
223 return NULL; \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
224 } \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
225 return generic(PyWeakref_GET_OBJECT(proxy), v); \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
226 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
227
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
228 #define WRAP_TERNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
229 static PyObject * \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
230 method(PyWeakReference *proxy, PyObject *v, PyObject *w) { \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
231 if (!proxy_checkref(proxy)) { \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
232 return NULL; \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
233 } \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
234 return generic(PyWeakref_GET_OBJECT(proxy), v, w); \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
235 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
236
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
238 /* direct slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
239
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
240 WRAP_BINARY(proxy_getattr, PyObject_GetAttr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
241 WRAP_UNARY(proxy_str, PyObject_Str)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
242 WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
243
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
244 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
245 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
246 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
247 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
248 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
249 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
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 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
253 proxy_repr(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
254 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
255 char buf[160];
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
256 sprintf(buf, "<weakref at %p to %.100s at %p>", proxy,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
257 PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
258 PyWeakref_GET_OBJECT(proxy));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
259 return PyString_FromString(buf);
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264 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
265 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
267 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
268 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
269 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
270
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
271 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
272 proxy_compare(PyWeakReference *proxy, PyObject *v)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
273 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
274 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
275 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
276 return PyObject_Compare(PyWeakref_GET_OBJECT(proxy), v);
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
279 /* number slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
280 WRAP_BINARY(proxy_add, PyNumber_Add)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
281 WRAP_BINARY(proxy_sub, PyNumber_Subtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
282 WRAP_BINARY(proxy_mul, PyNumber_Multiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
283 WRAP_BINARY(proxy_div, PyNumber_Divide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
284 WRAP_BINARY(proxy_mod, PyNumber_Remainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
285 WRAP_BINARY(proxy_divmod, PyNumber_Divmod)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
286 WRAP_TERNARY(proxy_pow, PyNumber_Power)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
287 WRAP_UNARY(proxy_neg, PyNumber_Negative)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
288 WRAP_UNARY(proxy_pos, PyNumber_Positive)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
289 WRAP_UNARY(proxy_abs, PyNumber_Absolute)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
290 WRAP_UNARY(proxy_invert, PyNumber_Invert)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
291 WRAP_BINARY(proxy_lshift, PyNumber_Lshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
292 WRAP_BINARY(proxy_rshift, PyNumber_Rshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
293 WRAP_BINARY(proxy_and, PyNumber_And)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
294 WRAP_BINARY(proxy_xor, PyNumber_Xor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
295 WRAP_BINARY(proxy_or, PyNumber_Or)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
296 WRAP_UNARY(proxy_int, PyNumber_Int)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
297 WRAP_UNARY(proxy_long, PyNumber_Long)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
298 WRAP_UNARY(proxy_float, PyNumber_Float)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
299 WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
300 WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
301 WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
302 WRAP_BINARY(proxy_idiv, PyNumber_InPlaceDivide)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
303 WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
304 WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
305 WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
306 WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
307 WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
308 WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
309 WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
310
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
311 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
312 proxy_nonzero(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
313 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
314 PyObject *o = PyWeakref_GET_OBJECT(proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
315 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
316 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
317 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
318 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
319 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
320 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
321 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
322 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
323
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
324 /* sequence slots */
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 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
327 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
328 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
329 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
330 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
331 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
332 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
333
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
334 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
335 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
336 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
337 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
338 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339 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
340 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
341
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
342 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
343 proxy_contains(PyWeakReference *proxy, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
344 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
345 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
347 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
348 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
350
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
351 /* mapping slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
353 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
354 proxy_length(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
355 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
356 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
357 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358 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
359 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
360
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
361 WRAP_BINARY(proxy_getitem, PyObject_GetItem)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
362
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
363 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
364 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
365 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
366 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
367 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
368 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
369 }
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
372 static PyNumberMethods proxy_as_number = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
373 (binaryfunc)proxy_add, /*nb_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
374 (binaryfunc)proxy_sub, /*nb_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
375 (binaryfunc)proxy_mul, /*nb_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
376 (binaryfunc)proxy_div, /*nb_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
377 (binaryfunc)proxy_mod, /*nb_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
378 (binaryfunc)proxy_divmod, /*nb_divmod*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379 (ternaryfunc)proxy_pow, /*nb_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
380 (unaryfunc)proxy_neg, /*nb_negative*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
381 (unaryfunc)proxy_pos, /*nb_positive*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382 (unaryfunc)proxy_abs, /*nb_absolute*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
383 (inquiry)proxy_nonzero, /*nb_nonzero*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
384 (unaryfunc)proxy_invert, /*nb_invert*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
385 (binaryfunc)proxy_lshift, /*nb_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
386 (binaryfunc)proxy_rshift, /*nb_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
387 (binaryfunc)proxy_and, /*nb_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
388 (binaryfunc)proxy_xor, /*nb_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
389 (binaryfunc)proxy_or, /*nb_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
390 (coercion)0, /*nb_coerce*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
391 (unaryfunc)proxy_int, /*nb_int*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
392 (unaryfunc)proxy_long, /*nb_long*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
393 (unaryfunc)proxy_float, /*nb_float*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
394 (unaryfunc)0, /*nb_oct*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
395 (unaryfunc)0, /*nb_hex*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
396 (binaryfunc)proxy_iadd, /*nb_inplace_add*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
397 (binaryfunc)proxy_isub, /*nb_inplace_subtract*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
398 (binaryfunc)proxy_imul, /*nb_inplace_multiply*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
399 (binaryfunc)proxy_idiv, /*nb_inplace_divide*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
400 (binaryfunc)proxy_imod, /*nb_inplace_remainder*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
401 (ternaryfunc)proxy_ipow, /*nb_inplace_power*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
402 (binaryfunc)proxy_ilshift, /*nb_inplace_lshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
403 (binaryfunc)proxy_irshift, /*nb_inplace_rshift*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
404 (binaryfunc)proxy_iand, /*nb_inplace_and*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
405 (binaryfunc)proxy_ixor, /*nb_inplace_xor*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
406 (binaryfunc)proxy_ior, /*nb_inplace_or*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
407 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
408
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
409 static PySequenceMethods proxy_as_sequence = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
410 (inquiry)proxy_length, /*sq_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
411 0, /*sq_concat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
412 0, /*sq_repeat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
413 0, /*sq_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
414 (intintargfunc)proxy_slice, /*sq_slice*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
415 0, /*sq_ass_item*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
416 (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
417 (objobjproc)proxy_contains, /* sq_contains */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
418 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
419
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
420 static PyMappingMethods proxy_as_mapping = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
421 (inquiry)proxy_length, /*mp_length*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
422 (binaryfunc)proxy_getitem, /*mp_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
423 (objobjargproc)proxy_setitem, /*mp_ass_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
424 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
425
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
426
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
427 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
428 _PyWeakref_ProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
429 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
430 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
431 "weakproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
432 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
433 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
434 /* methods */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
435 (destructor)weakref_dealloc,/*tp_dealloc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
436 (printfunc)proxy_print, /*tp_print*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
437 0, /*tp_getattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
438 0, /*tp_setattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
439 (cmpfunc)proxy_compare, /*tp_compare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
440 (unaryfunc)proxy_repr, /*tp_repr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
441 &proxy_as_number, /*tp_as_number*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
442 &proxy_as_sequence, /*tp_as_sequence*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
443 &proxy_as_mapping, /*tp_as_mapping*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
444 0, /*tp_hash*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
445 (ternaryfunc)0, /*tp_call*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
446 (unaryfunc)proxy_str, /*tp_str*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
447 (getattrofunc)proxy_getattr,/*tp_getattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
448 (setattrofunc)proxy_setattr,/*tp_setattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
449 0, /*tp_as_buffer*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
450 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
451 |Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
452 0, /*tp_doc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
453 (traverseproc)gc_traverse, /*tp_traverse*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
454 (inquiry)gc_clear, /*tp_clear*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
455 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
456
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
457
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
458 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
459 _PyWeakref_CallableProxyType = {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
460 PyObject_HEAD_INIT(&PyType_Type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
461 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
462 "weakcallableproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
463 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
464 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
465 /* methods */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
466 (destructor)weakref_dealloc,/*tp_dealloc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
467 (printfunc)proxy_print, /*tp_print*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
468 0, /*tp_getattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
469 0, /*tp_setattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
470 (cmpfunc)proxy_compare, /*tp_compare*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
471 (unaryfunc)proxy_repr, /*tp_repr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
472 &proxy_as_number, /*tp_as_number*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
473 &proxy_as_sequence, /*tp_as_sequence*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
474 &proxy_as_mapping, /*tp_as_mapping*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
475 0, /*tp_hash*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
476 (ternaryfunc)proxy_call, /*tp_call*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
477 (unaryfunc)proxy_str, /*tp_str*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
478 (getattrofunc)proxy_getattr,/*tp_getattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
479 (setattrofunc)proxy_setattr,/*tp_setattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
480 0, /*tp_as_buffer*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
481 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
482 |Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
483 0, /*tp_doc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
484 (traverseproc)gc_traverse, /*tp_traverse*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
485 (inquiry)gc_clear, /*tp_clear*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
486 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
487
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
488
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
489 /* 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
490 * 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
491 * 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
492 * inserted references.
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 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
495 get_basic_refs(PyWeakReference *head,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
496 PyWeakReference **refp, PyWeakReference **proxyp)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
497 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
498 *refp = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
499 *proxyp = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
500
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
501 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
502 if (head->ob_type == &_PyWeakref_RefType) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
503 *refp = head;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
504 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
505 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
506 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
507 *proxyp = head;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
508 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
509 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
510 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
511 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
512
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
513 /* 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
514 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
515 insert_after(PyWeakReference *newref, PyWeakReference *prev)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
516 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
517 newref->wr_prev = prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
518 newref->wr_next = prev->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
519 if (prev->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
520 prev->wr_next->wr_prev = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
521 prev->wr_next = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
522 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
523
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
524 /* 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
525 * that stores the head.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
526 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
527 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
528 insert_head(PyWeakReference *newref, PyWeakReference **list)
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 PyWeakReference *next = *list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
531
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
532 newref->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
533 newref->wr_next = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
534 if (next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
535 next->wr_prev = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
536 *list = newref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
537 }
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
540 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
541 PyWeakref_NewRef(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
542 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
543 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
544 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
545 PyWeakReference *ref, *proxy;
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 (!PyType_SUPPORTS_WEAKREFS(ob->ob_type)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
548 PyErr_Format(PyExc_TypeError,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
549 "'%s' objects are not weakly referencable",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
550 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
551 return NULL;
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 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
554 get_basic_refs(*list, &ref, &proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
555 if (callback == NULL || callback == Py_None)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
556 /* 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
557 result = ref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
558 if (result != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
559 Py_XINCREF(result);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
560 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
561 result = new_weakref();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
562 if (result != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
563 Py_XINCREF(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
564 result->wr_callback = callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
565 result->wr_object = ob;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
566 if (callback == NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
567 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
568 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
569 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
570 PyWeakReference *prev = (proxy == NULL) ? ref : proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
571
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
572 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
573 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
574 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
575 insert_after(result, prev);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
576 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
577 PyObject_GC_Track(result);
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 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
581 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
582
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
583
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
584 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
585 PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
586 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
587 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
588 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
589 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
590
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
591 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
592 PyErr_Format(PyExc_TypeError,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
593 "'%s' objects are not weakly referencable",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
594 ob->ob_type->tp_name);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
595 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
596 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
597 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
598 get_basic_refs(*list, &ref, &proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
599 if (callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
600 /* 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
601 result = proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
602 if (result != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
603 Py_XINCREF(result);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
604 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
605 result = new_weakref();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
606 if (result != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
607 PyWeakReference *prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
608
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
609 if (PyCallable_Check(ob))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
610 result->ob_type = &_PyWeakref_CallableProxyType;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
611 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
612 result->ob_type = &_PyWeakref_ProxyType;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
613 result->wr_object = ob;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
614 Py_XINCREF(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
615 result->wr_callback = callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
616 if (callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
617 prev = ref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
618 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
619 prev = (proxy == NULL) ? ref : proxy;
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 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
622 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
623 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
624 insert_after(result, prev);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
625 PyObject_GC_Track(result);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
626 }
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 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
629 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
630
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
631
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
632 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
633 PyWeakref_GetObject(PyObject *ref)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
634 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
635 if (ref == NULL || !PyWeakref_Check(ref)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
636 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
637 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
638 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
639 return PyWeakref_GET_OBJECT(ref);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
640 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
641
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
642
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
643 /* This is the implementation of the PyObject_ClearWeakRefs() function; it
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
644 * is installed in the init_weakref() function. It is called by the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
645 * tp_dealloc handler to clear weak references.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
646 *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
647 * 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
648 * 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
649 * been attempted.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
650 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
651 void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
652 PyObject_ClearWeakRefs(PyObject *object)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
653 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
654 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
655
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
656 if (object == NULL
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
657 || !PyType_SUPPORTS_WEAKREFS(object->ob_type)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
658 || object->ob_refcnt != 0) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
659 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
660 return;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
661 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
662 list = GET_WEAKREFS_LISTPTR(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
663 /* 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
664 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
665 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
666 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
667 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
668 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
669 if (*list != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
670 int count = _PyWeakref_GetWeakrefCount(*list);
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 if (count == 1) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
673 PyWeakReference *current = *list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
674 PyObject *callback = current->wr_callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
675 PyObject *cbresult;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
676
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
677 Py_INCREF(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
678 clear_weakref(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
679 cbresult = PyObject_CallFunction(callback, "O", current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
680 if (cbresult == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
681 PyErr_WriteUnraisable(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
682 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
683 Py_DECREF(cbresult);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
684 Py_DECREF(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
685 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
686 else {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
687 PyObject *tuple = PyTuple_New(count * 2);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
688 PyWeakReference *current = *list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
689 int i = 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
690
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
691 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
692 PyWeakReference *next = current->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
693
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
694 Py_INCREF(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
695 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
696 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
697 current->wr_callback = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
698 next = current->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
699 clear_weakref(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
700 current = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
701 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
702 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
703 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
704 PyObject *callback = PyTuple_GET_ITEM(tuple, i * 2 + 1);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
705 PyObject *cbresult = PyObject_CallFunction(callback, "O",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
706 current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
707 if (cbresult == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
708 PyErr_WriteUnraisable(callback);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
709 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
710 Py_DECREF(cbresult);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
711 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
712 Py_DECREF(tuple);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
713 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
714 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
715 }