Mercurial > cpython-lru_cache-weakref
annotate Objects/weakrefobject.c @ 82251:bb5a8564e186 2.7
Issue #13153: Tkinter functions now raise TclError instead of ValueError when
a unicode argument contains non-BMP character.
| author | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| date | Mon, 18 Feb 2013 13:00:08 +0200 |
| parents | 59acde449f8d |
| children |
| 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 |
|
39351
12ffed927a3d
_PyWeakref_GetWeakrefCount() now returns a Py_ssize_t instead of long.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38314
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 { |
|
39351
12ffed927a3d
_PyWeakref_GetWeakrefCount() now returns a Py_ssize_t instead of long.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38314
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 |
|
80762
7e771f0363e2
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
80400
diff
changeset
|
55 if (self->wr_object != Py_None) { |
|
7e771f0363e2
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Antoine Pitrou <solipsis@pitrou.net>
parents:
80400
diff
changeset
|
56 PyWeakReference **list = GET_WEAKREFS_LISTPTR(self->wr_object); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 if (*list == self) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
59 /* If 'self' is the end of the list (and thus self->wr_next == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
60 then the weakref list itself (and thus the value of *list) will |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
61 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
|
62 *list = self->wr_next; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 self->wr_object = Py_None; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 if (self->wr_prev != NULL) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 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
|
66 if (self->wr_next != NULL) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 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
|
68 self->wr_prev = NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 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
|
70 } |
|
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
71 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
|
72 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
|
73 self->wr_callback = NULL; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 |
|
30991
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
77 /* 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
|
78 * 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
|
79 * 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
|
80 * 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
|
81 * 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
|
82 * 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
|
83 * 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
|
84 * 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
|
85 * 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
|
86 * 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
|
87 */ |
|
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
88 void |
|
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
89 _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
|
90 { |
|
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
91 PyObject *callback; |
|
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
92 |
|
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
93 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
|
94 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
|
95 /* 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 static void |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
103 weakref_dealloc(PyObject *self) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 { |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
105 PyObject_GC_UnTrack(self); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
106 clear_weakref((PyWeakReference *) self); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
107 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
|
108 } |
|
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 static int |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
112 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
|
113 { |
|
37756
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37304
diff
changeset
|
114 Py_VISIT(self->wr_callback); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 return 0; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 static int |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
120 gc_clear(PyWeakReference *self) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 clear_weakref(self); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 return 0; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
126 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
127 static PyObject * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
128 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
|
129 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
130 static char *kwlist[] = {NULL}; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
132 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
|
133 PyObject *object = PyWeakref_GET_OBJECT(self); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 Py_INCREF(object); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
135 return (object); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 return NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 static long |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 weakref_hash(PyWeakReference *self) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
143 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
144 if (self->hash != -1) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 return self->hash; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 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
|
147 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
|
148 return -1; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 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
|
151 return self->hash; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 } |
|
30991
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
153 |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
154 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 static PyObject * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 weakref_repr(PyWeakReference *self) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 char buffer[256]; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 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
|
160 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
|
161 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 else { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
163 char *name = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
164 PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
165 "__name__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
166 if (nameobj == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
167 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
168 else if (PyString_Check(nameobj)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
169 name = PyString_AS_STRING(nameobj); |
|
81893
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
170 if (name != NULL) { |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
171 PyOS_snprintf(buffer, sizeof(buffer), |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
172 "<weakref at %p; to '%.50s' at %p (%s)>", |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
173 self, |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
174 Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name, |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
175 PyWeakref_GET_OBJECT(self), |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
176 name); |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
177 } |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
178 else { |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
179 PyOS_snprintf(buffer, sizeof(buffer), |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
180 "<weakref at %p; to '%.50s' at %p>", |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
181 self, |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
182 Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name, |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
183 PyWeakref_GET_OBJECT(self)); |
|
59acde449f8d
Silence a -Wformat-extra-argument warning when compiling.
Gregory P. Smith <greg@krypto.org>
parents:
80762
diff
changeset
|
184 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
185 Py_XDECREF(nameobj); |
|
20280
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 return PyString_FromString(buffer); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
188 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
189 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
190 /* 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
|
191 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
|
192 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
|
193 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
194 static PyObject * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
195 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
|
196 { |
|
80400
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
197 if ((op != Py_EQ && op != Py_NE) || self->ob_type != other->ob_type) { |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
198 Py_INCREF(Py_NotImplemented); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
199 return Py_NotImplemented; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
200 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
201 if (PyWeakref_GET_OBJECT(self) == Py_None |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
202 || PyWeakref_GET_OBJECT(other) == Py_None) { |
|
80400
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
203 int res = (self == other); |
|
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
204 if (op == Py_NE) |
|
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
205 res = !res; |
|
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
206 if (res) |
|
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
207 Py_RETURN_TRUE; |
|
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
208 else |
|
13b74c0b040c
Issue #16453: Fix equality testing of dead weakref objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68255
diff
changeset
|
209 Py_RETURN_FALSE; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
210 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
211 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
|
212 PyWeakref_GET_OBJECT(other), op); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
213 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
214 |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
215 /* 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
|
216 * 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
|
217 * 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
|
218 * inserted references. |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
219 */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
220 static void |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
221 get_basic_refs(PyWeakReference *head, |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
222 PyWeakReference **refp, PyWeakReference **proxyp) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
223 { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
224 *refp = NULL; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
225 *proxyp = NULL; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
226 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
227 if (head != NULL && head->wr_callback == NULL) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
228 /* 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
|
229 subclasses of the main types. That complicates this a |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
230 little. */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
231 if (PyWeakref_CheckRefExact(head)) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
232 *refp = head; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
233 head = head->wr_next; |
|
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 if (head != NULL |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
236 && head->wr_callback == NULL |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
237 && PyWeakref_CheckProxy(head)) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
238 *proxyp = head; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
239 /* head = head->wr_next; */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
240 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
241 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
242 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
243 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
244 /* 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
|
245 static void |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
246 insert_after(PyWeakReference *newref, PyWeakReference *prev) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
247 { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
248 newref->wr_prev = prev; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
249 newref->wr_next = prev->wr_next; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
250 if (prev->wr_next != NULL) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
251 prev->wr_next->wr_prev = newref; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
252 prev->wr_next = newref; |
|
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 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
255 /* 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
|
256 * that stores the head. |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
257 */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
258 static void |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
259 insert_head(PyWeakReference *newref, PyWeakReference **list) |
|
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 PyWeakReference *next = *list; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
262 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
263 newref->wr_prev = NULL; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
264 newref->wr_next = next; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
265 if (next != NULL) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
266 next->wr_prev = newref; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
267 *list = newref; |
|
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 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
270 static int |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
271 parse_weakref_init_args(char *funcname, PyObject *args, PyObject *kwargs, |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
272 PyObject **obp, PyObject **callbackp) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
273 { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
274 /* XXX Should check that kwargs == NULL or is empty. */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
275 return PyArg_UnpackTuple(args, funcname, 1, 2, obp, callbackp); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
276 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
277 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
278 static PyObject * |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
279 weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
280 { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
281 PyWeakReference *self = NULL; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
282 PyObject *ob, *callback = NULL; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
283 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
284 if (parse_weakref_init_args("__new__", args, kwargs, &ob, &callback)) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
285 PyWeakReference *ref, *proxy; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
286 PyWeakReference **list; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
287 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
288 if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
289 PyErr_Format(PyExc_TypeError, |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
290 "cannot create weak reference to '%s' object", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
291 Py_TYPE(ob)->tp_name); |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
292 return NULL; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
293 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
294 if (callback == Py_None) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
295 callback = NULL; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
296 list = GET_WEAKREFS_LISTPTR(ob); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
297 get_basic_refs(*list, &ref, &proxy); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
298 if (callback == NULL && type == &_PyWeakref_RefType) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
299 if (ref != NULL) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
300 /* We can re-use an existing reference. */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
301 Py_INCREF(ref); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
302 return (PyObject *)ref; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
303 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
304 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
305 /* We have to create a new reference. */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
306 /* 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
|
307 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
|
308 proxy pointers we got back earlier may have been collected, |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
309 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
|
310 them. */ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
311 self = (PyWeakReference *) (type->tp_alloc(type, 0)); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
312 if (self != NULL) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
313 init_weakref(self, ob, callback); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
314 if (callback == NULL && type == &_PyWeakref_RefType) { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
315 insert_head(self, list); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
316 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
317 else { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
318 PyWeakReference *prev; |
|
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 get_basic_refs(*list, &ref, &proxy); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
321 prev = (proxy == NULL) ? ref : proxy; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
322 if (prev == NULL) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
323 insert_head(self, list); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
324 else |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
325 insert_after(self, prev); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
326 } |
|
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 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
329 return (PyObject *)self; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
330 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
331 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
332 static int |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
333 weakref___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
334 { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
335 PyObject *tmp; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
336 |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
337 if (parse_weakref_init_args("__init__", args, kwargs, &tmp, &tmp)) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
338 return 0; |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
339 else |
|
49120
73cc7b6355b8
Fix #3634 invalid return value from _weakref.ref(Exception).__init__
Benjamin Peterson <benjamin@python.org>
parents:
47732
diff
changeset
|
340 return -1; |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
341 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
342 |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
343 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
344 PyTypeObject |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
345 _PyWeakref_RefType = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
40707
diff
changeset
|
346 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
|
347 "weakref", |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
348 sizeof(PyWeakReference), |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
349 0, |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
350 weakref_dealloc, /*tp_dealloc*/ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
351 0, /*tp_print*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
352 0, /*tp_getattr*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
353 0, /*tp_setattr*/ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
354 0, /*tp_compare*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
355 (reprfunc)weakref_repr, /*tp_repr*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
356 0, /*tp_as_number*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
357 0, /*tp_as_sequence*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
358 0, /*tp_as_mapping*/ |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
359 (hashfunc)weakref_hash, /*tp_hash*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
360 (ternaryfunc)weakref_call, /*tp_call*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
361 0, /*tp_str*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
362 0, /*tp_getattro*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
363 0, /*tp_setattro*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
364 0, /*tp_as_buffer*/ |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
365 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_RICHCOMPARE |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
366 | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
367 0, /*tp_doc*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
368 (traverseproc)gc_traverse, /*tp_traverse*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
369 (inquiry)gc_clear, /*tp_clear*/ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
370 (richcmpfunc)weakref_richcompare, /*tp_richcompare*/ |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
371 0, /*tp_weaklistoffset*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
372 0, /*tp_iter*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
373 0, /*tp_iternext*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
374 0, /*tp_methods*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
375 0, /*tp_members*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
376 0, /*tp_getset*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
377 0, /*tp_base*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
378 0, /*tp_dict*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
379 0, /*tp_descr_get*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
380 0, /*tp_descr_set*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
381 0, /*tp_dictoffset*/ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
382 weakref___init__, /*tp_init*/ |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
383 PyType_GenericAlloc, /*tp_alloc*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
384 weakref___new__, /*tp_new*/ |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
385 PyObject_GC_Del, /*tp_free*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
386 }; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
387 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
388 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
389 static int |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
390 proxy_checkref(PyWeakReference *proxy) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
391 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
392 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
|
393 PyErr_SetString(PyExc_ReferenceError, |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
394 "weakly-referenced object no longer exists"); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
395 return 0; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
396 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
397 return 1; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
398 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
399 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
400 |
|
20521
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
401 /* 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
|
402 * 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
|
403 * 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
|
404 */ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
405 #define UNWRAP(o) \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
406 if (PyWeakref_CheckProxy(o)) { \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
407 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
|
408 return NULL; \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
409 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
|
410 } |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
411 |
|
21483
873291a4889e
proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents:
21339
diff
changeset
|
412 #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
|
413 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
|
414 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
|
415 return -1; \ |
|
873291a4889e
proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents:
21339
diff
changeset
|
416 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
|
417 } |
|
873291a4889e
proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents:
21339
diff
changeset
|
418 |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
419 #define WRAP_UNARY(method, generic) \ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
420 static PyObject * \ |
|
20521
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
421 method(PyObject *proxy) { \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
422 UNWRAP(proxy); \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
423 return generic(proxy); \ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
424 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
425 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
426 #define WRAP_BINARY(method, generic) \ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
427 static PyObject * \ |
|
20521
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
428 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
|
429 UNWRAP(x); \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
430 UNWRAP(y); \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
431 return generic(x, y); \ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
432 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
433 |
|
20532
bc138bc73cdd
Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents:
20521
diff
changeset
|
434 /* 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
|
435 * 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
|
436 */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
437 #define WRAP_TERNARY(method, generic) \ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
438 static PyObject * \ |
|
20521
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
439 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
|
440 UNWRAP(proxy); \ |
|
20532
bc138bc73cdd
Remove an unnecessary check for NULL.
Fred Drake <fdrake@acm.org>
parents:
20521
diff
changeset
|
441 UNWRAP(v); \ |
|
20521
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
442 if (w != NULL) \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
443 UNWRAP(w); \ |
|
cb87bd97f740
When weakref proxies are involved in binary & ternary slot operations,
Fred Drake <fdrake@acm.org>
parents:
20280
diff
changeset
|
444 return generic(proxy, v, w); \ |
|
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 |
|
57349
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
447 #define WRAP_METHOD(method, special) \ |
|
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
448 static PyObject * \ |
|
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
449 method(PyObject *proxy) { \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
450 UNWRAP(proxy); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
451 return PyObject_CallMethod(proxy, special, ""); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
452 } |
|
57349
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
453 |
|
20280
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 /* direct slots */ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
456 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
457 WRAP_BINARY(proxy_getattr, PyObject_GetAttr) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
458 WRAP_UNARY(proxy_str, PyObject_Str) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
459 WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
460 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
461 static PyObject * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
462 proxy_repr(PyWeakReference *proxy) |
|
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 char buf[160]; |
|
21083
d3b6401ca273
weakref_repr(), proxy_repr(): Conversion of sprintf() to
Barry Warsaw <barry@python.org>
parents:
21066
diff
changeset
|
465 PyOS_snprintf(buf, sizeof(buf), |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
466 "<weakproxy at %p to %.100s at %p>", proxy, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
467 Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
468 PyWeakref_GET_OBJECT(proxy)); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
469 return PyString_FromString(buf); |
|
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 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
472 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
473 static int |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
474 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
|
475 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
476 if (!proxy_checkref(proxy)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
477 return -1; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
478 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
|
479 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
480 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
481 static int |
|
21483
873291a4889e
proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents:
21339
diff
changeset
|
482 proxy_compare(PyObject *proxy, PyObject *v) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
483 { |
|
21483
873291a4889e
proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents:
21339
diff
changeset
|
484 UNWRAP_I(proxy); |
|
873291a4889e
proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents:
21339
diff
changeset
|
485 UNWRAP_I(v); |
|
873291a4889e
proxy_compare(): Make sure that we unwrap both objects being compared if
Fred Drake <fdrake@acm.org>
parents:
21339
diff
changeset
|
486 return PyObject_Compare(proxy, v); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
487 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
488 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
489 /* number slots */ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
490 WRAP_BINARY(proxy_add, PyNumber_Add) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
491 WRAP_BINARY(proxy_sub, PyNumber_Subtract) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
492 WRAP_BINARY(proxy_mul, PyNumber_Multiply) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
493 WRAP_BINARY(proxy_div, PyNumber_Divide) |
|
47196
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
494 WRAP_BINARY(proxy_floor_div, PyNumber_FloorDivide) |
|
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
495 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
|
496 WRAP_BINARY(proxy_mod, PyNumber_Remainder) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
497 WRAP_BINARY(proxy_divmod, PyNumber_Divmod) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
498 WRAP_TERNARY(proxy_pow, PyNumber_Power) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
499 WRAP_UNARY(proxy_neg, PyNumber_Negative) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
500 WRAP_UNARY(proxy_pos, PyNumber_Positive) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
501 WRAP_UNARY(proxy_abs, PyNumber_Absolute) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
502 WRAP_UNARY(proxy_invert, PyNumber_Invert) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
503 WRAP_BINARY(proxy_lshift, PyNumber_Lshift) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
504 WRAP_BINARY(proxy_rshift, PyNumber_Rshift) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
505 WRAP_BINARY(proxy_and, PyNumber_And) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
506 WRAP_BINARY(proxy_xor, PyNumber_Xor) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
507 WRAP_BINARY(proxy_or, PyNumber_Or) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
508 WRAP_UNARY(proxy_int, PyNumber_Int) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
509 WRAP_UNARY(proxy_long, PyNumber_Long) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
510 WRAP_UNARY(proxy_float, PyNumber_Float) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
511 WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
512 WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
513 WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
514 WRAP_BINARY(proxy_idiv, PyNumber_InPlaceDivide) |
|
47196
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
515 WRAP_BINARY(proxy_ifloor_div, PyNumber_InPlaceFloorDivide) |
|
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
516 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
|
517 WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
518 WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
519 WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
520 WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
521 WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
522 WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
523 WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr) |
|
47196
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
524 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
|
525 |
|
30991
14d321466a06
SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents:
29863
diff
changeset
|
526 static int |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
527 proxy_nonzero(PyWeakReference *proxy) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
528 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
529 PyObject *o = PyWeakref_GET_OBJECT(proxy); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
530 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
|
531 return -1; |
|
34847
51ba8ce8d92a
SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior.
Raymond Hettinger <python@rcn.com>
parents:
33862
diff
changeset
|
532 return PyObject_IsTrue(o); |
|
20280
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 |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
535 static void |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
536 proxy_dealloc(PyWeakReference *self) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
537 { |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
538 if (self->wr_callback != NULL) |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
539 PyObject_GC_UnTrack((PyObject *)self); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
540 clear_weakref(self); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
541 PyObject_GC_Del(self); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
542 } |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
543 |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
544 /* sequence slots */ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
545 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
546 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
547 proxy_slice(PyWeakReference *proxy, Py_ssize_t i, Py_ssize_t j) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
548 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
549 if (!proxy_checkref(proxy)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
550 return NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
551 return PySequence_GetSlice(PyWeakref_GET_OBJECT(proxy), i, j); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
552 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
553 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
554 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
555 proxy_ass_slice(PyWeakReference *proxy, Py_ssize_t i, Py_ssize_t j, PyObject *value) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
556 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
557 if (!proxy_checkref(proxy)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
558 return -1; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
559 return PySequence_SetSlice(PyWeakref_GET_OBJECT(proxy), i, j, value); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
560 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
561 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
562 static int |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
563 proxy_contains(PyWeakReference *proxy, PyObject *value) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
564 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
565 if (!proxy_checkref(proxy)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
566 return -1; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
567 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
|
568 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
569 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
570 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
571 /* mapping slots */ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
572 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
573 static Py_ssize_t |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
574 proxy_length(PyWeakReference *proxy) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
575 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
576 if (!proxy_checkref(proxy)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
577 return -1; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
578 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
|
579 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
580 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
581 WRAP_BINARY(proxy_getitem, PyObject_GetItem) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
582 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
583 static int |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
584 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
|
585 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
586 if (!proxy_checkref(proxy)) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
587 return -1; |
|
29697
1d763862d1a1
Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents:
28644
diff
changeset
|
588 |
|
1d763862d1a1
Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents:
28644
diff
changeset
|
589 if (value == NULL) |
|
1d763862d1a1
Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents:
28644
diff
changeset
|
590 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
|
591 else |
|
1d763862d1a1
Fix SF 762891: "del p[key]" on proxy object raises SystemError()
Raymond Hettinger <python@rcn.com>
parents:
28644
diff
changeset
|
592 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
|
593 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
594 |
|
24717
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
595 /* iterator slots */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
596 |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
597 static PyObject * |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
598 proxy_iter(PyWeakReference *proxy) |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
599 { |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
600 if (!proxy_checkref(proxy)) |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
601 return NULL; |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
602 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
|
603 } |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
604 |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
605 static PyObject * |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
606 proxy_iternext(PyWeakReference *proxy) |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
607 { |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
608 if (!proxy_checkref(proxy)) |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
609 return NULL; |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
610 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
|
611 } |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
612 |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
613 |
|
57349
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
614 WRAP_METHOD(proxy_unicode, "__unicode__"); |
|
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
615 |
|
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
616 |
|
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
617 static PyMethodDef proxy_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
618 {"__unicode__", (PyCFunction)proxy_unicode, METH_NOARGS}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
619 {NULL, NULL} |
|
57349
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
620 }; |
|
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
621 |
|
d5779e159ddb
Merged revisions 76395 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49120
diff
changeset
|
622 |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
623 static PyNumberMethods proxy_as_number = { |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
624 proxy_add, /*nb_add*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
625 proxy_sub, /*nb_subtract*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
626 proxy_mul, /*nb_multiply*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
627 proxy_div, /*nb_divide*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
628 proxy_mod, /*nb_remainder*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
629 proxy_divmod, /*nb_divmod*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
630 proxy_pow, /*nb_power*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
631 proxy_neg, /*nb_negative*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
632 proxy_pos, /*nb_positive*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
633 proxy_abs, /*nb_absolute*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
634 (inquiry)proxy_nonzero, /*nb_nonzero*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
635 proxy_invert, /*nb_invert*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
636 proxy_lshift, /*nb_lshift*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
637 proxy_rshift, /*nb_rshift*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
638 proxy_and, /*nb_and*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
639 proxy_xor, /*nb_xor*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
640 proxy_or, /*nb_or*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
641 0, /*nb_coerce*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
642 proxy_int, /*nb_int*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
643 proxy_long, /*nb_long*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
644 proxy_float, /*nb_float*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
645 0, /*nb_oct*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
646 0, /*nb_hex*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
647 proxy_iadd, /*nb_inplace_add*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
648 proxy_isub, /*nb_inplace_subtract*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
649 proxy_imul, /*nb_inplace_multiply*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
650 proxy_idiv, /*nb_inplace_divide*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
651 proxy_imod, /*nb_inplace_remainder*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
652 proxy_ipow, /*nb_inplace_power*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
653 proxy_ilshift, /*nb_inplace_lshift*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
654 proxy_irshift, /*nb_inplace_rshift*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
655 proxy_iand, /*nb_inplace_and*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
656 proxy_ixor, /*nb_inplace_xor*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
657 proxy_ior, /*nb_inplace_or*/ |
|
47196
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
658 proxy_floor_div, /*nb_floor_divide*/ |
|
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
659 proxy_true_div, /*nb_true_divide*/ |
|
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
660 proxy_ifloor_div, /*nb_inplace_floor_divide*/ |
|
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
661 proxy_itrue_div, /*nb_inplace_true_divide*/ |
|
12c56d2eb4d9
#2592: delegate nb_index and the floor/truediv slots in weakref.proxy.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
662 proxy_index, /*nb_index*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
663 }; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
664 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
665 static PySequenceMethods proxy_as_sequence = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
666 (lenfunc)proxy_length, /*sq_length*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
667 0, /*sq_concat*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
668 0, /*sq_repeat*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
669 0, /*sq_item*/ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
670 (ssizessizeargfunc)proxy_slice, /*sq_slice*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
671 0, /*sq_ass_item*/ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
672 (ssizessizeobjargproc)proxy_ass_slice, /*sq_ass_slice*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
673 (objobjproc)proxy_contains, /* sq_contains */ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
674 }; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
675 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
676 static PyMappingMethods proxy_as_mapping = { |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
677 (lenfunc)proxy_length, /*mp_length*/ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
678 proxy_getitem, /*mp_subscript*/ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
679 (objobjargproc)proxy_setitem, /*mp_ass_subscript*/ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
680 }; |
|
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 PyTypeObject |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
684 _PyWeakref_ProxyType = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
40707
diff
changeset
|
685 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
|
686 "weakproxy", |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
687 sizeof(PyWeakReference), |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
688 0, |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
689 /* methods */ |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
690 (destructor)proxy_dealloc, /* tp_dealloc */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
691 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
692 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
693 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
694 proxy_compare, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
695 (reprfunc)proxy_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
696 &proxy_as_number, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
697 &proxy_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
698 &proxy_as_mapping, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
699 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
700 0, /* tp_call */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
701 proxy_str, /* tp_str */ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
702 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
|
703 (setattrofunc)proxy_setattr, /* tp_setattro */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
704 0, /* tp_as_buffer */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
705 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
24717
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
706 | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
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 */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
710 0, /* tp_richcompare */ |
|
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 */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
714 proxy_methods, /* tp_methods */ |
|
20280
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 PyTypeObject |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
719 _PyWeakref_CallableProxyType = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
40707
diff
changeset
|
720 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
|
721 "weakcallableproxy", |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
722 sizeof(PyWeakReference), |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
723 0, |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
724 /* methods */ |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
725 (destructor)proxy_dealloc, /* tp_dealloc */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
726 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
727 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
728 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
729 proxy_compare, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
730 (unaryfunc)proxy_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
731 &proxy_as_number, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
732 &proxy_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
733 &proxy_as_mapping, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
734 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
735 proxy_call, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
736 proxy_str, /* tp_str */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36877
diff
changeset
|
737 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
|
738 (setattrofunc)proxy_setattr, /* tp_setattro */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
739 0, /* tp_as_buffer */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
740 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
24717
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
741 | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
742 0, /* tp_doc */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
743 (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
|
744 (inquiry)gc_clear, /* tp_clear */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
745 0, /* tp_richcompare */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
746 0, /* tp_weaklistoffset */ |
|
1d3d980a3186
Add support for the iterator protocol to weakref proxy objects.
Fred Drake <fdrake@acm.org>
parents:
22607
diff
changeset
|
747 (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
|
748 (iternextfunc)proxy_iternext, /* tp_iternext */ |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
749 }; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
750 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
751 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
752 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
753 PyObject * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
754 PyWeakref_NewRef(PyObject *ob, PyObject *callback) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
755 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
756 PyWeakReference *result = NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
757 PyWeakReference **list; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
758 PyWeakReference *ref, *proxy; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
759 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
760 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
|
761 PyErr_Format(PyExc_TypeError, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
762 "cannot create weak reference to '%s' object", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
763 Py_TYPE(ob)->tp_name); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
764 return NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
765 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
766 list = GET_WEAKREFS_LISTPTR(ob); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
767 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
|
768 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
|
769 callback = NULL; |
|
b4bc83f2fd04
Fix bug in interpretation of the "callback" argument in the constructors for
Fred Drake <fdrake@acm.org>
parents:
30991
diff
changeset
|
770 if (callback == NULL) |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
771 /* 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
|
772 result = ref; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
773 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
|
774 Py_INCREF(result); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
775 else { |
|
31454
d4165d12f73c
Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents:
31449
diff
changeset
|
776 /* 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
|
777 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
|
778 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
|
779 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
|
780 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
|
781 result = new_weakref(ob, callback); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
782 if (result != NULL) { |
|
32932
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
783 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
|
784 if (callback == NULL) { |
|
32932
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
785 if (ref == NULL) |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
786 insert_head(result, list); |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
787 else { |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
788 /* 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
|
789 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
|
790 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
|
791 of weakrefs for ob. */ |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
792 Py_DECREF(result); |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
793 Py_INCREF(ref); |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
794 result = ref; |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
795 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
796 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
797 else { |
|
31454
d4165d12f73c
Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents:
31449
diff
changeset
|
798 PyWeakReference *prev; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
799 |
|
31454
d4165d12f73c
Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents:
31449
diff
changeset
|
800 prev = (proxy == NULL) ? ref : proxy; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
801 if (prev == NULL) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
802 insert_head(result, list); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
803 else |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
804 insert_after(result, prev); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
805 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
806 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
807 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
808 return (PyObject *) result; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
809 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
810 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
811 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
812 PyObject * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
813 PyWeakref_NewProxy(PyObject *ob, PyObject *callback) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
814 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
815 PyWeakReference *result = NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
816 PyWeakReference **list; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
817 PyWeakReference *ref, *proxy; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
818 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
819 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
|
820 PyErr_Format(PyExc_TypeError, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
821 "cannot create weak reference to '%s' object", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
822 Py_TYPE(ob)->tp_name); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
823 return NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
824 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
825 list = GET_WEAKREFS_LISTPTR(ob); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
826 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
|
827 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
|
828 callback = NULL; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
829 if (callback == NULL) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
830 /* 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
|
831 result = proxy; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
832 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
|
833 Py_INCREF(result); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
834 else { |
|
31454
d4165d12f73c
Allocating a new weakref object can cause existing weakref objects for
Fred Drake <fdrake@acm.org>
parents:
31449
diff
changeset
|
835 /* 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
|
836 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
|
837 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
|
838 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
|
839 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
|
840 result = new_weakref(ob, callback); |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
841 if (result != NULL) { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
842 PyWeakReference *prev; |
|
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 (PyCallable_Check(ob)) |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
845 Py_TYPE(result) = &_PyWeakref_CallableProxyType; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
846 else |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
847 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
|
848 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
|
849 if (callback == NULL) { |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
850 if (proxy != NULL) { |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
851 /* 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
|
852 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
|
853 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
|
854 of weakrefs for ob. */ |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
855 Py_DECREF(result); |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
856 Py_INCREF(result = proxy); |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
857 goto skip_insert; |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
858 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
859 prev = ref; |
|
32932
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
860 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
861 else |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
862 prev = (proxy == NULL) ? ref : proxy; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
863 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
864 if (prev == NULL) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
865 insert_head(result, list); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
866 else |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
867 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
|
868 skip_insert: |
|
bd3dd6b3ff97
Be more careful about maintaining the invariants; it was actually
Fred Drake <fdrake@acm.org>
parents:
32530
diff
changeset
|
869 ; |
|
20280
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 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
872 return (PyObject *) result; |
|
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 |
|
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 PyObject * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
877 PyWeakref_GetObject(PyObject *ref) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
878 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
879 if (ref == NULL || !PyWeakref_Check(ref)) { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
880 PyErr_BadInternalCall(); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
881 return NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
882 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
883 return PyWeakref_GET_OBJECT(ref); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
884 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
885 |
|
33862
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32932
diff
changeset
|
886 /* 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
|
887 * handle_weakrefs(). |
|
facb5a2f4b53
SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents:
32932
diff
changeset
|
888 */ |
|
21339
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
889 static void |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
890 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
|
891 { |
|
38314
1fdf29a88f0e
Replace PyObject_CallFunction calls with only object args
Georg Brandl <georg@python.org>
parents:
37756
diff
changeset
|
892 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
|
893 |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
894 if (cbresult == NULL) |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
895 PyErr_WriteUnraisable(callback); |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
896 else |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
897 Py_DECREF(cbresult); |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
898 } |
|
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 /* 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
|
901 * |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
902 * 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
|
903 * 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
|
904 * been attempted. |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
905 */ |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
906 void |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
907 PyObject_ClearWeakRefs(PyObject *object) |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
908 { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
909 PyWeakReference **list; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
910 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
911 if (object == NULL |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
912 || !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
|
913 || object->ob_refcnt != 0) { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
914 PyErr_BadInternalCall(); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
915 return; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
916 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
917 list = GET_WEAKREFS_LISTPTR(object); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
918 /* 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
|
919 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
|
920 clear_weakref(*list); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
921 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
|
922 clear_weakref(*list); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
923 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
924 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
|
925 PyWeakReference *current = *list; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
926 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
|
927 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
|
928 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
|
929 |
|
21339
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
930 if (restore_error) |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
931 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
|
932 if (count == 1) { |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
933 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
|
934 |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
935 current->wr_callback = NULL; |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
936 clear_weakref(current); |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
937 if (callback != NULL) { |
|
47733
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
938 if (current->ob_refcnt > 0) |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
939 handle_callback(current, callback); |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
940 Py_DECREF(callback); |
|
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
941 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
942 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
943 else { |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
944 PyObject *tuple; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36244
diff
changeset
|
945 Py_ssize_t i = 0; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57349
diff
changeset
|
946 |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
947 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
|
948 if (tuple == NULL) { |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
949 if (restore_error) |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
950 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
|
951 return; |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
952 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
953 |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
954 for (i = 0; i < count; ++i) { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
955 PyWeakReference *next = current->wr_next; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
956 |
|
47733
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
957 if (current->ob_refcnt > 0) |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
958 { |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
959 Py_INCREF(current); |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
960 PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current); |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
961 PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback); |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
962 } |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
963 else { |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
964 Py_DECREF(current->wr_callback); |
|
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
965 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
966 current->wr_callback = NULL; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
967 clear_weakref(current); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
968 current = next; |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
969 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
970 for (i = 0; i < count; ++i) { |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
971 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
|
972 |
|
47733
4ed165940bdd
Issue 3110: Crash with weakref subclass,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
40707
diff
changeset
|
973 /* The tuple may have slots left to NULL */ |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
974 if (callback != NULL) { |
|
36244
e7880f4f53a4
Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
975 PyObject *item = PyTuple_GET_ITEM(tuple, i * 2); |
|
e7880f4f53a4
Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
976 handle_callback((PyWeakReference *)item, callback); |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
31454
diff
changeset
|
977 } |
|
20280
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
978 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
979 Py_DECREF(tuple); |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
980 } |
|
21339
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
981 if (restore_error) |
|
525c77368cd0
Make sure that when we invoke callback functions associated with weak
Fred Drake <fdrake@acm.org>
parents:
21083
diff
changeset
|
982 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
|
983 } |
|
97cfa461d793
The weak reference implementation, separated from the weakref module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
984 } |
