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 | |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 17 | if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) { |
| 18 | PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 19 | |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 20 | result = PyInt_FromLong(_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 |
| 23 | result = PyInt_FromLong(0); |
| 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 | |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 38 | if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) { |
| 39 | PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 40 | long 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; |
| 45 | long i; |
| 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_ref__doc__, |
Fred Drake | 610291c | 2002-08-02 20:23:40 +0000 | [diff] [blame] | 61 | "ref(object[, callback]) -- create a weak reference to 'object';\n" |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 62 | "when 'object' is finalized, 'callback' will be called and passed\n" |
Fred Drake | 610291c | 2002-08-02 20:23:40 +0000 | [diff] [blame] | 63 | "a reference to the weak reference object when 'object' is about\n" |
| 64 | "to be finalized."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 65 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 66 | static PyObject * |
| 67 | weakref_ref(PyObject *self, PyObject *args) |
| 68 | { |
| 69 | PyObject *object; |
| 70 | PyObject *callback = NULL; |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 71 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 72 | |
Fred Drake | 0d429e8 | 2001-10-23 21:12:47 +0000 | [diff] [blame] | 73 | if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 74 | result = PyWeakref_NewRef(object, callback); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 75 | } |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 76 | return result; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 80 | PyDoc_STRVAR(weakref_proxy__doc__, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 81 | "proxy(object[, callback]) -- create a proxy object that weakly\n" |
| 82 | "references 'object'. 'callback', if given, is called with a\n" |
Fred Drake | 610291c | 2002-08-02 20:23:40 +0000 | [diff] [blame] | 83 | "reference to the proxy when 'object' is about to be finalized."); |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 84 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 85 | static PyObject * |
| 86 | weakref_proxy(PyObject *self, PyObject *args) |
| 87 | { |
| 88 | PyObject *object; |
| 89 | PyObject *callback = NULL; |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 90 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 91 | |
Fred Drake | 0d429e8 | 2001-10-23 21:12:47 +0000 | [diff] [blame] | 92 | if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 93 | result = PyWeakref_NewProxy(object, callback); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 94 | } |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 95 | return result; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | static PyMethodDef |
| 100 | weakref_functions[] = { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 101 | {"getweakrefcount", weakref_getweakrefcount, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 102 | weakref_getweakrefcount__doc__}, |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 103 | {"getweakrefs", weakref_getweakrefs, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 104 | weakref_getweakrefs__doc__}, |
| 105 | {"proxy", weakref_proxy, METH_VARARGS, |
| 106 | weakref_proxy__doc__}, |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 107 | {"ref", weakref_ref, METH_VARARGS, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 108 | weakref_ref__doc__}, |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 109 | {NULL, NULL, 0, NULL} |
| 110 | }; |
| 111 | |
| 112 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 113 | PyMODINIT_FUNC |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 114 | init_weakref(void) |
| 115 | { |
| 116 | PyObject *m; |
| 117 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 118 | m = Py_InitModule3("_weakref", weakref_functions, |
| 119 | "Weak-reference support module."); |
| 120 | if (m != NULL) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 121 | Py_INCREF(&_PyWeakref_RefType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 122 | PyModule_AddObject(m, "ReferenceType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 123 | (PyObject *) &_PyWeakref_RefType); |
| 124 | Py_INCREF(&_PyWeakref_ProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 125 | PyModule_AddObject(m, "ProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 126 | (PyObject *) &_PyWeakref_ProxyType); |
| 127 | Py_INCREF(&_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 128 | PyModule_AddObject(m, "CallableProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 129 | (PyObject *) &_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 130 | } |
| 131 | } |