2001-02-01 01:27:45 -04:00
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define GET_WEAKREFS_LISTPTR(o) \
|
|
|
|
((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
|
|
|
|
|
|
|
|
|
2001-02-18 01:20:18 -04:00
|
|
|
static char weakref_getweakrefcount__doc__[] =
|
|
|
|
"getweakrefcount(object) -- return the number of weak references\n"
|
|
|
|
"to 'object'.";
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
static PyObject *
|
2001-08-16 11:11:30 -03:00
|
|
|
weakref_getweakrefcount(PyObject *self, PyObject *object)
|
2001-02-01 01:27:45 -04:00
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
|
|
|
|
2001-08-16 11:11:30 -03:00
|
|
|
if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) {
|
|
|
|
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2001-10-05 19:00:24 -03:00
|
|
|
result = PyInt_FromLong(_PyWeakref_GetWeakrefCount(*list));
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
2001-08-16 11:11:30 -03:00
|
|
|
else
|
|
|
|
result = PyInt_FromLong(0);
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-18 01:20:18 -04:00
|
|
|
static char weakref_getweakrefs__doc__[] =
|
|
|
|
"getweakrefs(object) -- return a list of all weak reference objects\n"
|
|
|
|
"that point to 'object'.";
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
static PyObject *
|
2001-08-16 11:11:30 -03:00
|
|
|
weakref_getweakrefs(PyObject *self, PyObject *object)
|
2001-02-01 01:27:45 -04:00
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
|
|
|
|
2001-08-16 11:11:30 -03:00
|
|
|
if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) {
|
|
|
|
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
2001-10-05 19:00:24 -03:00
|
|
|
long count = _PyWeakref_GetWeakrefCount(*list);
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2001-08-16 11:11:30 -03:00
|
|
|
result = PyList_New(count);
|
|
|
|
if (result != NULL) {
|
|
|
|
PyWeakReference *current = *list;
|
|
|
|
long i;
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
PyList_SET_ITEM(result, i, (PyObject *) current);
|
|
|
|
Py_INCREF(current);
|
|
|
|
current = current->wr_next;
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
}
|
2001-08-16 11:11:30 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PyList_New(0);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-18 01:20:18 -04:00
|
|
|
static char weakref_ref__doc__[] =
|
|
|
|
"new(object[, callback]) -- create a weak reference to 'object';\n"
|
|
|
|
"when 'object' is finalized, 'callback' will be called and passed\n"
|
|
|
|
"a reference to 'object'.";
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
static PyObject *
|
|
|
|
weakref_ref(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *object;
|
|
|
|
PyObject *callback = NULL;
|
2001-10-05 19:00:24 -03:00
|
|
|
PyObject *result = NULL;
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2001-10-05 19:00:24 -03:00
|
|
|
if (PyArg_ParseTuple(args, "O|O:ref", &object, &callback)) {
|
|
|
|
result = PyWeakref_NewRef(object, callback);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
2001-10-05 19:00:24 -03:00
|
|
|
return result;
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-18 01:20:18 -04:00
|
|
|
static char weakref_proxy__doc__[] =
|
|
|
|
"proxy(object[, callback]) -- create a proxy object that weakly\n"
|
|
|
|
"references 'object'. 'callback', if given, is called with a\n"
|
|
|
|
"reference to the proxy when it is about to be finalized.";
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
static PyObject *
|
|
|
|
weakref_proxy(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *object;
|
|
|
|
PyObject *callback = NULL;
|
2001-10-05 19:00:24 -03:00
|
|
|
PyObject *result = NULL;
|
2001-02-01 01:27:45 -04:00
|
|
|
|
|
|
|
if (PyArg_ParseTuple(args, "O|O:new", &object, &callback)) {
|
2001-10-05 19:00:24 -03:00
|
|
|
result = PyWeakref_NewProxy(object, callback);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
2001-10-05 19:00:24 -03:00
|
|
|
return result;
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef
|
|
|
|
weakref_functions[] = {
|
2001-08-16 11:11:30 -03:00
|
|
|
{"getweakrefcount", weakref_getweakrefcount, METH_O,
|
2001-02-18 01:20:18 -04:00
|
|
|
weakref_getweakrefcount__doc__},
|
2001-08-16 11:11:30 -03:00
|
|
|
{"getweakrefs", weakref_getweakrefs, METH_O,
|
2001-02-18 01:20:18 -04:00
|
|
|
weakref_getweakrefs__doc__},
|
|
|
|
{"proxy", weakref_proxy, METH_VARARGS,
|
|
|
|
weakref_proxy__doc__},
|
2001-02-01 01:27:45 -04:00
|
|
|
{"ref", weakref_ref, METH_VARARGS,
|
2001-02-18 01:20:18 -04:00
|
|
|
weakref_ref__doc__},
|
2001-02-01 01:27:45 -04:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-02-01 20:07:07 -04:00
|
|
|
DL_EXPORT(void)
|
2001-02-01 01:27:45 -04:00
|
|
|
init_weakref(void)
|
|
|
|
{
|
|
|
|
PyObject *m;
|
|
|
|
|
|
|
|
m = Py_InitModule3("_weakref", weakref_functions,
|
|
|
|
"Weak-reference support module.");
|
|
|
|
if (m != NULL) {
|
2001-10-05 19:00:24 -03:00
|
|
|
Py_INCREF(&_PyWeakref_RefType);
|
2001-02-01 01:27:45 -04:00
|
|
|
PyModule_AddObject(m, "ReferenceType",
|
2001-10-05 19:00:24 -03:00
|
|
|
(PyObject *) &_PyWeakref_RefType);
|
|
|
|
Py_INCREF(&_PyWeakref_ProxyType);
|
2001-02-01 01:27:45 -04:00
|
|
|
PyModule_AddObject(m, "ProxyType",
|
2001-10-05 19:00:24 -03:00
|
|
|
(PyObject *) &_PyWeakref_ProxyType);
|
|
|
|
Py_INCREF(&_PyWeakref_CallableProxyType);
|
2001-02-01 01:27:45 -04:00
|
|
|
PyModule_AddObject(m, "CallableProxyType",
|
2001-10-05 19:00:24 -03:00
|
|
|
(PyObject *) &_PyWeakref_CallableProxyType);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
}
|