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