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