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