blob: 88995b88a85c5f19b553e4e77dd6b9ecb6182411 [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
Fred Drake41deb1e2001-02-01 05:27:45 +00007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008PyDoc_STRVAR(weakref_getweakrefcount__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +00009"getweakrefcount(object) -- return the number of weak references\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010"to 'object'.");
Fred Drake7855aba2001-02-18 05:20:18 +000011
Fred Drake41deb1e2001-02-01 05:27:45 +000012static PyObject *
Fred Drake7fdc0a12001-08-16 14:11:30 +000013weakref_getweakrefcount(PyObject *self, PyObject *object)
Fred Drake41deb1e2001-02-01 05:27:45 +000014{
15 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000016
Christian Heimes90aa7642007-12-19 02:45:37 +000017 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
Fred Drake7fdc0a12001-08-16 14:11:30 +000018 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Fred Drake41deb1e2001-02-01 05:27:45 +000019
Christian Heimes217cfd12007-12-02 14:31:20 +000020 result = PyLong_FromSsize_t(_PyWeakref_GetWeakrefCount(*list));
Fred Drake41deb1e2001-02-01 05:27:45 +000021 }
Fred Drake7fdc0a12001-08-16 14:11:30 +000022 else
Christian Heimes217cfd12007-12-02 14:31:20 +000023 result = PyLong_FromLong(0);
Fred Drake7fdc0a12001-08-16 14:11:30 +000024
Fred Drake41deb1e2001-02-01 05:27:45 +000025 return result;
26}
27
28
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029PyDoc_STRVAR(weakref_getweakrefs__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +000030"getweakrefs(object) -- return a list of all weak reference objects\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031"that point to 'object'.");
Fred Drake7855aba2001-02-18 05:20:18 +000032
Fred Drake41deb1e2001-02-01 05:27:45 +000033static PyObject *
Fred Drake7fdc0a12001-08-16 14:11:30 +000034weakref_getweakrefs(PyObject *self, PyObject *object)
Fred Drake41deb1e2001-02-01 05:27:45 +000035{
36 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000037
Christian Heimes90aa7642007-12-19 02:45:37 +000038 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
Fred Drake7fdc0a12001-08-16 14:11:30 +000039 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000040 Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
Fred Drake41deb1e2001-02-01 05:27:45 +000041
Fred Drake7fdc0a12001-08-16 14:11:30 +000042 result = PyList_New(count);
43 if (result != NULL) {
44 PyWeakReference *current = *list;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000045 Py_ssize_t i;
Fred Drake7fdc0a12001-08-16 14:11:30 +000046 for (i = 0; i < count; ++i) {
47 PyList_SET_ITEM(result, i, (PyObject *) current);
48 Py_INCREF(current);
49 current = current->wr_next;
Fred Drake41deb1e2001-02-01 05:27:45 +000050 }
51 }
Fred Drake7fdc0a12001-08-16 14:11:30 +000052 }
53 else {
54 result = PyList_New(0);
Fred Drake41deb1e2001-02-01 05:27:45 +000055 }
56 return result;
57}
58
59
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000060PyDoc_STRVAR(weakref_proxy__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +000061"proxy(object[, callback]) -- create a proxy object that weakly\n"
62"references 'object'. 'callback', if given, is called with a\n"
Fred Drake610291c2002-08-02 20:23:40 +000063"reference to the proxy when 'object' is about to be finalized.");
Fred Drake7855aba2001-02-18 05:20:18 +000064
Fred Drake41deb1e2001-02-01 05:27:45 +000065static PyObject *
66weakref_proxy(PyObject *self, PyObject *args)
67{
68 PyObject *object;
69 PyObject *callback = NULL;
Fred Drakef7f8cad2001-10-05 22:00:24 +000070 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000071
Fred Drake0d429e82001-10-23 21:12:47 +000072 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
Fred Drakef7f8cad2001-10-05 22:00:24 +000073 result = PyWeakref_NewProxy(object, callback);
Fred Drake41deb1e2001-02-01 05:27:45 +000074 }
Fred Drakef7f8cad2001-10-05 22:00:24 +000075 return result;
Fred Drake41deb1e2001-02-01 05:27:45 +000076}
77
78
79static PyMethodDef
80weakref_functions[] = {
Fred Drake7fdc0a12001-08-16 14:11:30 +000081 {"getweakrefcount", weakref_getweakrefcount, METH_O,
Fred Drake7855aba2001-02-18 05:20:18 +000082 weakref_getweakrefcount__doc__},
Fred Drake7fdc0a12001-08-16 14:11:30 +000083 {"getweakrefs", weakref_getweakrefs, METH_O,
Fred Drake7855aba2001-02-18 05:20:18 +000084 weakref_getweakrefs__doc__},
85 {"proxy", weakref_proxy, METH_VARARGS,
86 weakref_proxy__doc__},
Fred Drake41deb1e2001-02-01 05:27:45 +000087 {NULL, NULL, 0, NULL}
88};
89
90
Martin v. Löwis1a214512008-06-11 05:26:20 +000091static struct PyModuleDef weakrefmodule = {
92 PyModuleDef_HEAD_INIT,
93 "_weakref",
94 "Weak-reference support module.",
95 -1,
96 weakref_functions,
97 NULL,
98 NULL,
99 NULL,
100 NULL
101};
102
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000103PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000104PyInit__weakref(void)
Fred Drake41deb1e2001-02-01 05:27:45 +0000105{
106 PyObject *m;
107
Martin v. Löwis1a214512008-06-11 05:26:20 +0000108 m = PyModule_Create(&weakrefmodule);
109
Fred Drake41deb1e2001-02-01 05:27:45 +0000110 if (m != NULL) {
Fred Drakef7f8cad2001-10-05 22:00:24 +0000111 Py_INCREF(&_PyWeakref_RefType);
Fred Drake0a4dd392004-07-02 18:57:45 +0000112 PyModule_AddObject(m, "ref",
113 (PyObject *) &_PyWeakref_RefType);
114 Py_INCREF(&_PyWeakref_RefType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000115 PyModule_AddObject(m, "ReferenceType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000116 (PyObject *) &_PyWeakref_RefType);
117 Py_INCREF(&_PyWeakref_ProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000118 PyModule_AddObject(m, "ProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000119 (PyObject *) &_PyWeakref_ProxyType);
120 Py_INCREF(&_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000121 PyModule_AddObject(m, "CallableProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000122 (PyObject *) &_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000123 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000124 return m;
Fred Drake41deb1e2001-02-01 05:27:45 +0000125}