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