Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | #define GET_WEAKREFS_LISTPTR(o) \ |
| 5 | ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o)) |
| 6 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 7 | |
Antoine Pitrou | f939b3c | 2016-12-27 15:08:27 +0100 | [diff] [blame] | 8 | static int |
| 9 | is_dead_weakref(PyObject *value) |
| 10 | { |
| 11 | if (!PyWeakref_Check(value)) { |
| 12 | PyErr_SetString(PyExc_TypeError, "not a weakref"); |
| 13 | return -1; |
| 14 | } |
| 15 | return PyWeakref_GET_OBJECT(value) == Py_None; |
| 16 | } |
| 17 | |
| 18 | PyDoc_STRVAR(remove_dead_weakref__doc__, |
| 19 | "_remove_dead_weakref(dict, key) -- atomically remove key from dict\n" |
| 20 | "if it points to a dead weakref."); |
| 21 | |
| 22 | static PyObject * |
| 23 | remove_dead_weakref(PyObject *self, PyObject *args) |
| 24 | { |
| 25 | PyObject *dct, *key; |
| 26 | |
| 27 | if (!PyArg_ParseTuple(args, "O!O:_remove_dead_weakref", |
| 28 | &PyDict_Type, &dct, &key)) { |
| 29 | return NULL; |
| 30 | } |
| 31 | if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) { |
| 32 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 33 | /* This function is meant to allow safe weak-value dicts |
| 34 | with GC in another thread (see issue #28427), so it's |
| 35 | ok if the key doesn't exist anymore. |
| 36 | */ |
| 37 | PyErr_Clear(); |
| 38 | else |
| 39 | return NULL; |
| 40 | } |
| 41 | Py_RETURN_NONE; |
| 42 | } |
| 43 | |
| 44 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | PyDoc_STRVAR(weakref_getweakrefcount__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 46 | "getweakrefcount(object) -- return the number of weak references\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 47 | "to 'object'."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 48 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 49 | static PyObject * |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 50 | weakref_getweakrefcount(PyObject *self, PyObject *object) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 51 | { |
| 52 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 53 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 54 | if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 55 | PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 56 | |
Neal Norwitz | c5e060d | 2006-08-02 06:14:22 +0000 | [diff] [blame] | 57 | result = PyInt_FromSsize_t(_PyWeakref_GetWeakrefCount(*list)); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 58 | } |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 59 | else |
| 60 | result = PyInt_FromLong(0); |
| 61 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 62 | return result; |
| 63 | } |
| 64 | |
| 65 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 66 | PyDoc_STRVAR(weakref_getweakrefs__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 67 | "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] | 68 | "that point to 'object'."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 69 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 70 | static PyObject * |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 71 | weakref_getweakrefs(PyObject *self, PyObject *object) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 72 | { |
| 73 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 74 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 75 | if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 76 | PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); |
Neal Norwitz | c5e060d | 2006-08-02 06:14:22 +0000 | [diff] [blame] | 77 | Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 78 | |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 79 | result = PyList_New(count); |
| 80 | if (result != NULL) { |
| 81 | PyWeakReference *current = *list; |
Neal Norwitz | c5e060d | 2006-08-02 06:14:22 +0000 | [diff] [blame] | 82 | Py_ssize_t i; |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 83 | for (i = 0; i < count; ++i) { |
| 84 | PyList_SET_ITEM(result, i, (PyObject *) current); |
| 85 | Py_INCREF(current); |
| 86 | current = current->wr_next; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 89 | } |
| 90 | else { |
| 91 | result = PyList_New(0); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 97 | PyDoc_STRVAR(weakref_proxy__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 98 | "proxy(object[, callback]) -- create a proxy object that weakly\n" |
| 99 | "references 'object'. 'callback', if given, is called with a\n" |
Fred Drake | 610291c | 2002-08-02 20:23:40 +0000 | [diff] [blame] | 100 | "reference to the proxy when 'object' is about to be finalized."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 101 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 102 | static PyObject * |
| 103 | weakref_proxy(PyObject *self, PyObject *args) |
| 104 | { |
| 105 | PyObject *object; |
| 106 | PyObject *callback = NULL; |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 107 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 108 | |
Fred Drake | 0d429e8 | 2001-10-23 21:12:47 +0000 | [diff] [blame] | 109 | if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 110 | result = PyWeakref_NewProxy(object, callback); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 111 | } |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 112 | return result; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | static PyMethodDef |
| 117 | weakref_functions[] = { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 118 | {"getweakrefcount", weakref_getweakrefcount, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 119 | weakref_getweakrefcount__doc__}, |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 120 | {"getweakrefs", weakref_getweakrefs, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 121 | weakref_getweakrefs__doc__}, |
| 122 | {"proxy", weakref_proxy, METH_VARARGS, |
| 123 | weakref_proxy__doc__}, |
Antoine Pitrou | f939b3c | 2016-12-27 15:08:27 +0100 | [diff] [blame] | 124 | {"_remove_dead_weakref", remove_dead_weakref, METH_VARARGS, |
| 125 | remove_dead_weakref__doc__}, |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 126 | {NULL, NULL, 0, NULL} |
| 127 | }; |
| 128 | |
| 129 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 130 | PyMODINIT_FUNC |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 131 | init_weakref(void) |
| 132 | { |
| 133 | PyObject *m; |
| 134 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 135 | m = Py_InitModule3("_weakref", weakref_functions, |
| 136 | "Weak-reference support module."); |
| 137 | if (m != NULL) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 138 | Py_INCREF(&_PyWeakref_RefType); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 139 | PyModule_AddObject(m, "ref", |
| 140 | (PyObject *) &_PyWeakref_RefType); |
| 141 | Py_INCREF(&_PyWeakref_RefType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 142 | PyModule_AddObject(m, "ReferenceType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 143 | (PyObject *) &_PyWeakref_RefType); |
| 144 | Py_INCREF(&_PyWeakref_ProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 145 | PyModule_AddObject(m, "ProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 146 | (PyObject *) &_PyWeakref_ProxyType); |
| 147 | Py_INCREF(&_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 148 | PyModule_AddObject(m, "CallableProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 149 | (PyObject *) &_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 150 | } |
| 151 | } |