blob: 21521150ae58b17e3ceb5a40d4458d6369b92521 [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
Fred Drake7fdc0a12001-08-16 14:11:30 +000017 if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) {
18 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Fred Drake41deb1e2001-02-01 05:27:45 +000019
Fred Drakef7f8cad2001-10-05 22:00:24 +000020 result = PyInt_FromLong(_PyWeakref_GetWeakrefCount(*list));
Fred Drake41deb1e2001-02-01 05:27:45 +000021 }
Fred Drake7fdc0a12001-08-16 14:11:30 +000022 else
23 result = PyInt_FromLong(0);
24
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
Fred Drake7fdc0a12001-08-16 14:11:30 +000038 if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) {
39 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Fred Drakef7f8cad2001-10-05 22:00:24 +000040 long 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;
45 long i;
46 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_ref__doc__,
Fred Drake610291c2002-08-02 20:23:40 +000061"ref(object[, callback]) -- create a weak reference to 'object';\n"
Fred Drake7855aba2001-02-18 05:20:18 +000062"when 'object' is finalized, 'callback' will be called and passed\n"
Fred Drake610291c2002-08-02 20:23:40 +000063"a reference to the weak reference object when 'object' is about\n"
64"to be finalized.");
Fred Drake7855aba2001-02-18 05:20:18 +000065
Fred Drake41deb1e2001-02-01 05:27:45 +000066static PyObject *
67weakref_ref(PyObject *self, PyObject *args)
68{
69 PyObject *object;
70 PyObject *callback = NULL;
Fred Drakef7f8cad2001-10-05 22:00:24 +000071 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000072
Fred Drake0d429e82001-10-23 21:12:47 +000073 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
Fred Drakef7f8cad2001-10-05 22:00:24 +000074 result = PyWeakref_NewRef(object, callback);
Fred Drake41deb1e2001-02-01 05:27:45 +000075 }
Fred Drakef7f8cad2001-10-05 22:00:24 +000076 return result;
Fred Drake41deb1e2001-02-01 05:27:45 +000077}
78
79
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(weakref_proxy__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +000081"proxy(object[, callback]) -- create a proxy object that weakly\n"
82"references 'object'. 'callback', if given, is called with a\n"
Fred Drake610291c2002-08-02 20:23:40 +000083"reference to the proxy when 'object' is about to be finalized.");
Fred Drake7855aba2001-02-18 05:20:18 +000084
Fred Drake41deb1e2001-02-01 05:27:45 +000085static PyObject *
86weakref_proxy(PyObject *self, PyObject *args)
87{
88 PyObject *object;
89 PyObject *callback = NULL;
Fred Drakef7f8cad2001-10-05 22:00:24 +000090 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000091
Fred Drake0d429e82001-10-23 21:12:47 +000092 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
Fred Drakef7f8cad2001-10-05 22:00:24 +000093 result = PyWeakref_NewProxy(object, callback);
Fred Drake41deb1e2001-02-01 05:27:45 +000094 }
Fred Drakef7f8cad2001-10-05 22:00:24 +000095 return result;
Fred Drake41deb1e2001-02-01 05:27:45 +000096}
97
98
99static PyMethodDef
100weakref_functions[] = {
Fred Drake7fdc0a12001-08-16 14:11:30 +0000101 {"getweakrefcount", weakref_getweakrefcount, METH_O,
Fred Drake7855aba2001-02-18 05:20:18 +0000102 weakref_getweakrefcount__doc__},
Fred Drake7fdc0a12001-08-16 14:11:30 +0000103 {"getweakrefs", weakref_getweakrefs, METH_O,
Fred Drake7855aba2001-02-18 05:20:18 +0000104 weakref_getweakrefs__doc__},
105 {"proxy", weakref_proxy, METH_VARARGS,
106 weakref_proxy__doc__},
Fred Drake41deb1e2001-02-01 05:27:45 +0000107 {"ref", weakref_ref, METH_VARARGS,
Fred Drake7855aba2001-02-18 05:20:18 +0000108 weakref_ref__doc__},
Fred Drake41deb1e2001-02-01 05:27:45 +0000109 {NULL, NULL, 0, NULL}
110};
111
112
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000113PyMODINIT_FUNC
Fred Drake41deb1e2001-02-01 05:27:45 +0000114init_weakref(void)
115{
116 PyObject *m;
117
Fred Drake41deb1e2001-02-01 05:27:45 +0000118 m = Py_InitModule3("_weakref", weakref_functions,
119 "Weak-reference support module.");
120 if (m != NULL) {
Fred Drakef7f8cad2001-10-05 22:00:24 +0000121 Py_INCREF(&_PyWeakref_RefType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000122 PyModule_AddObject(m, "ReferenceType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000123 (PyObject *) &_PyWeakref_RefType);
124 Py_INCREF(&_PyWeakref_ProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000125 PyModule_AddObject(m, "ProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000126 (PyObject *) &_PyWeakref_ProxyType);
127 Py_INCREF(&_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000128 PyModule_AddObject(m, "CallableProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000129 (PyObject *) &_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000130 }
131}