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