Merged revisions 81146 via svnmerge from
[python/dscho.git] / Modules / _weakref.c
blob88995b88a85c5f19b553e4e77dd6b9ecb6182411
1 #include "Python.h"
4 #define GET_WEAKREFS_LISTPTR(o) \
5 ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
8 PyDoc_STRVAR(weakref_getweakrefcount__doc__,
9 "getweakrefcount(object) -- return the number of weak references\n"
10 "to 'object'.");
12 static PyObject *
13 weakref_getweakrefcount(PyObject *self, PyObject *object)
15 PyObject *result = NULL;
17 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
18 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
20 result = PyLong_FromSsize_t(_PyWeakref_GetWeakrefCount(*list));
22 else
23 result = PyLong_FromLong(0);
25 return result;
29 PyDoc_STRVAR(weakref_getweakrefs__doc__,
30 "getweakrefs(object) -- return a list of all weak reference objects\n"
31 "that point to 'object'.");
33 static PyObject *
34 weakref_getweakrefs(PyObject *self, PyObject *object)
36 PyObject *result = NULL;
38 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
39 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
40 Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
42 result = PyList_New(count);
43 if (result != NULL) {
44 PyWeakReference *current = *list;
45 Py_ssize_t 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;
53 else {
54 result = PyList_New(0);
56 return result;
60 PyDoc_STRVAR(weakref_proxy__doc__,
61 "proxy(object[, callback]) -- create a proxy object that weakly\n"
62 "references 'object'. 'callback', if given, is called with a\n"
63 "reference to the proxy when 'object' is about to be finalized.");
65 static PyObject *
66 weakref_proxy(PyObject *self, PyObject *args)
68 PyObject *object;
69 PyObject *callback = NULL;
70 PyObject *result = NULL;
72 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
73 result = PyWeakref_NewProxy(object, callback);
75 return result;
79 static PyMethodDef
80 weakref_functions[] = {
81 {"getweakrefcount", weakref_getweakrefcount, METH_O,
82 weakref_getweakrefcount__doc__},
83 {"getweakrefs", weakref_getweakrefs, METH_O,
84 weakref_getweakrefs__doc__},
85 {"proxy", weakref_proxy, METH_VARARGS,
86 weakref_proxy__doc__},
87 {NULL, NULL, 0, NULL}
91 static 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
103 PyMODINIT_FUNC
104 PyInit__weakref(void)
106 PyObject *m;
108 m = PyModule_Create(&weakrefmodule);
110 if (m != NULL) {
111 Py_INCREF(&_PyWeakref_RefType);
112 PyModule_AddObject(m, "ref",
113 (PyObject *) &_PyWeakref_RefType);
114 Py_INCREF(&_PyWeakref_RefType);
115 PyModule_AddObject(m, "ReferenceType",
116 (PyObject *) &_PyWeakref_RefType);
117 Py_INCREF(&_PyWeakref_ProxyType);
118 PyModule_AddObject(m, "ProxyType",
119 (PyObject *) &_PyWeakref_ProxyType);
120 Py_INCREF(&_PyWeakref_CallableProxyType);
121 PyModule_AddObject(m, "CallableProxyType",
122 (PyObject *) &_PyWeakref_CallableProxyType);
124 return m;