_make_boundary(): Fix for SF bug #745478, broken boundary calculation
[python/dscho.git] / Modules / _weakref.c
blob21521150ae58b17e3ceb5a40d4458d6369b92521
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(object->ob_type)) {
18 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
20 result = PyInt_FromLong(_PyWeakref_GetWeakrefCount(*list));
22 else
23 result = PyInt_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(object->ob_type)) {
39 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
40 long count = _PyWeakref_GetWeakrefCount(*list);
42 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;
53 else {
54 result = PyList_New(0);
56 return result;
60 PyDoc_STRVAR(weakref_ref__doc__,
61 "ref(object[, callback]) -- create a weak reference to 'object';\n"
62 "when 'object' is finalized, 'callback' will be called and passed\n"
63 "a reference to the weak reference object when 'object' is about\n"
64 "to be finalized.");
66 static PyObject *
67 weakref_ref(PyObject *self, PyObject *args)
69 PyObject *object;
70 PyObject *callback = NULL;
71 PyObject *result = NULL;
73 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
74 result = PyWeakref_NewRef(object, callback);
76 return result;
80 PyDoc_STRVAR(weakref_proxy__doc__,
81 "proxy(object[, callback]) -- create a proxy object that weakly\n"
82 "references 'object'. 'callback', if given, is called with a\n"
83 "reference to the proxy when 'object' is about to be finalized.");
85 static PyObject *
86 weakref_proxy(PyObject *self, PyObject *args)
88 PyObject *object;
89 PyObject *callback = NULL;
90 PyObject *result = NULL;
92 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
93 result = PyWeakref_NewProxy(object, callback);
95 return result;
99 static PyMethodDef
100 weakref_functions[] = {
101 {"getweakrefcount", weakref_getweakrefcount, METH_O,
102 weakref_getweakrefcount__doc__},
103 {"getweakrefs", weakref_getweakrefs, METH_O,
104 weakref_getweakrefs__doc__},
105 {"proxy", weakref_proxy, METH_VARARGS,
106 weakref_proxy__doc__},
107 {"ref", weakref_ref, METH_VARARGS,
108 weakref_ref__doc__},
109 {NULL, NULL, 0, NULL}
113 PyMODINIT_FUNC
114 init_weakref(void)
116 PyObject *m;
118 m = Py_InitModule3("_weakref", weakref_functions,
119 "Weak-reference support module.");
120 if (m != NULL) {
121 Py_INCREF(&_PyWeakref_RefType);
122 PyModule_AddObject(m, "ReferenceType",
123 (PyObject *) &_PyWeakref_RefType);
124 Py_INCREF(&_PyWeakref_ProxyType);
125 PyModule_AddObject(m, "ProxyType",
126 (PyObject *) &_PyWeakref_ProxyType);
127 Py_INCREF(&_PyWeakref_CallableProxyType);
128 PyModule_AddObject(m, "CallableProxyType",
129 (PyObject *) &_PyWeakref_CallableProxyType);