2001-02-01 01:27:45 -04:00
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define GET_WEAKREFS_LISTPTR(o) \
|
|
|
|
((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
|
|
|
|
|
|
|
|
|
2016-12-27 10:08:27 -04:00
|
|
|
static int
|
|
|
|
is_dead_weakref(PyObject *value)
|
|
|
|
{
|
|
|
|
if (!PyWeakref_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "not a weakref");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return PyWeakref_GET_OBJECT(value) == Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(remove_dead_weakref__doc__,
|
|
|
|
"_remove_dead_weakref(dict, key) -- atomically remove key from dict\n"
|
|
|
|
"if it points to a dead weakref.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
remove_dead_weakref(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *dct, *key;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O!O:_remove_dead_weakref",
|
|
|
|
&PyDict_Type, &dct, &key)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_KeyError))
|
|
|
|
/* This function is meant to allow safe weak-value dicts
|
|
|
|
with GC in another thread (see issue #28427), so it's
|
|
|
|
ok if the key doesn't exist anymore.
|
|
|
|
*/
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(weakref_getweakrefcount__doc__,
|
2001-02-18 01:20:18 -04:00
|
|
|
"getweakrefcount(object) -- return the number of weak references\n"
|
2002-06-13 17:33:02 -03:00
|
|
|
"to 'object'.");
|
2001-02-18 01:20:18 -04:00
|
|
|
|
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;
|
|
|
|
|
2007-12-18 22:37:44 -04:00
|
|
|
if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
|
2001-08-16 11:11:30 -03:00
|
|
|
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2006-08-02 03:14:22 -03:00
|
|
|
result = PyInt_FromSsize_t(_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(weakref_getweakrefs__doc__,
|
2001-02-18 01:20:18 -04:00
|
|
|
"getweakrefs(object) -- return a list of all weak reference objects\n"
|
2002-06-13 17:33:02 -03:00
|
|
|
"that point to 'object'.");
|
2001-02-18 01:20:18 -04:00
|
|
|
|
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;
|
|
|
|
|
2007-12-18 22:37:44 -04:00
|
|
|
if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
|
2001-08-16 11:11:30 -03:00
|
|
|
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
2006-08-02 03:14:22 -03:00
|
|
|
Py_ssize_t 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;
|
2006-08-02 03:14:22 -03:00
|
|
|
Py_ssize_t i;
|
2001-08-16 11:11:30 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(weakref_proxy__doc__,
|
2001-02-18 01:20:18 -04:00
|
|
|
"proxy(object[, callback]) -- create a proxy object that weakly\n"
|
|
|
|
"references 'object'. 'callback', if given, is called with a\n"
|
2002-08-02 17:23:40 -03:00
|
|
|
"reference to the proxy when 'object' is about to be finalized.");
|
2001-02-18 01:20:18 -04:00
|
|
|
|
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
|
|
|
|
2001-10-23 18:12:47 -03:00
|
|
|
if (PyArg_UnpackTuple(args, "proxy", 1, 2, &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__},
|
2016-12-27 10:08:27 -04:00
|
|
|
{"_remove_dead_weakref", remove_dead_weakref, METH_VARARGS,
|
|
|
|
remove_dead_weakref__doc__},
|
2001-02-01 01:27:45 -04:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
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);
|
2004-07-02 15:57:45 -03:00
|
|
|
PyModule_AddObject(m, "ref",
|
|
|
|
(PyObject *) &_PyWeakref_RefType);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|