Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 38aefc5 | 2020-04-06 14:07:02 +0200 | [diff] [blame] | 2 | #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | #define GET_WEAKREFS_LISTPTR(o) \ |
Victor Stinner | 38aefc5 | 2020-04-06 14:07:02 +0200 | [diff] [blame] | 6 | ((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 7 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 8 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 9 | module _weakref |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 10 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 11 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 12 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 13 | #include "clinic/_weakref.c.h" |
| 14 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 15 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 16 | |
| 17 | _weakref.getweakrefcount -> Py_ssize_t |
| 18 | |
| 19 | object: object |
| 20 | / |
| 21 | |
| 22 | Return the number of weak references to 'object'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 23 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 24 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 25 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 26 | _weakref_getweakrefcount_impl(PyObject *module, PyObject *object) |
| 27 | /*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 28 | { |
| 29 | PyWeakReference **list; |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 30 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 31 | if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) |
| 32 | return 0; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 33 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 34 | list = GET_WEAKREFS_LISTPTR(object); |
| 35 | return _PyWeakref_GetWeakrefCount(*list); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 39 | static int |
| 40 | is_dead_weakref(PyObject *value) |
| 41 | { |
| 42 | if (!PyWeakref_Check(value)) { |
| 43 | PyErr_SetString(PyExc_TypeError, "not a weakref"); |
| 44 | return -1; |
| 45 | } |
| 46 | return PyWeakref_GET_OBJECT(value) == Py_None; |
| 47 | } |
| 48 | |
| 49 | /*[clinic input] |
| 50 | |
| 51 | _weakref._remove_dead_weakref -> object |
| 52 | |
| 53 | dct: object(subclass_of='&PyDict_Type') |
| 54 | key: object |
| 55 | / |
| 56 | |
| 57 | Atomically remove key from dict if it points to a dead weakref. |
| 58 | [clinic start generated code]*/ |
| 59 | |
| 60 | static PyObject * |
| 61 | _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct, |
| 62 | PyObject *key) |
| 63 | /*[clinic end generated code: output=d9ff53061fcb875c input=19fc91f257f96a1d]*/ |
| 64 | { |
| 65 | if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) { |
| 66 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 67 | /* This function is meant to allow safe weak-value dicts |
| 68 | with GC in another thread (see issue #28427), so it's |
| 69 | ok if the key doesn't exist anymore. |
| 70 | */ |
| 71 | PyErr_Clear(); |
| 72 | else |
| 73 | return NULL; |
| 74 | } |
| 75 | Py_RETURN_NONE; |
| 76 | } |
| 77 | |
| 78 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 79 | PyDoc_STRVAR(weakref_getweakrefs__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 80 | "getweakrefs(object) -- return a list of all weak reference objects\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 81 | "that point to 'object'."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 82 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 83 | static PyObject * |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 84 | weakref_getweakrefs(PyObject *self, PyObject *object) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 85 | { |
| 86 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 87 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 88 | if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 89 | PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 90 | Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 91 | |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 92 | result = PyList_New(count); |
| 93 | if (result != NULL) { |
| 94 | PyWeakReference *current = *list; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 95 | Py_ssize_t i; |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 96 | for (i = 0; i < count; ++i) { |
| 97 | PyList_SET_ITEM(result, i, (PyObject *) current); |
| 98 | Py_INCREF(current); |
| 99 | current = current->wr_next; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 102 | } |
| 103 | else { |
| 104 | result = PyList_New(0); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 105 | } |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 110 | PyDoc_STRVAR(weakref_proxy__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 111 | "proxy(object[, callback]) -- create a proxy object that weakly\n" |
| 112 | "references 'object'. 'callback', if given, is called with a\n" |
Fred Drake | 610291c | 2002-08-02 20:23:40 +0000 | [diff] [blame] | 113 | "reference to the proxy when 'object' is about to be finalized."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 114 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 115 | static PyObject * |
| 116 | weakref_proxy(PyObject *self, PyObject *args) |
| 117 | { |
| 118 | PyObject *object; |
| 119 | PyObject *callback = NULL; |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 120 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 121 | |
Fred Drake | 0d429e8 | 2001-10-23 21:12:47 +0000 | [diff] [blame] | 122 | if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 123 | result = PyWeakref_NewProxy(object, callback); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 124 | } |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 125 | return result; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
| 129 | static PyMethodDef |
| 130 | weakref_functions[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 131 | _WEAKREF_GETWEAKREFCOUNT_METHODDEF |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 132 | _WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 133 | {"getweakrefs", weakref_getweakrefs, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 134 | weakref_getweakrefs__doc__}, |
| 135 | {"proxy", weakref_proxy, METH_VARARGS, |
| 136 | weakref_proxy__doc__}, |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 137 | {NULL, NULL, 0, NULL} |
| 138 | }; |
| 139 | |
Victor Stinner | 93460d0 | 2020-03-24 18:31:19 +0100 | [diff] [blame] | 140 | static int |
| 141 | weakref_exec(PyObject *module) |
| 142 | { |
| 143 | Py_INCREF(&_PyWeakref_RefType); |
| 144 | if (PyModule_AddObject(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) { |
| 145 | Py_DECREF(&_PyWeakref_RefType); |
| 146 | return -1; |
| 147 | } |
| 148 | Py_INCREF(&_PyWeakref_RefType); |
| 149 | if (PyModule_AddObject(module, "ReferenceType", |
| 150 | (PyObject *) &_PyWeakref_RefType) < 0) { |
| 151 | Py_DECREF(&_PyWeakref_RefType); |
| 152 | return -1; |
| 153 | } |
| 154 | Py_INCREF(&_PyWeakref_ProxyType); |
| 155 | if (PyModule_AddObject(module, "ProxyType", |
| 156 | (PyObject *) &_PyWeakref_ProxyType) < 0) { |
| 157 | Py_DECREF(&_PyWeakref_ProxyType); |
| 158 | return -1; |
| 159 | } |
| 160 | Py_INCREF(&_PyWeakref_CallableProxyType); |
| 161 | if (PyModule_AddObject(module, "CallableProxyType", |
| 162 | (PyObject *) &_PyWeakref_CallableProxyType) < 0) { |
| 163 | Py_DECREF(&_PyWeakref_CallableProxyType); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static struct PyModuleDef_Slot weakref_slots[] = { |
| 171 | {Py_mod_exec, weakref_exec}, |
| 172 | {0, NULL} |
| 173 | }; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 174 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 175 | static struct PyModuleDef weakrefmodule = { |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 176 | PyModuleDef_HEAD_INIT, |
| 177 | "_weakref", |
| 178 | "Weak-reference support module.", |
Victor Stinner | 93460d0 | 2020-03-24 18:31:19 +0100 | [diff] [blame] | 179 | 0, |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 180 | weakref_functions, |
Victor Stinner | 93460d0 | 2020-03-24 18:31:19 +0100 | [diff] [blame] | 181 | weakref_slots, |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 182 | NULL, |
| 183 | NULL, |
| 184 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 187 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 188 | PyInit__weakref(void) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 189 | { |
Victor Stinner | 93460d0 | 2020-03-24 18:31:19 +0100 | [diff] [blame] | 190 | return PyModuleDef_Init(&weakrefmodule); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 191 | } |