annotate Objects/weakrefobject.c @ 73807:1efefeda00a7 3.2

#8414: add more tests for "assert". Initial patch by Gregory Nofi.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 02 Dec 2011 18:22:52 +0200
parents 448ab7516c0a
children 590f1b55abea e2f96bd29252
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
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
9 Py_ssize_t
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 _PyWeakref_GetWeakrefCount(PyWeakReference *head)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11 {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
12 Py_ssize_t count = 0;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 while (head != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 ++count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 head = head->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 return count;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
22 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
23 init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
24 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
25 self->hash = -1;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
26 self->wr_object = ob;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
27 Py_XINCREF(callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
28 self->wr_callback = callback;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
29 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
30
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 static PyWeakReference *
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
32 new_weakref(PyObject *ob, PyObject *callback)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 PyWeakReference *result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
36 result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
37 if (result) {
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
38 init_weakref(result, ob, callback);
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
39 PyObject_GC_Track(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 return result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 /* This function clears the passed-in reference and removes it from the
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 * list of weak references for the referent. This is the only code that
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 * removes an item from the doubly-linked list of weak references for an
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 * object; it is also responsible for clearing the callback slot.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 static void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 clear_weakref(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 PyObject *callback = self->wr_callback;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 if (PyWeakref_GET_OBJECT(self) != Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 PyWeakReference **list = GET_WEAKREFS_LISTPTR(
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 PyWeakref_GET_OBJECT(self));
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 if (*list == self)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
60 /* If 'self' is the end of the list (and thus self->wr_next == NULL)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
61 then the weakref list itself (and thus the value of *list) will
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
62 end up being set to NULL. */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 *list = self->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 self->wr_object = Py_None;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 if (self->wr_prev != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 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
67 if (self->wr_next != NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 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
69 self->wr_prev = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 self->wr_next = NULL;
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
71 }
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
72 if (callback != NULL) {
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
73 Py_DECREF(callback);
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
74 self->wr_callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
78 /* Cyclic gc uses this to *just* clear the passed-in reference, leaving
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
79 * the callback intact and uncalled. It must be possible to call self's
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
80 * tp_dealloc() after calling this, so self has to be left in a sane enough
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
81 * state for that to work. We expect tp_dealloc to decref the callback
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
82 * then. The reason for not letting clear_weakref() decref the callback
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
83 * right now is that if the callback goes away, that may in turn trigger
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
84 * another callback (if a weak reference to the callback exists) -- running
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
85 * arbitrary Python code in the middle of gc is a disaster. The convolution
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
86 * here allows gc to delay triggering such callbacks until the world is in
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
87 * a sane state again.
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
88 */
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
89 void
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
90 _PyWeakref_ClearRef(PyWeakReference *self)
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
91 {
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
92 PyObject *callback;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
93
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
94 assert(self != NULL);
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
95 assert(PyWeakref_Check(self));
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
96 /* Preserve and restore the callback around clear_weakref. */
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
97 callback = self->wr_callback;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
98 self->wr_callback = NULL;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
99 clear_weakref(self);
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
100 self->wr_callback = callback;
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
101 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 static void
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
104 weakref_dealloc(PyObject *self)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 {
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
106 PyObject_GC_UnTrack(self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
107 clear_weakref((PyWeakReference *) self);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
108 Py_TYPE(self)->tp_free(self);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
113 gc_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
114 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
115 Py_VISIT(self->wr_callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 gc_clear(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 clear_weakref(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126
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 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129 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
130 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
131 static char *kwlist[] = {NULL};
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
132
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
133 if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", kwlist)) {
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
134 PyObject *object = PyWeakref_GET_OBJECT(self);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
135 Py_INCREF(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
136 return (object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
137 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
138 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
139 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
140
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
141
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 61466
diff changeset
142 static Py_hash_t
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
143 weakref_hash(PyWeakReference *self)
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 if (self->hash != -1)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
146 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
147 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
148 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
149 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
150 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
151 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
152 return self->hash;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
153 }
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
154
20280
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 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
157 weakref_repr(PyWeakReference *self)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
158 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
159 char buffer[256];
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
160 if (PyWeakref_GET_OBJECT(self) == Py_None) {
28644
8e541b078edb - The repr() of a weakref object now shows the __name__ attribute of
Guido van Rossum <guido@python.org>
parents: 24717
diff changeset
161 PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
162 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
163 else {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
164 char *name = NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
165 PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self),
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
166 "__name__");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
167 if (nameobj == NULL)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
168 PyErr_Clear();
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
169 else if (PyUnicode_Check(nameobj))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
170 name = _PyUnicode_AsString(nameobj);
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
171 PyOS_snprintf(buffer, sizeof(buffer),
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
172 name ? "<weakref at %p; to '%.50s' at %p (%s)>"
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
173 : "<weakref at %p; to '%.50s' at %p>",
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
174 self,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
175 Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
176 PyWeakref_GET_OBJECT(self),
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
177 name);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
178 Py_XDECREF(nameobj);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179 }
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 40737
diff changeset
180 return PyUnicode_FromString(buffer);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
181 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
182
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
183 /* 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
184 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
185 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
186
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188 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
189 {
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
190 if ((op != Py_EQ && op != Py_NE) ||
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
191 !PyWeakref_Check(self) ||
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
192 !PyWeakref_Check(other)) {
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
193 Py_INCREF(Py_NotImplemented);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
194 return Py_NotImplemented;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
195 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 if (PyWeakref_GET_OBJECT(self) == Py_None
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 || PyWeakref_GET_OBJECT(other) == Py_None) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198 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
199 Py_INCREF(res);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
200 return res;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
201 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
202 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
203 PyWeakref_GET_OBJECT(other), op);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
204 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
206 /* Given the head of an object's list of weak references, extract the
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
207 * two callback-less refs (ref and proxy). Used to determine if the
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
208 * shared references exist and to determine the back link for newly
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
209 * inserted references.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
210 */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
211 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
212 get_basic_refs(PyWeakReference *head,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
213 PyWeakReference **refp, PyWeakReference **proxyp)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
214 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
215 *refp = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
216 *proxyp = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
217
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
218 if (head != NULL && head->wr_callback == NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
219 /* We need to be careful that the "basic refs" aren't
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
220 subclasses of the main types. That complicates this a
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
221 little. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
222 if (PyWeakref_CheckRefExact(head)) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
223 *refp = head;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
224 head = head->wr_next;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
225 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
226 if (head != NULL
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
227 && head->wr_callback == NULL
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
228 && PyWeakref_CheckProxy(head)) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
229 *proxyp = head;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
230 /* head = head->wr_next; */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
231 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
232 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
233 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
234
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
235 /* Insert 'newref' in the list after 'prev'. Both must be non-NULL. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
236 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
237 insert_after(PyWeakReference *newref, PyWeakReference *prev)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
238 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
239 newref->wr_prev = prev;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
240 newref->wr_next = prev->wr_next;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
241 if (prev->wr_next != NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
242 prev->wr_next->wr_prev = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
243 prev->wr_next = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
244 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
245
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
246 /* Insert 'newref' at the head of the list; 'list' points to the variable
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
247 * that stores the head.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
248 */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
249 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
250 insert_head(PyWeakReference *newref, PyWeakReference **list)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
251 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
252 PyWeakReference *next = *list;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
253
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
254 newref->wr_prev = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
255 newref->wr_next = next;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
256 if (next != NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
257 next->wr_prev = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
258 *list = newref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
259 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
260
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
261 static int
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
262 parse_weakref_init_args(char *funcname, PyObject *args, PyObject *kwargs,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
263 PyObject **obp, PyObject **callbackp)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
264 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
265 /* XXX Should check that kwargs == NULL or is empty. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
266 return PyArg_UnpackTuple(args, funcname, 1, 2, obp, callbackp);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
267 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
268
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
269 static PyObject *
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
270 weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
271 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
272 PyWeakReference *self = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
273 PyObject *ob, *callback = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
274
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
275 if (parse_weakref_init_args("__new__", args, kwargs, &ob, &callback)) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
276 PyWeakReference *ref, *proxy;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
277 PyWeakReference **list;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
278
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
279 if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
280 PyErr_Format(PyExc_TypeError,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
281 "cannot create weak reference to '%s' object",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
282 Py_TYPE(ob)->tp_name);
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
283 return NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
284 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
285 if (callback == Py_None)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
286 callback = NULL;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
287 list = GET_WEAKREFS_LISTPTR(ob);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
288 get_basic_refs(*list, &ref, &proxy);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
289 if (callback == NULL && type == &_PyWeakref_RefType) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
290 if (ref != NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
291 /* We can re-use an existing reference. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
292 Py_INCREF(ref);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
293 return (PyObject *)ref;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
294 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
295 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
296 /* We have to create a new reference. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
297 /* Note: the tp_alloc() can trigger cyclic GC, so the weakref
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
298 list on ob can be mutated. This means that the ref and
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
299 proxy pointers we got back earlier may have been collected,
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
300 so we need to compute these values again before we use
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
301 them. */
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
302 self = (PyWeakReference *) (type->tp_alloc(type, 0));
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
303 if (self != NULL) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
304 init_weakref(self, ob, callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
305 if (callback == NULL && type == &_PyWeakref_RefType) {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
306 insert_head(self, list);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
307 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
308 else {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
309 PyWeakReference *prev;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
310
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
311 get_basic_refs(*list, &ref, &proxy);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
312 prev = (proxy == NULL) ? ref : proxy;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
313 if (prev == NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
314 insert_head(self, list);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
315 else
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
316 insert_after(self, prev);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
317 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
318 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
319 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
320 return (PyObject *)self;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
321 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
322
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
323 static int
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
324 weakref___init__(PyObject *self, PyObject *args, PyObject *kwargs)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
325 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
326 PyObject *tmp;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
327
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
328 if (parse_weakref_init_args("__init__", args, kwargs, &tmp, &tmp))
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
329 return 0;
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
330 else
49137
17475eb55067 Merged revisions 66337,66347,66350,66352,66358 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
331 return -1;
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
332 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
333
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
334
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
335 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
336 _PyWeakref_RefType = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
337 PyVarObject_HEAD_INIT(&PyType_Type, 0)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
338 "weakref",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
339 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
340 0,
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
341 weakref_dealloc, /*tp_dealloc*/
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
342 0, /*tp_print*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
343 0, /*tp_getattr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
344 0, /*tp_setattr*/
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
345 0, /*tp_reserved*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
346 (reprfunc)weakref_repr, /*tp_repr*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
347 0, /*tp_as_number*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
348 0, /*tp_as_sequence*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349 0, /*tp_as_mapping*/
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
350 (hashfunc)weakref_hash, /*tp_hash*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
351 (ternaryfunc)weakref_call, /*tp_call*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352 0, /*tp_str*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
353 0, /*tp_getattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
354 0, /*tp_setattro*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
355 0, /*tp_as_buffer*/
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
356 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
357 | Py_TPFLAGS_BASETYPE, /*tp_flags*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358 0, /*tp_doc*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
359 (traverseproc)gc_traverse, /*tp_traverse*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
360 (inquiry)gc_clear, /*tp_clear*/
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
361 (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
362 0, /*tp_weaklistoffset*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
363 0, /*tp_iter*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
364 0, /*tp_iternext*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
365 0, /*tp_methods*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
366 0, /*tp_members*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
367 0, /*tp_getset*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
368 0, /*tp_base*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
369 0, /*tp_dict*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
370 0, /*tp_descr_get*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
371 0, /*tp_descr_set*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
372 0, /*tp_dictoffset*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
373 weakref___init__, /*tp_init*/
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
374 PyType_GenericAlloc, /*tp_alloc*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
375 weakref___new__, /*tp_new*/
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
376 PyObject_GC_Del, /*tp_free*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
377 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
378
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
379
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
380 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
381 proxy_checkref(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
382 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
383 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
384 PyErr_SetString(PyExc_ReferenceError,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
385 "weakly-referenced object no longer exists");
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
386 return 0;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
387 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
388 return 1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
389 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
390
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
391
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
392 /* If a parameter is a proxy, check that it is still "live" and wrap it,
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
393 * replacing the original value with the raw object. Raises ReferenceError
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
394 * if the param is a dead proxy.
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
395 */
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
396 #define UNWRAP(o) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
397 if (PyWeakref_CheckProxy(o)) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
398 if (!proxy_checkref((PyWeakReference *)o)) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
399 return NULL; \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
400 o = PyWeakref_GET_OBJECT(o); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
401 }
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
402
21483
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
403 #define UNWRAP_I(o) \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
404 if (PyWeakref_CheckProxy(o)) { \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
405 if (!proxy_checkref((PyWeakReference *)o)) \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
406 return -1; \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
407 o = PyWeakref_GET_OBJECT(o); \
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
408 }
873291a4889e proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents: 21339
diff changeset
409
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
410 #define WRAP_UNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
411 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
412 method(PyObject *proxy) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
413 UNWRAP(proxy); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
414 return generic(proxy); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
415 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
416
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
417 #define WRAP_BINARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
418 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
419 method(PyObject *x, PyObject *y) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
420 UNWRAP(x); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
421 UNWRAP(y); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
422 return generic(x, y); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
423 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
424
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
425 /* Note that the third arg needs to be checked for NULL since the tp_call
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
426 * slot can receive NULL for this arg.
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
427 */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
428 #define WRAP_TERNARY(method, generic) \
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
429 static PyObject * \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
430 method(PyObject *proxy, PyObject *v, PyObject *w) { \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
431 UNWRAP(proxy); \
20532
bc138bc73cdd Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents: 20521
diff changeset
432 UNWRAP(v); \
20521
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
433 if (w != NULL) \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
434 UNWRAP(w); \
cb87bd97f740 When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents: 20280
diff changeset
435 return generic(proxy, v, w); \
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
436 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
437
57348
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
438 #define WRAP_METHOD(method, special) \
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
439 static PyObject * \
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
440 method(PyObject *proxy) { \
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
441 UNWRAP(proxy); \
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
442 return PyObject_CallMethod(proxy, special, ""); \
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
443 }
57348
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
444
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
445
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
446 /* direct slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
447
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
448 WRAP_BINARY(proxy_getattr, PyObject_GetAttr)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
449 WRAP_UNARY(proxy_str, PyObject_Str)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
450 WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
451
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
452 static PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
453 proxy_repr(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
454 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
455 char buf[160];
21083
d3b6401ca273 weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
456 PyOS_snprintf(buf, sizeof(buf),
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
457 "<weakproxy at %p to %.100s at %p>", proxy,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
458 Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
459 PyWeakref_GET_OBJECT(proxy));
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 40737
diff changeset
460 return PyUnicode_FromString(buf);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
461 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
462
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
463
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
464 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
465 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
466 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
467 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
468 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
469 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
470 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
471
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
472 static PyObject *
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
473 proxy_richcompare(PyObject *proxy, PyObject *v, int op)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
474 {
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
475 UNWRAP(proxy);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
476 UNWRAP(v);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
477 return PyObject_RichCompare(proxy, v, op);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
478 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
479
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
480 /* number slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
481 WRAP_BINARY(proxy_add, PyNumber_Add)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
482 WRAP_BINARY(proxy_sub, PyNumber_Subtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
483 WRAP_BINARY(proxy_mul, PyNumber_Multiply)
47278
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
484 WRAP_BINARY(proxy_floor_div, PyNumber_FloorDivide)
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
485 WRAP_BINARY(proxy_true_div, PyNumber_TrueDivide)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
486 WRAP_BINARY(proxy_mod, PyNumber_Remainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
487 WRAP_BINARY(proxy_divmod, PyNumber_Divmod)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
488 WRAP_TERNARY(proxy_pow, PyNumber_Power)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
489 WRAP_UNARY(proxy_neg, PyNumber_Negative)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
490 WRAP_UNARY(proxy_pos, PyNumber_Positive)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
491 WRAP_UNARY(proxy_abs, PyNumber_Absolute)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
492 WRAP_UNARY(proxy_invert, PyNumber_Invert)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
493 WRAP_BINARY(proxy_lshift, PyNumber_Lshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
494 WRAP_BINARY(proxy_rshift, PyNumber_Rshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
495 WRAP_BINARY(proxy_and, PyNumber_And)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
496 WRAP_BINARY(proxy_xor, PyNumber_Xor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
497 WRAP_BINARY(proxy_or, PyNumber_Or)
50866
8325eb041921 Merged revisions 68675 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50829
diff changeset
498 WRAP_UNARY(proxy_int, PyNumber_Long)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
499 WRAP_UNARY(proxy_float, PyNumber_Float)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
500 WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
501 WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
502 WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)
47278
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
503 WRAP_BINARY(proxy_ifloor_div, PyNumber_InPlaceFloorDivide)
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
504 WRAP_BINARY(proxy_itrue_div, PyNumber_InPlaceTrueDivide)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
505 WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
506 WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
507 WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
508 WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
509 WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
510 WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
511 WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
47278
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
512 WRAP_UNARY(proxy_index, PyNumber_Index)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
513
30991
14d321466a06 SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 29863
diff changeset
514 static int
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39644
diff changeset
515 proxy_bool(PyWeakReference *proxy)
20280
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 PyObject *o = PyWeakref_GET_OBJECT(proxy);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
518 if (!proxy_checkref(proxy))
32530
2451c9572edf SF bug #978308, Spurious errors taking bool of dead pro
Neal Norwitz <nnorwitz@gmail.com>
parents: 32472
diff changeset
519 return -1;
34847
51ba8ce8d92a SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents: 33862
diff changeset
520 return PyObject_IsTrue(o);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
521 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
522
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
523 static void
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
524 proxy_dealloc(PyWeakReference *self)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
525 {
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
526 if (self->wr_callback != NULL)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
527 PyObject_GC_UnTrack((PyObject *)self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
528 clear_weakref(self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
529 PyObject_GC_Del(self);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
530 }
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
531
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
532 /* sequence slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
533
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
534 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
535 proxy_contains(PyWeakReference *proxy, PyObject *value)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
536 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
537 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
538 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
539 return PySequence_Contains(PyWeakref_GET_OBJECT(proxy), value);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
540 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
541
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
542
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
543 /* mapping slots */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
544
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36244
diff changeset
545 static Py_ssize_t
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
546 proxy_length(PyWeakReference *proxy)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
547 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
548 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
549 return -1;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
550 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
551 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
552
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
553 WRAP_BINARY(proxy_getitem, PyObject_GetItem)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
554
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
555 static int
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
556 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
557 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
558 if (!proxy_checkref(proxy))
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
559 return -1;
29697
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
560
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
561 if (value == NULL)
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
562 return PyObject_DelItem(PyWeakref_GET_OBJECT(proxy), key);
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
563 else
1d763862d1a1 Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents: 28644
diff changeset
564 return PyObject_SetItem(PyWeakref_GET_OBJECT(proxy), key, value);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
565 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
566
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
567 /* iterator slots */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
568
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
569 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
570 proxy_iter(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
571 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
572 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
573 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
574 return PyObject_GetIter(PyWeakref_GET_OBJECT(proxy));
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
575 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
576
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
577 static PyObject *
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
578 proxy_iternext(PyWeakReference *proxy)
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
579 {
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
580 if (!proxy_checkref(proxy))
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
581 return NULL;
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
582 return PyIter_Next(PyWeakref_GET_OBJECT(proxy));
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
583 }
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
584
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
585
66104
448ab7516c0a Issue #10359: Remove ";" after function definition, invalid in ISO C
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65494
diff changeset
586 WRAP_METHOD(proxy_bytes, "__bytes__")
57348
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
587
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
588
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
589 static PyMethodDef proxy_methods[] = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
590 {"__bytes__", (PyCFunction)proxy_bytes, METH_NOARGS},
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
591 {NULL, NULL}
57348
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
592 };
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
593
42aaedaefbcb Merged revisions 76396 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51271
diff changeset
594
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
595 static PyNumberMethods proxy_as_number = {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
596 proxy_add, /*nb_add*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
597 proxy_sub, /*nb_subtract*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
598 proxy_mul, /*nb_multiply*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
599 proxy_mod, /*nb_remainder*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
600 proxy_divmod, /*nb_divmod*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
601 proxy_pow, /*nb_power*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
602 proxy_neg, /*nb_negative*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
603 proxy_pos, /*nb_positive*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
604 proxy_abs, /*nb_absolute*/
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39644
diff changeset
605 (inquiry)proxy_bool, /*nb_bool*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
606 proxy_invert, /*nb_invert*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
607 proxy_lshift, /*nb_lshift*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
608 proxy_rshift, /*nb_rshift*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
609 proxy_and, /*nb_and*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
610 proxy_xor, /*nb_xor*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
611 proxy_or, /*nb_or*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
612 proxy_int, /*nb_int*/
50829
af02b6e42142 Merged revisions 68650-68653 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50804
diff changeset
613 0, /*nb_reserved*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
614 proxy_float, /*nb_float*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
615 proxy_iadd, /*nb_inplace_add*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
616 proxy_isub, /*nb_inplace_subtract*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
617 proxy_imul, /*nb_inplace_multiply*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
618 proxy_imod, /*nb_inplace_remainder*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
619 proxy_ipow, /*nb_inplace_power*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
620 proxy_ilshift, /*nb_inplace_lshift*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
621 proxy_irshift, /*nb_inplace_rshift*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
622 proxy_iand, /*nb_inplace_and*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
623 proxy_ixor, /*nb_inplace_xor*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
624 proxy_ior, /*nb_inplace_or*/
47278
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
625 proxy_floor_div, /*nb_floor_divide*/
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
626 proxy_true_div, /*nb_true_divide*/
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
627 proxy_ifloor_div, /*nb_inplace_floor_divide*/
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
628 proxy_itrue_div, /*nb_inplace_true_divide*/
f762a9ab69dd Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44351
diff changeset
629 proxy_index, /*nb_index*/
20280
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 static PySequenceMethods proxy_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36244
diff changeset
633 (lenfunc)proxy_length, /*sq_length*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
634 0, /*sq_concat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
635 0, /*sq_repeat*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
636 0, /*sq_item*/
43100
4e6b4ba60c3b Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents: 42824
diff changeset
637 0, /*sq_slice*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
638 0, /*sq_ass_item*/
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
639 0, /*sq_ass_slice*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
640 (objobjproc)proxy_contains, /* sq_contains */
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 static PyMappingMethods proxy_as_mapping = {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
644 (lenfunc)proxy_length, /*mp_length*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
645 proxy_getitem, /*mp_subscript*/
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
646 (objobjargproc)proxy_setitem, /*mp_ass_subscript*/
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
647 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
648
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
649
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
650 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
651 _PyWeakref_ProxyType = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
652 PyVarObject_HEAD_INIT(&PyType_Type, 0)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
653 "weakproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
654 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
655 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
656 /* methods */
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
657 (destructor)proxy_dealloc, /* tp_dealloc */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
658 0, /* tp_print */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
659 0, /* tp_getattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
660 0, /* tp_setattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
661 0, /* tp_reserved */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
662 (reprfunc)proxy_repr, /* tp_repr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
663 &proxy_as_number, /* tp_as_number */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
664 &proxy_as_sequence, /* tp_as_sequence */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
665 &proxy_as_mapping, /* tp_as_mapping */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
666 0, /* tp_hash */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
667 0, /* tp_call */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
668 proxy_str, /* tp_str */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
669 proxy_getattr, /* tp_getattro */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
670 (setattrofunc)proxy_setattr, /* tp_setattro */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
671 0, /* tp_as_buffer */
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
672 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
673 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
674 (traverseproc)gc_traverse, /* tp_traverse */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
675 (inquiry)gc_clear, /* tp_clear */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
676 proxy_richcompare, /* tp_richcompare */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
677 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
678 (getiterfunc)proxy_iter, /* tp_iter */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
679 (iternextfunc)proxy_iternext, /* tp_iternext */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
680 proxy_methods, /* tp_methods */
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
681 };
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
682
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
683
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
684 PyTypeObject
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
685 _PyWeakref_CallableProxyType = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
686 PyVarObject_HEAD_INIT(&PyType_Type, 0)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
687 "weakcallableproxy",
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
688 sizeof(PyWeakReference),
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
689 0,
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
690 /* methods */
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
691 (destructor)proxy_dealloc, /* tp_dealloc */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
692 0, /* tp_print */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
693 0, /* tp_getattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
694 0, /* tp_setattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
695 0, /* tp_reserved */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
696 (unaryfunc)proxy_repr, /* tp_repr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
697 &proxy_as_number, /* tp_as_number */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
698 &proxy_as_sequence, /* tp_as_sequence */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
699 &proxy_as_mapping, /* tp_as_mapping */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
700 0, /* tp_hash */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
701 proxy_call, /* tp_call */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
702 proxy_str, /* tp_str */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
703 proxy_getattr, /* tp_getattro */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
704 (setattrofunc)proxy_setattr, /* tp_setattro */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
705 0, /* tp_as_buffer */
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
706 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
707 0, /* tp_doc */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
708 (traverseproc)gc_traverse, /* tp_traverse */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
709 (inquiry)gc_clear, /* tp_clear */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
710 proxy_richcompare, /* tp_richcompare */
24717
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
711 0, /* tp_weaklistoffset */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
712 (getiterfunc)proxy_iter, /* tp_iter */
1d3d980a3186 Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents: 22607
diff changeset
713 (iternextfunc)proxy_iternext, /* tp_iternext */
20280
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
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
716
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
717
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
718 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
719 PyWeakref_NewRef(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
720 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
721 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
722 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
723 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
724
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
725 if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
726 PyErr_Format(PyExc_TypeError,
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
727 "cannot create weak reference to '%s' object",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
728 Py_TYPE(ob)->tp_name);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
729 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
730 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
731 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
732 get_basic_refs(*list, &ref, &proxy);
31449
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
733 if (callback == Py_None)
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
734 callback = NULL;
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
735 if (callback == NULL)
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
736 /* 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
737 result = ref;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
738 if (result != NULL)
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
739 Py_INCREF(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
740 else {
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
741 /* Note: new_weakref() can trigger cyclic GC, so the weakref
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
742 list on ob can be mutated. This means that the ref and
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
743 proxy pointers we got back earlier may have been collected,
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
744 so we need to compute these values again before we use
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
745 them. */
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
746 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
747 if (result != NULL) {
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
748 get_basic_refs(*list, &ref, &proxy);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
749 if (callback == NULL) {
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
750 if (ref == NULL)
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
751 insert_head(result, list);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
752 else {
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
753 /* Someone else added a ref without a callback
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
754 during GC. Return that one instead of this one
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
755 to avoid violating the invariants of the list
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
756 of weakrefs for ob. */
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
757 Py_DECREF(result);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
758 Py_INCREF(ref);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
759 result = ref;
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
760 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
761 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
762 else {
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
763 PyWeakReference *prev;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
764
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
765 prev = (proxy == NULL) ? ref : proxy;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
766 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
767 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
768 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
769 insert_after(result, prev);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
770 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
771 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
772 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
773 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
774 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
775
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
776
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
777 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
778 PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
779 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
780 PyWeakReference *result = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
781 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
782 PyWeakReference *ref, *proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
783
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
784 if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
785 PyErr_Format(PyExc_TypeError,
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
786 "cannot create weak reference to '%s' object",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
787 Py_TYPE(ob)->tp_name);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
788 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
789 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
790 list = GET_WEAKREFS_LISTPTR(ob);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
791 get_basic_refs(*list, &ref, &proxy);
31449
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
792 if (callback == Py_None)
b4bc83f2fd04 Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents: 30991
diff changeset
793 callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
794 if (callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
795 /* 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
796 result = proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
797 if (result != NULL)
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
798 Py_INCREF(result);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
799 else {
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
800 /* Note: new_weakref() can trigger cyclic GC, so the weakref
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
801 list on ob can be mutated. This means that the ref and
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
802 proxy pointers we got back earlier may have been collected,
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
803 so we need to compute these values again before we use
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
804 them. */
22607
ac24c2c9bcaf Remove weakref free list. This has the side effect of fixing a memory
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21483
diff changeset
805 result = new_weakref(ob, callback);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
806 if (result != NULL) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
807 PyWeakReference *prev;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
808
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
809 if (PyCallable_Check(ob))
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
810 Py_TYPE(result) = &_PyWeakref_CallableProxyType;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
811 else
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
812 Py_TYPE(result) = &_PyWeakref_ProxyType;
31454
d4165d12f73c Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents: 31449
diff changeset
813 get_basic_refs(*list, &ref, &proxy);
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
814 if (callback == NULL) {
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
815 if (proxy != NULL) {
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
816 /* Someone else added a proxy without a callback
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
817 during GC. Return that one instead of this one
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
818 to avoid violating the invariants of the list
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
819 of weakrefs for ob. */
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
820 Py_DECREF(result);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
821 Py_INCREF(result = proxy);
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
822 goto skip_insert;
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
823 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
824 prev = ref;
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
825 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
826 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
827 prev = (proxy == NULL) ? ref : proxy;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
828
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
829 if (prev == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
830 insert_head(result, list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
831 else
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
832 insert_after(result, prev);
32932
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
833 skip_insert:
bd3dd6b3ff97 Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents: 32530
diff changeset
834 ;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
835 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
836 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
837 return (PyObject *) result;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
838 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
839
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
840
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
841 PyObject *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
842 PyWeakref_GetObject(PyObject *ref)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
843 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
844 if (ref == NULL || !PyWeakref_Check(ref)) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
845 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
846 return NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
847 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
848 return PyWeakref_GET_OBJECT(ref);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
849 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
850
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 32932
diff changeset
851 /* Note that there's an inlined copy-paste of handle_callback() in gcmodule.c's
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 32932
diff changeset
852 * handle_weakrefs().
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 32932
diff changeset
853 */
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
854 static void
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
855 handle_callback(PyWeakReference *ref, PyObject *callback)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
856 {
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
857 PyObject *cbresult = PyObject_CallFunctionObjArgs(callback, ref, NULL);
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
858
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
859 if (cbresult == NULL)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
860 PyErr_WriteUnraisable(callback);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
861 else
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
862 Py_DECREF(cbresult);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
863 }
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
864
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
865 /* This function is called by the tp_dealloc handler to clear weak references.
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
866 *
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
867 * 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
868 * 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
869 * been attempted.
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
870 */
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
871 void
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
872 PyObject_ClearWeakRefs(PyObject *object)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
873 {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
874 PyWeakReference **list;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
875
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
876 if (object == NULL
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 43420
diff changeset
877 || !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
878 || object->ob_refcnt != 0) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
879 PyErr_BadInternalCall();
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
880 return;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
881 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
882 list = GET_WEAKREFS_LISTPTR(object);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
883 /* 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
884 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
885 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
886 if (*list != NULL && (*list)->wr_callback == NULL)
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
887 clear_weakref(*list);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
888 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
889 if (*list != NULL) {
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
890 PyWeakReference *current = *list;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36244
diff changeset
891 Py_ssize_t count = _PyWeakref_GetWeakrefCount(current);
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
892 int restore_error = PyErr_Occurred() ? 1 : 0;
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
893 PyObject *err_type, *err_value, *err_tb;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
894
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
895 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
896 PyErr_Fetch(&err_type, &err_value, &err_tb);
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
897 if (count == 1) {
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
898 PyObject *callback = current->wr_callback;
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
899
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
900 current->wr_callback = NULL;
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
901 clear_weakref(current);
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
902 if (callback != NULL) {
47738
84590a64f3a2 add some casts and fix the build from 64311
Benjamin Peterson <benjamin@python.org>
parents: 47734
diff changeset
903 if (((PyObject *)current)->ob_refcnt > 0)
47734
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
904 handle_callback(current, callback);
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
905 Py_DECREF(callback);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
906 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
907 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
908 else {
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
909 PyObject *tuple;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36244
diff changeset
910 Py_ssize_t i = 0;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57348
diff changeset
911
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
912 tuple = PyTuple_New(count * 2);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
913 if (tuple == NULL) {
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
914 if (restore_error)
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
915 PyErr_Fetch(&err_type, &err_value, &err_tb);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
916 return;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
917 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
918
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
919 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
920 PyWeakReference *next = current->wr_next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
921
47738
84590a64f3a2 add some casts and fix the build from 64311
Benjamin Peterson <benjamin@python.org>
parents: 47734
diff changeset
922 if (((PyObject *)current)->ob_refcnt > 0)
47734
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
923 {
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
924 Py_INCREF(current);
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
925 PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current);
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
926 PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback);
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
927 }
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
928 else {
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
929 Py_DECREF(current->wr_callback);
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
930 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
931 current->wr_callback = NULL;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
932 clear_weakref(current);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
933 current = next;
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
934 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
935 for (i = 0; i < count; ++i) {
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
936 PyObject *callback = PyTuple_GET_ITEM(tuple, i * 2 + 1);
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
937
47734
5c85d55c4fb2 Merged revisions 64309 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47620
diff changeset
938 /* The tuple may have slots left to NULL */
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
939 if (callback != NULL) {
36244
e7880f4f53a4 Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
940 PyObject *item = PyTuple_GET_ITEM(tuple, i * 2);
e7880f4f53a4 Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
941 handle_callback((PyWeakReference *)item, callback);
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 31454
diff changeset
942 }
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
943 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
944 Py_DECREF(tuple);
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
945 }
21339
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
946 if (restore_error)
525c77368cd0 Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents: 21083
diff changeset
947 PyErr_Restore(err_type, err_value, err_tb);
20280
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
948 }
97cfa461d793 The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
949 }