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


#define GET_WEAKREFS_LISTPTR(o) \
        ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))

7
/*[clinic input]
8
module _weakref
9
[clinic start generated code]*/
10
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
11

12
/*[clinic input]
13 14 15 16 17 18 19

_weakref.getweakrefcount -> Py_ssize_t

  object: object
  /

Return the number of weak references to 'object'.
20
[clinic start generated code]*/
21 22

PyDoc_STRVAR(_weakref_getweakrefcount__doc__,
23 24 25
"getweakrefcount($module, object, /)\n"
"--\n"
"\n"
26
"Return the number of weak references to \'object\'.");
27 28 29 30 31

#define _WEAKREF_GETWEAKREFCOUNT_METHODDEF    \
    {"getweakrefcount", (PyCFunction)_weakref_getweakrefcount, METH_O, _weakref_getweakrefcount__doc__},

static Py_ssize_t
32
_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object);
33

34
static PyObject *
35
_weakref_getweakrefcount(PyModuleDef *module, PyObject *object)
36
{
37 38
    PyObject *return_value = NULL;
    Py_ssize_t _return_value;
39

40
    _return_value = _weakref_getweakrefcount_impl(module, object);
41 42 43 44 45 46 47
    if ((_return_value == -1) && PyErr_Occurred())
        goto exit;
    return_value = PyLong_FromSsize_t(_return_value);

exit:
    return return_value;
}
48

49
static Py_ssize_t
50
_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object)
51
/*[clinic end generated code: output=032eedbfd7d69e10 input=cedb69711b6a2507]*/
52 53
{
    PyWeakReference **list;
54

55 56
    if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
        return 0;
57

58 59
    list = GET_WEAKREFS_LISTPTR(object);
    return _PyWeakref_GetWeakrefCount(*list);
60 61 62
}


63
PyDoc_STRVAR(weakref_getweakrefs__doc__,
64
"getweakrefs(object) -- return a list of all weak reference objects\n"
65
"that point to 'object'.");
66

67
static PyObject *
68
weakref_getweakrefs(PyObject *self, PyObject *object)
69 70 71
{
    PyObject *result = NULL;

72
    if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
73
        PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
74
        Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
75

76 77 78
        result = PyList_New(count);
        if (result != NULL) {
            PyWeakReference *current = *list;
79
            Py_ssize_t i;
80 81 82 83
            for (i = 0; i < count; ++i) {
                PyList_SET_ITEM(result, i, (PyObject *) current);
                Py_INCREF(current);
                current = current->wr_next;
84 85
            }
        }
86 87 88
    }
    else {
        result = PyList_New(0);
89 90 91 92 93
    }
    return result;
}


94
PyDoc_STRVAR(weakref_proxy__doc__,
95 96
"proxy(object[, callback]) -- create a proxy object that weakly\n"
"references 'object'.  'callback', if given, is called with a\n"
97
"reference to the proxy when 'object' is about to be finalized.");
98

99 100 101 102 103
static PyObject *
weakref_proxy(PyObject *self, PyObject *args)
{
    PyObject *object;
    PyObject *callback = NULL;
104
    PyObject *result = NULL;
105

106
    if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
107
        result = PyWeakref_NewProxy(object, callback);
108
    }
109
    return result;
110 111 112 113 114
}


static PyMethodDef
weakref_functions[] =  {
115
    _WEAKREF_GETWEAKREFCOUNT_METHODDEF
116
    {"getweakrefs",     weakref_getweakrefs,            METH_O,
117 118 119
     weakref_getweakrefs__doc__},
    {"proxy",           weakref_proxy,                  METH_VARARGS,
     weakref_proxy__doc__},
120 121 122 123
    {NULL, NULL, 0, NULL}
};


124 125 126 127 128 129 130 131 132 133 134 135
static struct PyModuleDef weakrefmodule = {
	PyModuleDef_HEAD_INIT,
	"_weakref",
	"Weak-reference support module.",
	-1,
	weakref_functions,
	NULL,
	NULL,
	NULL,
	NULL
};

136
PyMODINIT_FUNC
137
PyInit__weakref(void)
138 139 140
{
    PyObject *m;

141
    m = PyModule_Create(&weakrefmodule);
142

143
    if (m != NULL) {
144
        Py_INCREF(&_PyWeakref_RefType);
145 146 147
        PyModule_AddObject(m, "ref",
                           (PyObject *) &_PyWeakref_RefType);
        Py_INCREF(&_PyWeakref_RefType);
148
        PyModule_AddObject(m, "ReferenceType",
149 150
                           (PyObject *) &_PyWeakref_RefType);
        Py_INCREF(&_PyWeakref_ProxyType);
151
        PyModule_AddObject(m, "ProxyType",
152 153
                           (PyObject *) &_PyWeakref_ProxyType);
        Py_INCREF(&_PyWeakref_CallableProxyType);
154
        PyModule_AddObject(m, "CallableProxyType",
155
                           (PyObject *) &_PyWeakref_CallableProxyType);
156
    }
157
    return m;
158
}