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 | |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 8 | static char weakref_getweakrefcount__doc__[] = |
| 9 | "getweakrefcount(object) -- return the number of weak references\n" |
| 10 | "to 'object'."; |
| 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 | |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 29 | static char weakref_getweakrefs__doc__[] = |
| 30 | "getweakrefs(object) -- return a list of all weak reference objects\n" |
| 31 | "that point to 'object'."; |
| 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 | |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 60 | static char weakref_ref__doc__[] = |
| 61 | "new(object[, callback]) -- create a weak reference to 'object';\n" |
| 62 | "when 'object' is finalized, 'callback' will be called and passed\n" |
| 63 | "a reference to 'object'."; |
| 64 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 65 | static PyObject * |
| 66 | weakref_ref(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 | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 72 | if (PyArg_ParseTuple(args, "O|O:ref", &object, &callback)) { |
| 73 | result = PyWeakref_NewRef(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 | |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 79 | static char weakref_proxy__doc__[] = |
| 80 | "proxy(object[, callback]) -- create a proxy object that weakly\n" |
| 81 | "references 'object'. 'callback', if given, is called with a\n" |
| 82 | "reference to the proxy when it is about to be finalized."; |
| 83 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 84 | static PyObject * |
| 85 | weakref_proxy(PyObject *self, PyObject *args) |
| 86 | { |
| 87 | PyObject *object; |
| 88 | PyObject *callback = NULL; |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 89 | PyObject *result = NULL; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 90 | |
| 91 | if (PyArg_ParseTuple(args, "O|O:new", &object, &callback)) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 92 | result = PyWeakref_NewProxy(object, callback); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 93 | } |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 94 | return result; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
| 98 | static PyMethodDef |
| 99 | weakref_functions[] = { |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 100 | {"getweakrefcount", weakref_getweakrefcount, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 101 | weakref_getweakrefcount__doc__}, |
Fred Drake | 7fdc0a1 | 2001-08-16 14:11:30 +0000 | [diff] [blame] | 102 | {"getweakrefs", weakref_getweakrefs, METH_O, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 103 | weakref_getweakrefs__doc__}, |
| 104 | {"proxy", weakref_proxy, METH_VARARGS, |
| 105 | weakref_proxy__doc__}, |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 106 | {"ref", weakref_ref, METH_VARARGS, |
Fred Drake | 7855aba | 2001-02-18 05:20:18 +0000 | [diff] [blame] | 107 | weakref_ref__doc__}, |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 108 | {NULL, NULL, 0, NULL} |
| 109 | }; |
| 110 | |
| 111 | |
Tim Peters | 747d5b6 | 2001-02-02 00:07:07 +0000 | [diff] [blame] | 112 | DL_EXPORT(void) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 113 | init_weakref(void) |
| 114 | { |
| 115 | PyObject *m; |
| 116 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 117 | m = Py_InitModule3("_weakref", weakref_functions, |
| 118 | "Weak-reference support module."); |
| 119 | if (m != NULL) { |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 120 | Py_INCREF(&_PyWeakref_RefType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 121 | PyModule_AddObject(m, "ReferenceType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 122 | (PyObject *) &_PyWeakref_RefType); |
| 123 | Py_INCREF(&_PyWeakref_ProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 124 | PyModule_AddObject(m, "ProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 125 | (PyObject *) &_PyWeakref_ProxyType); |
| 126 | Py_INCREF(&_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 127 | PyModule_AddObject(m, "CallableProxyType", |
Fred Drake | f7f8cad | 2001-10-05 22:00:24 +0000 | [diff] [blame] | 128 | (PyObject *) &_PyWeakref_CallableProxyType); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 129 | } |
| 130 | } |