2001-02-01 01:27:45 -04:00
|
|
|
#include "Python.h"
|
2023-06-22 16:56:44 -03:00
|
|
|
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
|
|
|
|
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
|
2001-02-01 01:27:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
#define GET_WEAKREFS_LISTPTR(o) \
|
2020-04-06 09:07:02 -03:00
|
|
|
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2014-01-07 16:41:53 -04:00
|
|
|
/*[clinic input]
|
2013-10-19 04:09:25 -03:00
|
|
|
module _weakref
|
2014-01-07 16:41:53 -04:00
|
|
|
[clinic start generated code]*/
|
2014-01-28 09:00:08 -04:00
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
|
2013-11-23 19:37:55 -04:00
|
|
|
|
2015-04-03 17:53:51 -03:00
|
|
|
#include "clinic/_weakref.c.h"
|
|
|
|
|
2014-01-07 16:41:53 -04:00
|
|
|
/*[clinic input]
|
2013-10-19 04:09:25 -03:00
|
|
|
|
|
|
|
_weakref.getweakrefcount -> Py_ssize_t
|
|
|
|
|
|
|
|
object: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Return the number of weak references to 'object'.
|
2014-01-07 16:41:53 -04:00
|
|
|
[clinic start generated code]*/
|
2013-10-19 04:09:25 -03:00
|
|
|
|
|
|
|
static Py_ssize_t
|
2016-07-07 11:35:15 -03:00
|
|
|
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
|
|
|
|
/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
|
2013-10-19 04:09:25 -03:00
|
|
|
{
|
|
|
|
PyWeakReference **list;
|
2001-08-16 11:11:30 -03:00
|
|
|
|
2022-01-26 22:00:55 -04:00
|
|
|
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
|
2013-10-19 04:09:25 -03:00
|
|
|
return 0;
|
2014-01-07 16:41:53 -04:00
|
|
|
|
2013-10-19 04:09:25 -03:00
|
|
|
list = GET_WEAKREFS_LISTPTR(object);
|
|
|
|
return _PyWeakref_GetWeakrefCount(*list);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-27 09:19:20 -04:00
|
|
|
static int
|
|
|
|
is_dead_weakref(PyObject *value)
|
|
|
|
{
|
|
|
|
if (!PyWeakref_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "not a weakref");
|
|
|
|
return -1;
|
|
|
|
}
|
2023-06-22 16:56:44 -03:00
|
|
|
return _PyWeakref_IS_DEAD(value);
|
2016-12-27 09:19:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
|
|
|
|
_weakref._remove_dead_weakref -> object
|
|
|
|
|
|
|
|
dct: object(subclass_of='&PyDict_Type')
|
|
|
|
key: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Atomically remove key from dict if it points to a dead weakref.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
|
|
|
|
PyObject *key)
|
|
|
|
/*[clinic end generated code: output=d9ff53061fcb875c input=19fc91f257f96a1d]*/
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-07 05:57:45 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_weakref.getweakrefs
|
|
|
|
object: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Return a list of all weak reference objects pointing to 'object'.
|
|
|
|
[clinic start generated code]*/
|
2001-02-18 01:20:18 -04:00
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
static PyObject *
|
2022-03-07 05:57:45 -04:00
|
|
|
_weakref_getweakrefs(PyObject *module, PyObject *object)
|
|
|
|
/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
|
2001-02-01 01:27:45 -04:00
|
|
|
{
|
2023-06-23 00:00:56 -03:00
|
|
|
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
|
|
|
|
return PyList_New(0);
|
2001-08-16 11:11:30 -03:00
|
|
|
}
|
2023-06-23 00:00:56 -03:00
|
|
|
|
|
|
|
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
|
|
|
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
|
|
|
|
|
|
|
|
PyObject *result = PyList_New(count);
|
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyWeakReference *current = *list;
|
|
|
|
for (Py_ssize_t i = 0; i < count; ++i) {
|
|
|
|
PyList_SET_ITEM(result, i, Py_NewRef(current));
|
|
|
|
current = current->wr_next;
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-07 05:57:45 -04:00
|
|
|
/*[clinic input]
|
|
|
|
|
|
|
|
_weakref.proxy
|
|
|
|
object: object
|
|
|
|
callback: object(c_default="NULL") = None
|
|
|
|
/
|
|
|
|
|
|
|
|
Create a proxy object that weakly references 'object'.
|
|
|
|
|
|
|
|
'callback', if given, is called with a reference to the
|
|
|
|
proxy when 'object' is about to be finalized.
|
|
|
|
[clinic start generated code]*/
|
2001-02-18 01:20:18 -04:00
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
static PyObject *
|
2022-03-07 05:57:45 -04:00
|
|
|
_weakref_proxy_impl(PyObject *module, PyObject *object, PyObject *callback)
|
|
|
|
/*[clinic end generated code: output=d68fa4ad9ea40519 input=4808adf22fd137e7]*/
|
2001-02-01 01:27:45 -04:00
|
|
|
{
|
2022-03-07 05:57:45 -04:00
|
|
|
return PyWeakref_NewProxy(object, callback);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef
|
|
|
|
weakref_functions[] = {
|
2013-10-19 04:09:25 -03:00
|
|
|
_WEAKREF_GETWEAKREFCOUNT_METHODDEF
|
2016-12-27 09:19:20 -04:00
|
|
|
_WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF
|
2022-03-07 05:57:45 -04:00
|
|
|
_WEAKREF_GETWEAKREFS_METHODDEF
|
|
|
|
_WEAKREF_PROXY_METHODDEF
|
2001-02-01 01:27:45 -04:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
2020-03-24 14:31:19 -03:00
|
|
|
static int
|
|
|
|
weakref_exec(PyObject *module)
|
|
|
|
{
|
|
|
|
Py_INCREF(&_PyWeakref_RefType);
|
|
|
|
if (PyModule_AddObject(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) {
|
|
|
|
Py_DECREF(&_PyWeakref_RefType);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(&_PyWeakref_RefType);
|
|
|
|
if (PyModule_AddObject(module, "ReferenceType",
|
|
|
|
(PyObject *) &_PyWeakref_RefType) < 0) {
|
|
|
|
Py_DECREF(&_PyWeakref_RefType);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(&_PyWeakref_ProxyType);
|
|
|
|
if (PyModule_AddObject(module, "ProxyType",
|
|
|
|
(PyObject *) &_PyWeakref_ProxyType) < 0) {
|
|
|
|
Py_DECREF(&_PyWeakref_ProxyType);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(&_PyWeakref_CallableProxyType);
|
|
|
|
if (PyModule_AddObject(module, "CallableProxyType",
|
|
|
|
(PyObject *) &_PyWeakref_CallableProxyType) < 0) {
|
|
|
|
Py_DECREF(&_PyWeakref_CallableProxyType);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct PyModuleDef_Slot weakref_slots[] = {
|
|
|
|
{Py_mod_exec, weakref_exec},
|
2023-05-05 18:11:27 -03:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2020-03-24 14:31:19 -03:00
|
|
|
{0, NULL}
|
|
|
|
};
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
static struct PyModuleDef weakrefmodule = {
|
2017-11-28 11:56:10 -04:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"_weakref",
|
|
|
|
"Weak-reference support module.",
|
2020-03-24 14:31:19 -03:00
|
|
|
0,
|
2017-11-28 11:56:10 -04:00
|
|
|
weakref_functions,
|
2020-03-24 14:31:19 -03:00
|
|
|
weakref_slots,
|
2017-11-28 11:56:10 -04:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2008-06-11 02:26:20 -03:00
|
|
|
};
|
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2008-06-11 02:26:20 -03:00
|
|
|
PyInit__weakref(void)
|
2001-02-01 01:27:45 -04:00
|
|
|
{
|
2020-03-24 14:31:19 -03:00
|
|
|
return PyModuleDef_Init(&weakrefmodule);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|