_weakref.c 3.26 KB
Newer Older
1 2 3 4 5 6 7
#include "Python.h"


#define GET_WEAKREFS_LISTPTR(o) \
        ((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'.");
11

12
static PyObject *
13
weakref_getweakrefcount(PyObject *self, PyObject *object)
14 15 16
{
    PyObject *result = NULL;

17
    if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
18
        PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
19

20
        result = PyLong_FromSsize_t(_PyWeakref_GetWeakrefCount(*list));
21
    }
22
    else
23
        result = PyLong_FromLong(0);
24

25 26 27 28
    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'.");
32

33
static PyObject *
34
weakref_getweakrefs(PyObject *self, PyObject *object)
35 36 37
{
    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);
41

42 43 44
        result = PyList_New(count);
        if (result != NULL) {
            PyWeakReference *current = *list;
45
            Py_ssize_t i;
46 47 48 49
            for (i = 0; i < count; ++i) {
                PyList_SET_ITEM(result, i, (PyObject *) current);
                Py_INCREF(current);
                current = current->wr_next;
50 51
            }
        }
52 53 54
    }
    else {
        result = PyList_New(0);
55 56 57 58 59
    }
    return result;
}


60
PyDoc_STRVAR(weakref_proxy__doc__,
61 62
"proxy(object[, callback]) -- create a proxy object that weakly\n"
"references 'object'.  'callback', if given, is called with a\n"
63
"reference to the proxy when 'object' is about to be finalized.");
64

65 66 67 68 69
static PyObject *
weakref_proxy(PyObject *self, PyObject *args)
{
    PyObject *object;
    PyObject *callback = NULL;
70
    PyObject *result = NULL;
71

72
    if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
73
        result = PyWeakref_NewProxy(object, callback);
74
    }
75
    return result;
76 77 78 79 80
}


static PyMethodDef
weakref_functions[] =  {
81
    {"getweakrefcount", weakref_getweakrefcount,        METH_O,
82
     weakref_getweakrefcount__doc__},
83
    {"getweakrefs",     weakref_getweakrefs,            METH_O,
84 85 86
     weakref_getweakrefs__doc__},
    {"proxy",           weakref_proxy,                  METH_VARARGS,
     weakref_proxy__doc__},
87 88 89 90
    {NULL, NULL, 0, NULL}
};


91 92 93 94 95 96 97 98 99 100 101 102
static struct PyModuleDef weakrefmodule = {
	PyModuleDef_HEAD_INIT,
	"_weakref",
	"Weak-reference support module.",
	-1,
	weakref_functions,
	NULL,
	NULL,
	NULL,
	NULL
};

103
PyMODINIT_FUNC
104
PyInit__weakref(void)
105 106 107
{
    PyObject *m;

108 109
    m = PyModule_Create(&weakrefmodule);
                       
110
    if (m != NULL) {
111
        Py_INCREF(&_PyWeakref_RefType);
112 113 114
        PyModule_AddObject(m, "ref",
                           (PyObject *) &_PyWeakref_RefType);
        Py_INCREF(&_PyWeakref_RefType);
115
        PyModule_AddObject(m, "ReferenceType",
116 117
                           (PyObject *) &_PyWeakref_RefType);
        Py_INCREF(&_PyWeakref_ProxyType);
118
        PyModule_AddObject(m, "ProxyType",
119 120
                           (PyObject *) &_PyWeakref_ProxyType);
        Py_INCREF(&_PyWeakref_CallableProxyType);
121
        PyModule_AddObject(m, "CallableProxyType",
122
                           (PyObject *) &_PyWeakref_CallableProxyType);
123
    }
124
    return m;
125
}