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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8 | PyDoc_STRVAR(weakref_getweakrefcount__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 9 | "getweakrefcount(object) -- return the number of weak references\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10 | "to 'object'."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 11 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 12 | static PyObject * |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 13 | weakref_getweakrefcount(PyObject *self, PyObject *object) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 14 | { |
| 15 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 16 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 17 | if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 18 | PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 19 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 20 | result = PyLong_FromSsize_t(_PyWeakref_GetWeakrefCount(*list)); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 21 | } |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 22 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 23 | result = PyLong_FromLong(0); |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 24 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 25 | return result; |
| 26 | } |
| 27 | |
| 28 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | PyDoc_STRVAR(weakref_getweakrefs__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 30 | "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] | 31 | "that point to 'object'."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 32 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 33 | static PyObject * |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 34 | weakref_getweakrefs(PyObject *self, PyObject *object) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 35 | { |
| 36 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 37 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 38 | if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 39 | PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 40 | Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 41 | |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 42 | result = PyList_New(count); |
| 43 | if (result != NULL) { |
| 44 | PyWeakReference *current = *list; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 45 | Py_ssize_t i; |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 46 | for (i = 0; i < count; ++i) { |
| 47 | PyList_SET_ITEM(result, i, (PyObject *) current); |
| 48 | Py_INCREF(current); |
| 49 | current = current->wr_next; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 52 | } |
| 53 | else { |
| 54 | result = PyList_New(0); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 60 | PyDoc_STRVAR(weakref_proxy__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 61 | "proxy(object[, callback]) -- create a proxy object that weakly\n" |
| 62 | "references 'object'. 'callback', if given, is called with a\n" |
Fred Drake | 610291c | 2002-08-02 20:23:40 +0000 | [diff] [blame] | 63 | "reference to the proxy when 'object' is about to be finalized."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 64 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 65 | static PyObject * |
| 66 | weakref_proxy(PyObject *self, PyObject *args) |
| 67 | { |
| 68 | PyObject *object; |
| 69 | PyObject *callback = NULL; |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 70 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 71 | |
Fred Drake | 0d429e8 | 2001-10-23 21:12:47 +0000 | [diff] [blame] | 72 | if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 73 | result = PyWeakref_NewProxy(object, callback); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 74 | } |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 75 | return result; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | static PyMethodDef |
| 80 | weakref_functions[] = { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 81 | {"getweakrefcount", weakref_getweakrefcount, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 82 | weakref_getweakrefcount__doc__}, |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 83 | {"getweakrefs", weakref_getweakrefs, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 84 | weakref_getweakrefs__doc__}, |
| 85 | {"proxy", weakref_proxy, METH_VARARGS, |
| 86 | weakref_proxy__doc__}, |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 87 | {NULL, NULL, 0, NULL} |
| 88 | }; |
| 89 | |
| 90 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 91 | static struct PyModuleDef weakrefmodule = { |
| 92 | PyModuleDef_HEAD_INIT, |
| 93 | "_weakref", |
| 94 | "Weak-reference support module.", |
| 95 | -1, |
| 96 | weakref_functions, |
| 97 | NULL, |
| 98 | NULL, |
| 99 | NULL, |
| 100 | NULL |
| 101 | }; |
| 102 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 103 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 104 | PyInit__weakref(void) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 105 | { |
| 106 | PyObject *m; |
| 107 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 108 | m = PyModule_Create(&weakrefmodule); |
| 109 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 110 | if (m != NULL) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 111 | Py_INCREF(&_PyWeakref_RefType); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 112 | PyModule_AddObject(m, "ref", |
| 113 | (PyObject *) &_PyWeakref_RefType); |
| 114 | Py_INCREF(&_PyWeakref_RefType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 115 | PyModule_AddObject(m, "ReferenceType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 116 | (PyObject *) &_PyWeakref_RefType); |
| 117 | Py_INCREF(&_PyWeakref_ProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 118 | PyModule_AddObject(m, "ProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 119 | (PyObject *) &_PyWeakref_ProxyType); |
| 120 | Py_INCREF(&_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 121 | PyModule_AddObject(m, "CallableProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 122 | (PyObject *) &_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 123 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 124 | return m; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 125 | } |