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