Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "structmember.h" |
| 3 | |
| 4 | |
| 5 | #define GET_WEAKREFS_LISTPTR(o) \ |
| 6 | ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o)) |
| 7 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 8 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9 | Py_ssize_t |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 10 | _PyWeakref_GetWeakrefCount(PyWeakReference *head) |
| 11 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12 | Py_ssize_t count = 0; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 13 | |
| 14 | while (head != NULL) { |
| 15 | ++count; |
| 16 | head = head->wr_next; |
| 17 | } |
| 18 | return count; |
| 19 | } |
| 20 | |
| 21 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 22 | static void |
| 23 | init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback) |
| 24 | { |
| 25 | self->hash = -1; |
| 26 | self->wr_object = ob; |
| 27 | Py_XINCREF(callback); |
| 28 | self->wr_callback = callback; |
| 29 | } |
| 30 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 31 | static PyWeakReference * |
Neil Schemenauer | 38a8916 | 2002-03-27 15:18:21 +0000 | [diff] [blame] | 32 | new_weakref(PyObject *ob, PyObject *callback) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 33 | { |
| 34 | PyWeakReference *result; |
| 35 | |
Neil Schemenauer | 38a8916 | 2002-03-27 15:18:21 +0000 | [diff] [blame] | 36 | result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType); |
| 37 | if (result) { |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 38 | init_weakref(result, ob, callback); |
Neil Schemenauer | 38a8916 | 2002-03-27 15:18:21 +0000 | [diff] [blame] | 39 | PyObject_GC_Track(result); |
| 40 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 41 | return result; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /* This function clears the passed-in reference and removes it from the |
| 46 | * list of weak references for the referent. This is the only code that |
| 47 | * removes an item from the doubly-linked list of weak references for an |
| 48 | * object; it is also responsible for clearing the callback slot. |
| 49 | */ |
| 50 | static void |
| 51 | clear_weakref(PyWeakReference *self) |
| 52 | { |
| 53 | PyObject *callback = self->wr_callback; |
| 54 | |
| 55 | if (PyWeakref_GET_OBJECT(self) != Py_None) { |
| 56 | PyWeakReference **list = GET_WEAKREFS_LISTPTR( |
| 57 | PyWeakref_GET_OBJECT(self)); |
| 58 | |
| 59 | if (*list == self) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 60 | /* If 'self' is the end of the list (and thus self->wr_next == NULL) |
| 61 | then the weakref list itself (and thus the value of *list) will |
| 62 | end up being set to NULL. */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 63 | *list = self->wr_next; |
| 64 | self->wr_object = Py_None; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 65 | if (self->wr_prev != NULL) |
| 66 | self->wr_prev->wr_next = self->wr_next; |
| 67 | if (self->wr_next != NULL) |
| 68 | self->wr_next->wr_prev = self->wr_prev; |
| 69 | self->wr_prev = NULL; |
| 70 | self->wr_next = NULL; |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 71 | } |
| 72 | if (callback != NULL) { |
| 73 | Py_DECREF(callback); |
| 74 | self->wr_callback = NULL; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 78 | /* Cyclic gc uses this to *just* clear the passed-in reference, leaving |
| 79 | * the callback intact and uncalled. It must be possible to call self's |
| 80 | * tp_dealloc() after calling this, so self has to be left in a sane enough |
| 81 | * state for that to work. We expect tp_dealloc to decref the callback |
| 82 | * then. The reason for not letting clear_weakref() decref the callback |
| 83 | * right now is that if the callback goes away, that may in turn trigger |
| 84 | * another callback (if a weak reference to the callback exists) -- running |
| 85 | * arbitrary Python code in the middle of gc is a disaster. The convolution |
| 86 | * here allows gc to delay triggering such callbacks until the world is in |
| 87 | * a sane state again. |
| 88 | */ |
| 89 | void |
| 90 | _PyWeakref_ClearRef(PyWeakReference *self) |
| 91 | { |
| 92 | PyObject *callback; |
| 93 | |
| 94 | assert(self != NULL); |
| 95 | assert(PyWeakref_Check(self)); |
| 96 | /* Preserve and restore the callback around clear_weakref. */ |
| 97 | callback = self->wr_callback; |
| 98 | self->wr_callback = NULL; |
| 99 | clear_weakref(self); |
| 100 | self->wr_callback = callback; |
| 101 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 102 | |
| 103 | static void |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 104 | weakref_dealloc(PyObject *self) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 105 | { |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 106 | PyObject_GC_UnTrack(self); |
| 107 | clear_weakref((PyWeakReference *) self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 108 | Py_TYPE(self)->tp_free(self); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | static int |
| 113 | gc_traverse(PyWeakReference *self, visitproc visit, void *arg) |
| 114 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 115 | Py_VISIT(self->wr_callback); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | static int |
| 121 | gc_clear(PyWeakReference *self) |
| 122 | { |
| 123 | clear_weakref(self); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static PyObject * |
| 129 | weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw) |
| 130 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 131 | static char *kwlist[] = {NULL}; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 132 | |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 133 | if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", kwlist)) { |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 134 | PyObject *object = PyWeakref_GET_OBJECT(self); |
| 135 | Py_INCREF(object); |
| 136 | return (object); |
| 137 | } |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static long |
| 143 | weakref_hash(PyWeakReference *self) |
| 144 | { |
| 145 | if (self->hash != -1) |
| 146 | return self->hash; |
| 147 | if (PyWeakref_GET_OBJECT(self) == Py_None) { |
| 148 | PyErr_SetString(PyExc_TypeError, "weak object has gone away"); |
| 149 | return -1; |
| 150 | } |
| 151 | self->hash = PyObject_Hash(PyWeakref_GET_OBJECT(self)); |
| 152 | return self->hash; |
| 153 | } |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 154 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 155 | |
| 156 | static PyObject * |
| 157 | weakref_repr(PyWeakReference *self) |
| 158 | { |
| 159 | char buffer[256]; |
| 160 | if (PyWeakref_GET_OBJECT(self) == Py_None) { |
Guido van Rossum | c1f6e8c | 2003-04-16 21:13:23 +0000 | [diff] [blame] | 161 | PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 162 | } |
| 163 | else { |
Guido van Rossum | c1f6e8c | 2003-04-16 21:13:23 +0000 | [diff] [blame] | 164 | char *name = NULL; |
| 165 | PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self), |
| 166 | "__name__"); |
| 167 | if (nameobj == NULL) |
| 168 | PyErr_Clear(); |
Neal Norwitz | f616b22 | 2007-08-25 08:13:40 +0000 | [diff] [blame] | 169 | else if (PyUnicode_Check(nameobj)) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame^] | 170 | name = _PyUnicode_AsString(nameobj); |
Barry Warsaw | d586756 | 2001-11-28 21:01:56 +0000 | [diff] [blame] | 171 | PyOS_snprintf(buffer, sizeof(buffer), |
Guido van Rossum | c1f6e8c | 2003-04-16 21:13:23 +0000 | [diff] [blame] | 172 | name ? "<weakref at %p; to '%.50s' at %p (%s)>" |
| 173 | : "<weakref at %p; to '%.50s' at %p>", |
| 174 | self, |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 175 | Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name, |
Guido van Rossum | c1f6e8c | 2003-04-16 21:13:23 +0000 | [diff] [blame] | 176 | PyWeakref_GET_OBJECT(self), |
| 177 | name); |
| 178 | Py_XDECREF(nameobj); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 179 | } |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 180 | return PyUnicode_FromString(buffer); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Weak references only support equality, not ordering. Two weak references |
| 184 | are equal if the underlying objects are equal. If the underlying object has |
| 185 | gone away, they are equal if they are identical. */ |
| 186 | |
| 187 | static PyObject * |
| 188 | weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op) |
| 189 | { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 190 | if ((op != Py_EQ && op != Py_NE) || |
| 191 | !PyWeakref_Check(self) || |
| 192 | !PyWeakref_Check(other)) { |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 193 | Py_INCREF(Py_NotImplemented); |
| 194 | return Py_NotImplemented; |
| 195 | } |
| 196 | if (PyWeakref_GET_OBJECT(self) == Py_None |
| 197 | || PyWeakref_GET_OBJECT(other) == Py_None) { |
| 198 | PyObject *res = self==other ? Py_True : Py_False; |
| 199 | Py_INCREF(res); |
| 200 | return res; |
| 201 | } |
| 202 | return PyObject_RichCompare(PyWeakref_GET_OBJECT(self), |
| 203 | PyWeakref_GET_OBJECT(other), op); |
| 204 | } |
| 205 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 206 | /* Given the head of an object's list of weak references, extract the |
| 207 | * two callback-less refs (ref and proxy). Used to determine if the |
| 208 | * shared references exist and to determine the back link for newly |
| 209 | * inserted references. |
| 210 | */ |
| 211 | static void |
| 212 | get_basic_refs(PyWeakReference *head, |
| 213 | PyWeakReference **refp, PyWeakReference **proxyp) |
| 214 | { |
| 215 | *refp = NULL; |
| 216 | *proxyp = NULL; |
| 217 | |
| 218 | if (head != NULL && head->wr_callback == NULL) { |
| 219 | /* We need to be careful that the "basic refs" aren't |
| 220 | subclasses of the main types. That complicates this a |
| 221 | little. */ |
| 222 | if (PyWeakref_CheckRefExact(head)) { |
| 223 | *refp = head; |
| 224 | head = head->wr_next; |
| 225 | } |
| 226 | if (head != NULL |
| 227 | && head->wr_callback == NULL |
| 228 | && PyWeakref_CheckProxy(head)) { |
| 229 | *proxyp = head; |
| 230 | /* head = head->wr_next; */ |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* Insert 'newref' in the list after 'prev'. Both must be non-NULL. */ |
| 236 | static void |
| 237 | insert_after(PyWeakReference *newref, PyWeakReference *prev) |
| 238 | { |
| 239 | newref->wr_prev = prev; |
| 240 | newref->wr_next = prev->wr_next; |
| 241 | if (prev->wr_next != NULL) |
| 242 | prev->wr_next->wr_prev = newref; |
| 243 | prev->wr_next = newref; |
| 244 | } |
| 245 | |
| 246 | /* Insert 'newref' at the head of the list; 'list' points to the variable |
| 247 | * that stores the head. |
| 248 | */ |
| 249 | static void |
| 250 | insert_head(PyWeakReference *newref, PyWeakReference **list) |
| 251 | { |
| 252 | PyWeakReference *next = *list; |
| 253 | |
| 254 | newref->wr_prev = NULL; |
| 255 | newref->wr_next = next; |
| 256 | if (next != NULL) |
| 257 | next->wr_prev = newref; |
| 258 | *list = newref; |
| 259 | } |
| 260 | |
| 261 | static int |
| 262 | parse_weakref_init_args(char *funcname, PyObject *args, PyObject *kwargs, |
| 263 | PyObject **obp, PyObject **callbackp) |
| 264 | { |
| 265 | /* XXX Should check that kwargs == NULL or is empty. */ |
| 266 | return PyArg_UnpackTuple(args, funcname, 1, 2, obp, callbackp); |
| 267 | } |
| 268 | |
| 269 | static PyObject * |
| 270 | weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 271 | { |
| 272 | PyWeakReference *self = NULL; |
| 273 | PyObject *ob, *callback = NULL; |
| 274 | |
| 275 | if (parse_weakref_init_args("__new__", args, kwargs, &ob, &callback)) { |
| 276 | PyWeakReference *ref, *proxy; |
| 277 | PyWeakReference **list; |
| 278 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 279 | if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 280 | PyErr_Format(PyExc_TypeError, |
| 281 | "cannot create weak reference to '%s' object", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 282 | Py_TYPE(ob)->tp_name); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 283 | return NULL; |
| 284 | } |
| 285 | if (callback == Py_None) |
| 286 | callback = NULL; |
| 287 | list = GET_WEAKREFS_LISTPTR(ob); |
| 288 | get_basic_refs(*list, &ref, &proxy); |
| 289 | if (callback == NULL && type == &_PyWeakref_RefType) { |
| 290 | if (ref != NULL) { |
| 291 | /* We can re-use an existing reference. */ |
| 292 | Py_INCREF(ref); |
| 293 | return (PyObject *)ref; |
| 294 | } |
| 295 | } |
| 296 | /* We have to create a new reference. */ |
| 297 | /* Note: the tp_alloc() can trigger cyclic GC, so the weakref |
| 298 | list on ob can be mutated. This means that the ref and |
| 299 | proxy pointers we got back earlier may have been collected, |
| 300 | so we need to compute these values again before we use |
| 301 | them. */ |
| 302 | self = (PyWeakReference *) (type->tp_alloc(type, 0)); |
| 303 | if (self != NULL) { |
| 304 | init_weakref(self, ob, callback); |
| 305 | if (callback == NULL && type == &_PyWeakref_RefType) { |
| 306 | insert_head(self, list); |
| 307 | } |
| 308 | else { |
| 309 | PyWeakReference *prev; |
| 310 | |
| 311 | get_basic_refs(*list, &ref, &proxy); |
| 312 | prev = (proxy == NULL) ? ref : proxy; |
| 313 | if (prev == NULL) |
| 314 | insert_head(self, list); |
| 315 | else |
| 316 | insert_after(self, prev); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | return (PyObject *)self; |
| 321 | } |
| 322 | |
| 323 | static int |
| 324 | weakref___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
| 325 | { |
| 326 | PyObject *tmp; |
| 327 | |
| 328 | if (parse_weakref_init_args("__init__", args, kwargs, &tmp, &tmp)) |
| 329 | return 0; |
| 330 | else |
| 331 | return 1; |
| 332 | } |
| 333 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 334 | |
| 335 | PyTypeObject |
| 336 | _PyWeakref_RefType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 337 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 338 | "weakref", |
| 339 | sizeof(PyWeakReference), |
| 340 | 0, |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 341 | weakref_dealloc, /*tp_dealloc*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 342 | 0, /*tp_print*/ |
| 343 | 0, /*tp_getattr*/ |
| 344 | 0, /*tp_setattr*/ |
| 345 | 0, /*tp_compare*/ |
| 346 | (reprfunc)weakref_repr, /*tp_repr*/ |
| 347 | 0, /*tp_as_number*/ |
| 348 | 0, /*tp_as_sequence*/ |
| 349 | 0, /*tp_as_mapping*/ |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 350 | (hashfunc)weakref_hash, /*tp_hash*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 351 | (ternaryfunc)weakref_call, /*tp_call*/ |
| 352 | 0, /*tp_str*/ |
| 353 | 0, /*tp_getattro*/ |
| 354 | 0, /*tp_setattro*/ |
| 355 | 0, /*tp_as_buffer*/ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 356 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 357 | | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 358 | 0, /*tp_doc*/ |
| 359 | (traverseproc)gc_traverse, /*tp_traverse*/ |
| 360 | (inquiry)gc_clear, /*tp_clear*/ |
| 361 | (richcmpfunc)weakref_richcompare, /*tp_richcompare*/ |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 362 | 0, /*tp_weaklistoffset*/ |
| 363 | 0, /*tp_iter*/ |
| 364 | 0, /*tp_iternext*/ |
| 365 | 0, /*tp_methods*/ |
| 366 | 0, /*tp_members*/ |
| 367 | 0, /*tp_getset*/ |
| 368 | 0, /*tp_base*/ |
| 369 | 0, /*tp_dict*/ |
| 370 | 0, /*tp_descr_get*/ |
| 371 | 0, /*tp_descr_set*/ |
| 372 | 0, /*tp_dictoffset*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 373 | weakref___init__, /*tp_init*/ |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 374 | PyType_GenericAlloc, /*tp_alloc*/ |
| 375 | weakref___new__, /*tp_new*/ |
| 376 | PyObject_GC_Del, /*tp_free*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | |
| 380 | static int |
| 381 | proxy_checkref(PyWeakReference *proxy) |
| 382 | { |
| 383 | if (PyWeakref_GET_OBJECT(proxy) == Py_None) { |
| 384 | PyErr_SetString(PyExc_ReferenceError, |
| 385 | "weakly-referenced object no longer exists"); |
| 386 | return 0; |
| 387 | } |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | |
Fred Drake | 73006d0 | 2001-10-18 18:04:18 +0000 | [diff] [blame] | 392 | /* If a parameter is a proxy, check that it is still "live" and wrap it, |
| 393 | * replacing the original value with the raw object. Raises ReferenceError |
| 394 | * if the param is a dead proxy. |
| 395 | */ |
| 396 | #define UNWRAP(o) \ |
| 397 | if (PyWeakref_CheckProxy(o)) { \ |
| 398 | if (!proxy_checkref((PyWeakReference *)o)) \ |
| 399 | return NULL; \ |
| 400 | o = PyWeakref_GET_OBJECT(o); \ |
| 401 | } |
| 402 | |
Fred Drake | 2a908f6 | 2001-12-19 16:44:30 +0000 | [diff] [blame] | 403 | #define UNWRAP_I(o) \ |
| 404 | if (PyWeakref_CheckProxy(o)) { \ |
| 405 | if (!proxy_checkref((PyWeakReference *)o)) \ |
| 406 | return -1; \ |
| 407 | o = PyWeakref_GET_OBJECT(o); \ |
| 408 | } |
| 409 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 410 | #define WRAP_UNARY(method, generic) \ |
| 411 | static PyObject * \ |
Fred Drake | 73006d0 | 2001-10-18 18:04:18 +0000 | [diff] [blame] | 412 | method(PyObject *proxy) { \ |
| 413 | UNWRAP(proxy); \ |
| 414 | return generic(proxy); \ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | #define WRAP_BINARY(method, generic) \ |
| 418 | static PyObject * \ |
Fred Drake | 73006d0 | 2001-10-18 18:04:18 +0000 | [diff] [blame] | 419 | method(PyObject *x, PyObject *y) { \ |
| 420 | UNWRAP(x); \ |
| 421 | UNWRAP(y); \ |
| 422 | return generic(x, y); \ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Fred Drake | 31f4d1f | 2001-10-18 19:21:46 +0000 | [diff] [blame] | 425 | /* Note that the third arg needs to be checked for NULL since the tp_call |
| 426 | * slot can receive NULL for this arg. |
Fred Drake | 73006d0 | 2001-10-18 18:04:18 +0000 | [diff] [blame] | 427 | */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 428 | #define WRAP_TERNARY(method, generic) \ |
| 429 | static PyObject * \ |
Fred Drake | 73006d0 | 2001-10-18 18:04:18 +0000 | [diff] [blame] | 430 | method(PyObject *proxy, PyObject *v, PyObject *w) { \ |
| 431 | UNWRAP(proxy); \ |
Fred Drake | 31f4d1f | 2001-10-18 19:21:46 +0000 | [diff] [blame] | 432 | UNWRAP(v); \ |
Fred Drake | 73006d0 | 2001-10-18 18:04:18 +0000 | [diff] [blame] | 433 | if (w != NULL) \ |
| 434 | UNWRAP(w); \ |
| 435 | return generic(proxy, v, w); \ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
| 439 | /* direct slots */ |
| 440 | |
| 441 | WRAP_BINARY(proxy_getattr, PyObject_GetAttr) |
| 442 | WRAP_UNARY(proxy_str, PyObject_Str) |
| 443 | WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords) |
| 444 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 445 | static PyObject * |
| 446 | proxy_repr(PyWeakReference *proxy) |
| 447 | { |
| 448 | char buf[160]; |
Barry Warsaw | d586756 | 2001-11-28 21:01:56 +0000 | [diff] [blame] | 449 | PyOS_snprintf(buf, sizeof(buf), |
Guido van Rossum | c1f6e8c | 2003-04-16 21:13:23 +0000 | [diff] [blame] | 450 | "<weakproxy at %p to %.100s at %p>", proxy, |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 451 | Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name, |
Barry Warsaw | d586756 | 2001-11-28 21:01:56 +0000 | [diff] [blame] | 452 | PyWeakref_GET_OBJECT(proxy)); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 453 | return PyUnicode_FromString(buf); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | static int |
| 458 | proxy_setattr(PyWeakReference *proxy, PyObject *name, PyObject *value) |
| 459 | { |
| 460 | if (!proxy_checkref(proxy)) |
| 461 | return -1; |
| 462 | return PyObject_SetAttr(PyWeakref_GET_OBJECT(proxy), name, value); |
| 463 | } |
| 464 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 465 | static PyObject * |
| 466 | proxy_richcompare(PyObject *proxy, PyObject *v, int op) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 467 | { |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 468 | UNWRAP(proxy); |
| 469 | UNWRAP(v); |
| 470 | return PyObject_RichCompare(proxy, v, op); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* number slots */ |
| 474 | WRAP_BINARY(proxy_add, PyNumber_Add) |
| 475 | WRAP_BINARY(proxy_sub, PyNumber_Subtract) |
| 476 | WRAP_BINARY(proxy_mul, PyNumber_Multiply) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 477 | WRAP_BINARY(proxy_floor_div, PyNumber_FloorDivide) |
| 478 | WRAP_BINARY(proxy_true_div, PyNumber_TrueDivide) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 479 | WRAP_BINARY(proxy_mod, PyNumber_Remainder) |
| 480 | WRAP_BINARY(proxy_divmod, PyNumber_Divmod) |
| 481 | WRAP_TERNARY(proxy_pow, PyNumber_Power) |
| 482 | WRAP_UNARY(proxy_neg, PyNumber_Negative) |
| 483 | WRAP_UNARY(proxy_pos, PyNumber_Positive) |
| 484 | WRAP_UNARY(proxy_abs, PyNumber_Absolute) |
| 485 | WRAP_UNARY(proxy_invert, PyNumber_Invert) |
| 486 | WRAP_BINARY(proxy_lshift, PyNumber_Lshift) |
| 487 | WRAP_BINARY(proxy_rshift, PyNumber_Rshift) |
| 488 | WRAP_BINARY(proxy_and, PyNumber_And) |
| 489 | WRAP_BINARY(proxy_xor, PyNumber_Xor) |
| 490 | WRAP_BINARY(proxy_or, PyNumber_Or) |
| 491 | WRAP_UNARY(proxy_int, PyNumber_Int) |
| 492 | WRAP_UNARY(proxy_long, PyNumber_Long) |
| 493 | WRAP_UNARY(proxy_float, PyNumber_Float) |
| 494 | WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd) |
| 495 | WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract) |
| 496 | WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 497 | WRAP_BINARY(proxy_ifloor_div, PyNumber_InPlaceFloorDivide) |
| 498 | WRAP_BINARY(proxy_itrue_div, PyNumber_InPlaceTrueDivide) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 499 | WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder) |
| 500 | WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower) |
| 501 | WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift) |
| 502 | WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift) |
| 503 | WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd) |
| 504 | WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor) |
| 505 | WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 506 | WRAP_UNARY(proxy_index, PyNumber_Index) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 507 | |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 508 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 509 | proxy_bool(PyWeakReference *proxy) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 510 | { |
| 511 | PyObject *o = PyWeakref_GET_OBJECT(proxy); |
| 512 | if (!proxy_checkref(proxy)) |
Neal Norwitz | bdcb941 | 2004-07-08 01:22:31 +0000 | [diff] [blame] | 513 | return -1; |
Raymond Hettinger | e6c470f | 2005-03-27 03:04:54 +0000 | [diff] [blame] | 514 | return PyObject_IsTrue(o); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 517 | static void |
| 518 | proxy_dealloc(PyWeakReference *self) |
| 519 | { |
| 520 | if (self->wr_callback != NULL) |
| 521 | PyObject_GC_UnTrack((PyObject *)self); |
| 522 | clear_weakref(self); |
| 523 | PyObject_GC_Del(self); |
| 524 | } |
| 525 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 526 | /* sequence slots */ |
| 527 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 528 | static int |
| 529 | proxy_contains(PyWeakReference *proxy, PyObject *value) |
| 530 | { |
| 531 | if (!proxy_checkref(proxy)) |
| 532 | return -1; |
| 533 | return PySequence_Contains(PyWeakref_GET_OBJECT(proxy), value); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | /* mapping slots */ |
| 538 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 539 | static Py_ssize_t |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 540 | proxy_length(PyWeakReference *proxy) |
| 541 | { |
| 542 | if (!proxy_checkref(proxy)) |
| 543 | return -1; |
| 544 | return PyObject_Length(PyWeakref_GET_OBJECT(proxy)); |
| 545 | } |
| 546 | |
| 547 | WRAP_BINARY(proxy_getitem, PyObject_GetItem) |
| 548 | |
| 549 | static int |
| 550 | proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value) |
| 551 | { |
| 552 | if (!proxy_checkref(proxy)) |
| 553 | return -1; |
Raymond Hettinger | d693a81 | 2003-06-30 04:18:48 +0000 | [diff] [blame] | 554 | |
| 555 | if (value == NULL) |
| 556 | return PyObject_DelItem(PyWeakref_GET_OBJECT(proxy), key); |
| 557 | else |
| 558 | return PyObject_SetItem(PyWeakref_GET_OBJECT(proxy), key, value); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 561 | /* iterator slots */ |
| 562 | |
| 563 | static PyObject * |
| 564 | proxy_iter(PyWeakReference *proxy) |
| 565 | { |
| 566 | if (!proxy_checkref(proxy)) |
| 567 | return NULL; |
| 568 | return PyObject_GetIter(PyWeakref_GET_OBJECT(proxy)); |
| 569 | } |
| 570 | |
| 571 | static PyObject * |
| 572 | proxy_iternext(PyWeakReference *proxy) |
| 573 | { |
| 574 | if (!proxy_checkref(proxy)) |
| 575 | return NULL; |
| 576 | return PyIter_Next(PyWeakref_GET_OBJECT(proxy)); |
| 577 | } |
| 578 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 579 | |
| 580 | static PyNumberMethods proxy_as_number = { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 581 | proxy_add, /*nb_add*/ |
| 582 | proxy_sub, /*nb_subtract*/ |
| 583 | proxy_mul, /*nb_multiply*/ |
| 584 | proxy_mod, /*nb_remainder*/ |
| 585 | proxy_divmod, /*nb_divmod*/ |
| 586 | proxy_pow, /*nb_power*/ |
| 587 | proxy_neg, /*nb_negative*/ |
| 588 | proxy_pos, /*nb_positive*/ |
| 589 | proxy_abs, /*nb_absolute*/ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 590 | (inquiry)proxy_bool, /*nb_bool*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 591 | proxy_invert, /*nb_invert*/ |
| 592 | proxy_lshift, /*nb_lshift*/ |
| 593 | proxy_rshift, /*nb_rshift*/ |
| 594 | proxy_and, /*nb_and*/ |
| 595 | proxy_xor, /*nb_xor*/ |
| 596 | proxy_or, /*nb_or*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 597 | proxy_int, /*nb_int*/ |
| 598 | proxy_long, /*nb_long*/ |
| 599 | proxy_float, /*nb_float*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 600 | proxy_iadd, /*nb_inplace_add*/ |
| 601 | proxy_isub, /*nb_inplace_subtract*/ |
| 602 | proxy_imul, /*nb_inplace_multiply*/ |
| 603 | proxy_imod, /*nb_inplace_remainder*/ |
| 604 | proxy_ipow, /*nb_inplace_power*/ |
| 605 | proxy_ilshift, /*nb_inplace_lshift*/ |
| 606 | proxy_irshift, /*nb_inplace_rshift*/ |
| 607 | proxy_iand, /*nb_inplace_and*/ |
| 608 | proxy_ixor, /*nb_inplace_xor*/ |
| 609 | proxy_ior, /*nb_inplace_or*/ |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 610 | proxy_floor_div, /*nb_floor_divide*/ |
| 611 | proxy_true_div, /*nb_true_divide*/ |
| 612 | proxy_ifloor_div, /*nb_inplace_floor_divide*/ |
| 613 | proxy_itrue_div, /*nb_inplace_true_divide*/ |
| 614 | proxy_index, /*nb_index*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 615 | }; |
| 616 | |
| 617 | static PySequenceMethods proxy_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 618 | (lenfunc)proxy_length, /*sq_length*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 619 | 0, /*sq_concat*/ |
| 620 | 0, /*sq_repeat*/ |
| 621 | 0, /*sq_item*/ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 622 | 0, /*sq_slice*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 623 | 0, /*sq_ass_item*/ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 624 | 0, /*sq_ass_slice*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 625 | (objobjproc)proxy_contains, /* sq_contains */ |
| 626 | }; |
| 627 | |
| 628 | static PyMappingMethods proxy_as_mapping = { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 629 | (lenfunc)proxy_length, /*mp_length*/ |
| 630 | proxy_getitem, /*mp_subscript*/ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 631 | (objobjargproc)proxy_setitem, /*mp_ass_subscript*/ |
| 632 | }; |
| 633 | |
| 634 | |
| 635 | PyTypeObject |
| 636 | _PyWeakref_ProxyType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 637 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 638 | "weakproxy", |
| 639 | sizeof(PyWeakReference), |
| 640 | 0, |
| 641 | /* methods */ |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 642 | (destructor)proxy_dealloc, /* tp_dealloc */ |
Fred Drake | fe89cc1 | 2003-07-14 21:46:23 +0000 | [diff] [blame] | 643 | 0, /* tp_print */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 644 | 0, /* tp_getattr */ |
| 645 | 0, /* tp_setattr */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 646 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 647 | (reprfunc)proxy_repr, /* tp_repr */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 648 | &proxy_as_number, /* tp_as_number */ |
| 649 | &proxy_as_sequence, /* tp_as_sequence */ |
| 650 | &proxy_as_mapping, /* tp_as_mapping */ |
| 651 | 0, /* tp_hash */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 652 | 0, /* tp_call */ |
| 653 | proxy_str, /* tp_str */ |
| 654 | proxy_getattr, /* tp_getattro */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 655 | (setattrofunc)proxy_setattr, /* tp_setattro */ |
| 656 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 657 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 658 | 0, /* tp_doc */ |
| 659 | (traverseproc)gc_traverse, /* tp_traverse */ |
| 660 | (inquiry)gc_clear, /* tp_clear */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 661 | proxy_richcompare, /* tp_richcompare */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 662 | 0, /* tp_weaklistoffset */ |
| 663 | (getiterfunc)proxy_iter, /* tp_iter */ |
| 664 | (iternextfunc)proxy_iternext, /* tp_iternext */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 665 | }; |
| 666 | |
| 667 | |
| 668 | PyTypeObject |
| 669 | _PyWeakref_CallableProxyType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 670 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 671 | "weakcallableproxy", |
| 672 | sizeof(PyWeakReference), |
| 673 | 0, |
| 674 | /* methods */ |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 675 | (destructor)proxy_dealloc, /* tp_dealloc */ |
Fred Drake | fe89cc1 | 2003-07-14 21:46:23 +0000 | [diff] [blame] | 676 | 0, /* tp_print */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 677 | 0, /* tp_getattr */ |
| 678 | 0, /* tp_setattr */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 679 | 0, /* tp_compare */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 680 | (unaryfunc)proxy_repr, /* tp_repr */ |
| 681 | &proxy_as_number, /* tp_as_number */ |
| 682 | &proxy_as_sequence, /* tp_as_sequence */ |
| 683 | &proxy_as_mapping, /* tp_as_mapping */ |
| 684 | 0, /* tp_hash */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 685 | proxy_call, /* tp_call */ |
| 686 | proxy_str, /* tp_str */ |
| 687 | proxy_getattr, /* tp_getattro */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 688 | (setattrofunc)proxy_setattr, /* tp_setattro */ |
| 689 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 690 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 691 | 0, /* tp_doc */ |
| 692 | (traverseproc)gc_traverse, /* tp_traverse */ |
| 693 | (inquiry)gc_clear, /* tp_clear */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 694 | proxy_richcompare, /* tp_richcompare */ |
Fred Drake | f16c3dc | 2002-08-09 18:34:16 +0000 | [diff] [blame] | 695 | 0, /* tp_weaklistoffset */ |
| 696 | (getiterfunc)proxy_iter, /* tp_iter */ |
| 697 | (iternextfunc)proxy_iternext, /* tp_iternext */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 698 | }; |
| 699 | |
| 700 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 701 | |
| 702 | PyObject * |
| 703 | PyWeakref_NewRef(PyObject *ob, PyObject *callback) |
| 704 | { |
| 705 | PyWeakReference *result = NULL; |
| 706 | PyWeakReference **list; |
| 707 | PyWeakReference *ref, *proxy; |
| 708 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 709 | if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 710 | PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | 996fad3 | 2001-10-22 16:31:40 +0000 | [diff] [blame] | 711 | "cannot create weak reference to '%s' object", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 712 | Py_TYPE(ob)->tp_name); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 713 | return NULL; |
| 714 | } |
| 715 | list = GET_WEAKREFS_LISTPTR(ob); |
| 716 | get_basic_refs(*list, &ref, &proxy); |
Fred Drake | 6a2852c | 2004-02-03 19:52:56 +0000 | [diff] [blame] | 717 | if (callback == Py_None) |
| 718 | callback = NULL; |
| 719 | if (callback == NULL) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 720 | /* return existing weak reference if it exists */ |
| 721 | result = ref; |
| 722 | if (result != NULL) |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 723 | Py_INCREF(result); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 724 | else { |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 725 | /* Note: new_weakref() can trigger cyclic GC, so the weakref |
| 726 | list on ob can be mutated. This means that the ref and |
| 727 | proxy pointers we got back earlier may have been collected, |
| 728 | so we need to compute these values again before we use |
| 729 | them. */ |
Neil Schemenauer | 38a8916 | 2002-03-27 15:18:21 +0000 | [diff] [blame] | 730 | result = new_weakref(ob, callback); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 731 | if (result != NULL) { |
Fred Drake | 6d3265d | 2004-08-03 14:47:25 +0000 | [diff] [blame] | 732 | get_basic_refs(*list, &ref, &proxy); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 733 | if (callback == NULL) { |
Fred Drake | 6d3265d | 2004-08-03 14:47:25 +0000 | [diff] [blame] | 734 | if (ref == NULL) |
| 735 | insert_head(result, list); |
| 736 | else { |
| 737 | /* Someone else added a ref without a callback |
| 738 | during GC. Return that one instead of this one |
| 739 | to avoid violating the invariants of the list |
| 740 | of weakrefs for ob. */ |
| 741 | Py_DECREF(result); |
| 742 | Py_INCREF(ref); |
| 743 | result = ref; |
| 744 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 745 | } |
| 746 | else { |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 747 | PyWeakReference *prev; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 748 | |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 749 | prev = (proxy == NULL) ? ref : proxy; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 750 | if (prev == NULL) |
| 751 | insert_head(result, list); |
| 752 | else |
| 753 | insert_after(result, prev); |
| 754 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | return (PyObject *) result; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | PyObject * |
| 762 | PyWeakref_NewProxy(PyObject *ob, PyObject *callback) |
| 763 | { |
| 764 | PyWeakReference *result = NULL; |
| 765 | PyWeakReference **list; |
| 766 | PyWeakReference *ref, *proxy; |
| 767 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 768 | if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) { |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 769 | PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | 996fad3 | 2001-10-22 16:31:40 +0000 | [diff] [blame] | 770 | "cannot create weak reference to '%s' object", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 771 | Py_TYPE(ob)->tp_name); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 772 | return NULL; |
| 773 | } |
| 774 | list = GET_WEAKREFS_LISTPTR(ob); |
| 775 | get_basic_refs(*list, &ref, &proxy); |
Fred Drake | 6a2852c | 2004-02-03 19:52:56 +0000 | [diff] [blame] | 776 | if (callback == Py_None) |
| 777 | callback = NULL; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 778 | if (callback == NULL) |
| 779 | /* attempt to return an existing weak reference if it exists */ |
| 780 | result = proxy; |
| 781 | if (result != NULL) |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 782 | Py_INCREF(result); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 783 | else { |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 784 | /* Note: new_weakref() can trigger cyclic GC, so the weakref |
| 785 | list on ob can be mutated. This means that the ref and |
| 786 | proxy pointers we got back earlier may have been collected, |
| 787 | so we need to compute these values again before we use |
| 788 | them. */ |
Neil Schemenauer | 38a8916 | 2002-03-27 15:18:21 +0000 | [diff] [blame] | 789 | result = new_weakref(ob, callback); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 790 | if (result != NULL) { |
| 791 | PyWeakReference *prev; |
| 792 | |
| 793 | if (PyCallable_Check(ob)) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 794 | Py_TYPE(result) = &_PyWeakref_CallableProxyType; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 795 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 796 | Py_TYPE(result) = &_PyWeakref_ProxyType; |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 797 | get_basic_refs(*list, &ref, &proxy); |
Fred Drake | 6d3265d | 2004-08-03 14:47:25 +0000 | [diff] [blame] | 798 | if (callback == NULL) { |
| 799 | if (proxy != NULL) { |
| 800 | /* Someone else added a proxy without a callback |
| 801 | during GC. Return that one instead of this one |
| 802 | to avoid violating the invariants of the list |
| 803 | of weakrefs for ob. */ |
| 804 | Py_DECREF(result); |
| 805 | Py_INCREF(result = proxy); |
| 806 | goto skip_insert; |
| 807 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 808 | prev = ref; |
Fred Drake | 6d3265d | 2004-08-03 14:47:25 +0000 | [diff] [blame] | 809 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 810 | else |
| 811 | prev = (proxy == NULL) ? ref : proxy; |
| 812 | |
| 813 | if (prev == NULL) |
| 814 | insert_head(result, list); |
| 815 | else |
| 816 | insert_after(result, prev); |
Fred Drake | 6d3265d | 2004-08-03 14:47:25 +0000 | [diff] [blame] | 817 | skip_insert: |
| 818 | ; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | return (PyObject *) result; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | PyObject * |
| 826 | PyWeakref_GetObject(PyObject *ref) |
| 827 | { |
| 828 | if (ref == NULL || !PyWeakref_Check(ref)) { |
| 829 | PyErr_BadInternalCall(); |
| 830 | return NULL; |
| 831 | } |
| 832 | return PyWeakref_GET_OBJECT(ref); |
| 833 | } |
| 834 | |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 835 | /* Note that there's an inlined copy-paste of handle_callback() in gcmodule.c's |
| 836 | * handle_weakrefs(). |
| 837 | */ |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 838 | static void |
| 839 | handle_callback(PyWeakReference *ref, PyObject *callback) |
| 840 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 841 | PyObject *cbresult = PyObject_CallFunctionObjArgs(callback, ref, NULL); |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 842 | |
| 843 | if (cbresult == NULL) |
| 844 | PyErr_WriteUnraisable(callback); |
| 845 | else |
| 846 | Py_DECREF(cbresult); |
| 847 | } |
| 848 | |
| 849 | /* This function is called by the tp_dealloc handler to clear weak references. |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 850 | * |
| 851 | * This iterates through the weak references for 'object' and calls callbacks |
| 852 | * for those references which have one. It returns when all callbacks have |
| 853 | * been attempted. |
| 854 | */ |
| 855 | void |
| 856 | PyObject_ClearWeakRefs(PyObject *object) |
| 857 | { |
| 858 | PyWeakReference **list; |
| 859 | |
| 860 | if (object == NULL |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 861 | || !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 862 | || object->ob_refcnt != 0) { |
| 863 | PyErr_BadInternalCall(); |
| 864 | return; |
| 865 | } |
| 866 | list = GET_WEAKREFS_LISTPTR(object); |
| 867 | /* Remove the callback-less basic and proxy references */ |
| 868 | if (*list != NULL && (*list)->wr_callback == NULL) { |
| 869 | clear_weakref(*list); |
| 870 | if (*list != NULL && (*list)->wr_callback == NULL) |
| 871 | clear_weakref(*list); |
| 872 | } |
| 873 | if (*list != NULL) { |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 874 | PyWeakReference *current = *list; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 875 | Py_ssize_t count = _PyWeakref_GetWeakrefCount(current); |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 876 | int restore_error = PyErr_Occurred() ? 1 : 0; |
| 877 | PyObject *err_type, *err_value, *err_tb; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 878 | |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 879 | if (restore_error) |
| 880 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 881 | if (count == 1) { |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 882 | PyObject *callback = current->wr_callback; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 883 | |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 884 | current->wr_callback = NULL; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 885 | clear_weakref(current); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 886 | if (callback != NULL) { |
Benjamin Peterson | a38d156 | 2008-06-16 20:47:12 +0000 | [diff] [blame] | 887 | if (((PyObject *)current)->ob_refcnt > 0) |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 888 | handle_callback(current, callback); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 889 | Py_DECREF(callback); |
| 890 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 891 | } |
| 892 | else { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 893 | PyObject *tuple; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 894 | Py_ssize_t i = 0; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 895 | |
| 896 | tuple = PyTuple_New(count * 2); |
| 897 | if (tuple == NULL) { |
| 898 | if (restore_error) |
| 899 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
| 900 | return; |
| 901 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 902 | |
| 903 | for (i = 0; i < count; ++i) { |
| 904 | PyWeakReference *next = current->wr_next; |
| 905 | |
Benjamin Peterson | a38d156 | 2008-06-16 20:47:12 +0000 | [diff] [blame] | 906 | if (((PyObject *)current)->ob_refcnt > 0) |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 907 | { |
| 908 | Py_INCREF(current); |
| 909 | PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current); |
| 910 | PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback); |
| 911 | } |
| 912 | else { |
| 913 | Py_DECREF(current->wr_callback); |
| 914 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 915 | current->wr_callback = NULL; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 916 | clear_weakref(current); |
| 917 | current = next; |
| 918 | } |
| 919 | for (i = 0; i < count; ++i) { |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 920 | PyObject *callback = PyTuple_GET_ITEM(tuple, i * 2 + 1); |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 921 | |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 922 | /* The tuple may have slots left to NULL */ |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 923 | if (callback != NULL) { |
Neal Norwitz | 0c6e2f1 | 2006-01-08 06:13:44 +0000 | [diff] [blame] | 924 | PyObject *item = PyTuple_GET_ITEM(tuple, i * 2); |
| 925 | handle_callback((PyWeakReference *)item, callback); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 926 | } |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 927 | } |
| 928 | Py_DECREF(tuple); |
| 929 | } |
Fred Drake | ef8ebd1 | 2001-12-10 23:44:54 +0000 | [diff] [blame] | 930 | if (restore_error) |
| 931 | PyErr_Restore(err_type, err_value, err_tb); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 932 | } |
| 933 | } |