blob: da589314ea5a50a65077790bb8818793a62849a3 [file] [log] [blame]
Fred Drake41deb1e2001-02-01 05:27:45 +00001#include "Python.h"
Fred Drake41deb1e2001-02-01 05:27:45 +00002
3
4#define GET_WEAKREFS_LISTPTR(o) \
5 ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07008module _weakref
Larry Hastings61272b72014-01-07 12:41:53 -08009[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080010/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080011
Larry Hastings61272b72014-01-07 12:41:53 -080012/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -070013
14_weakref.getweakrefcount -> Py_ssize_t
15
16 object: object
17 /
18
19Return the number of weak references to 'object'.
Larry Hastings61272b72014-01-07 12:41:53 -080020[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -070021
22PyDoc_STRVAR(_weakref_getweakrefcount__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080023"getweakrefcount($module, object, /)\n"
24"--\n"
25"\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -080026"Return the number of weak references to \'object\'.");
Larry Hastings31826802013-10-19 00:09:25 -070027
28#define _WEAKREF_GETWEAKREFCOUNT_METHODDEF \
29 {"getweakrefcount", (PyCFunction)_weakref_getweakrefcount, METH_O, _weakref_getweakrefcount__doc__},
30
31static Py_ssize_t
Larry Hastingsebdcb502013-11-23 14:54:00 -080032_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object);
Fred Drake7855aba2001-02-18 05:20:18 +000033
Fred Drake41deb1e2001-02-01 05:27:45 +000034static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -080035_weakref_getweakrefcount(PyModuleDef *module, PyObject *object)
Fred Drake41deb1e2001-02-01 05:27:45 +000036{
Larry Hastings31826802013-10-19 00:09:25 -070037 PyObject *return_value = NULL;
38 Py_ssize_t _return_value;
Larry Hastingsbebf7352014-01-17 17:47:17 -080039
Larry Hastingsed4a1c52013-11-18 09:32:13 -080040 _return_value = _weakref_getweakrefcount_impl(module, object);
Larry Hastings31826802013-10-19 00:09:25 -070041 if ((_return_value == -1) && PyErr_Occurred())
42 goto exit;
43 return_value = PyLong_FromSsize_t(_return_value);
Fred Drake41deb1e2001-02-01 05:27:45 +000044
Larry Hastings31826802013-10-19 00:09:25 -070045exit:
46 return return_value;
47}
Fred Drake41deb1e2001-02-01 05:27:45 +000048
Larry Hastings31826802013-10-19 00:09:25 -070049static Py_ssize_t
Larry Hastingsebdcb502013-11-23 14:54:00 -080050_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object)
Larry Hastings2623c8c2014-02-08 22:15:29 -080051/*[clinic end generated code: output=032eedbfd7d69e10 input=cedb69711b6a2507]*/
Larry Hastings31826802013-10-19 00:09:25 -070052{
53 PyWeakReference **list;
Fred Drake7fdc0a12001-08-16 14:11:30 +000054
Larry Hastings31826802013-10-19 00:09:25 -070055 if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
56 return 0;
Larry Hastings61272b72014-01-07 12:41:53 -080057
Larry Hastings31826802013-10-19 00:09:25 -070058 list = GET_WEAKREFS_LISTPTR(object);
59 return _PyWeakref_GetWeakrefCount(*list);
Fred Drake41deb1e2001-02-01 05:27:45 +000060}
61
62
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000063PyDoc_STRVAR(weakref_getweakrefs__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +000064"getweakrefs(object) -- return a list of all weak reference objects\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065"that point to 'object'.");
Fred Drake7855aba2001-02-18 05:20:18 +000066
Fred Drake41deb1e2001-02-01 05:27:45 +000067static PyObject *
Fred Drake7fdc0a12001-08-16 14:11:30 +000068weakref_getweakrefs(PyObject *self, PyObject *object)
Fred Drake41deb1e2001-02-01 05:27:45 +000069{
70 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000071
Christian Heimes90aa7642007-12-19 02:45:37 +000072 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
Fred Drake7fdc0a12001-08-16 14:11:30 +000073 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074 Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
Fred Drake41deb1e2001-02-01 05:27:45 +000075
Fred Drake7fdc0a12001-08-16 14:11:30 +000076 result = PyList_New(count);
77 if (result != NULL) {
78 PyWeakReference *current = *list;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 Py_ssize_t i;
Fred Drake7fdc0a12001-08-16 14:11:30 +000080 for (i = 0; i < count; ++i) {
81 PyList_SET_ITEM(result, i, (PyObject *) current);
82 Py_INCREF(current);
83 current = current->wr_next;
Fred Drake41deb1e2001-02-01 05:27:45 +000084 }
85 }
Fred Drake7fdc0a12001-08-16 14:11:30 +000086 }
87 else {
88 result = PyList_New(0);
Fred Drake41deb1e2001-02-01 05:27:45 +000089 }
90 return result;
91}
92
93
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094PyDoc_STRVAR(weakref_proxy__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +000095"proxy(object[, callback]) -- create a proxy object that weakly\n"
96"references 'object'. 'callback', if given, is called with a\n"
Fred Drake610291c2002-08-02 20:23:40 +000097"reference to the proxy when 'object' is about to be finalized.");
Fred Drake7855aba2001-02-18 05:20:18 +000098
Fred Drake41deb1e2001-02-01 05:27:45 +000099static PyObject *
100weakref_proxy(PyObject *self, PyObject *args)
101{
102 PyObject *object;
103 PyObject *callback = NULL;
Fred Drakef7f8cad2001-10-05 22:00:24 +0000104 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +0000105
Fred Drake0d429e82001-10-23 21:12:47 +0000106 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
Fred Drakef7f8cad2001-10-05 22:00:24 +0000107 result = PyWeakref_NewProxy(object, callback);
Fred Drake41deb1e2001-02-01 05:27:45 +0000108 }
Fred Drakef7f8cad2001-10-05 22:00:24 +0000109 return result;
Fred Drake41deb1e2001-02-01 05:27:45 +0000110}
111
112
113static PyMethodDef
114weakref_functions[] = {
Larry Hastings31826802013-10-19 00:09:25 -0700115 _WEAKREF_GETWEAKREFCOUNT_METHODDEF
Fred Drake7fdc0a12001-08-16 14:11:30 +0000116 {"getweakrefs", weakref_getweakrefs, METH_O,
Fred Drake7855aba2001-02-18 05:20:18 +0000117 weakref_getweakrefs__doc__},
118 {"proxy", weakref_proxy, METH_VARARGS,
119 weakref_proxy__doc__},
Fred Drake41deb1e2001-02-01 05:27:45 +0000120 {NULL, NULL, 0, NULL}
121};
122
123
Martin v. Löwis1a214512008-06-11 05:26:20 +0000124static struct PyModuleDef weakrefmodule = {
125 PyModuleDef_HEAD_INIT,
126 "_weakref",
127 "Weak-reference support module.",
128 -1,
129 weakref_functions,
130 NULL,
131 NULL,
132 NULL,
133 NULL
134};
135
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000136PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000137PyInit__weakref(void)
Fred Drake41deb1e2001-02-01 05:27:45 +0000138{
139 PyObject *m;
140
Martin v. Löwis1a214512008-06-11 05:26:20 +0000141 m = PyModule_Create(&weakrefmodule);
Larry Hastings31826802013-10-19 00:09:25 -0700142
Fred Drake41deb1e2001-02-01 05:27:45 +0000143 if (m != NULL) {
Fred Drakef7f8cad2001-10-05 22:00:24 +0000144 Py_INCREF(&_PyWeakref_RefType);
Fred Drake0a4dd392004-07-02 18:57:45 +0000145 PyModule_AddObject(m, "ref",
146 (PyObject *) &_PyWeakref_RefType);
147 Py_INCREF(&_PyWeakref_RefType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000148 PyModule_AddObject(m, "ReferenceType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000149 (PyObject *) &_PyWeakref_RefType);
150 Py_INCREF(&_PyWeakref_ProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000151 PyModule_AddObject(m, "ProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000152 (PyObject *) &_PyWeakref_ProxyType);
153 Py_INCREF(&_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000154 PyModule_AddObject(m, "CallableProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000155 (PyObject *) &_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000156 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000157 return m;
Fred Drake41deb1e2001-02-01 05:27:45 +0000158}