2001-02-01 01:27:45 -04:00
|
|
|
#include "Python.h"
|
2023-11-18 03:09:23 -04:00
|
|
|
#include "pycore_dict.h" // _PyDict_DelItemIf()
|
|
|
|
#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)
|
2024-04-08 11:58:38 -03:00
|
|
|
/*[clinic end generated code: output=301806d59558ff3e input=7d4d04fcaccf64d5]*/
|
2013-10-19 04:09:25 -03:00
|
|
|
{
|
2024-04-08 11:58:38 -03:00
|
|
|
return _PyWeakref_GetWeakrefCount(object);
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-27 09:19:20 -04:00
|
|
|
static int
|
2024-07-23 14:06:03 -03:00
|
|
|
is_dead_weakref(PyObject *value, void *unused)
|
2016-12-27 09:19:20 -04:00
|
|
|
{
|
|
|
|
if (!PyWeakref_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "not a weakref");
|
|
|
|
return -1;
|
|
|
|
}
|
2023-11-18 21:43:51 -04: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]*/
|
|
|
|
{
|
2024-07-23 14:06:03 -03:00
|
|
|
if (_PyDict_DelItemIf(dct, key, is_dead_weakref, NULL) < 0) {
|
|
|
|
return NULL;
|
2016-12-27 09:19:20 -04:00
|
|
|
}
|
|
|
|
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 *
|
2024-04-08 11:58:38 -03: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
|
|
|
|
2024-04-08 11:58:38 -03:00
|
|
|
PyObject *result = PyList_New(0);
|
2023-06-23 00:00:56 -03:00
|
|
|
if (result == NULL) {
|
2023-11-18 21:43:51 -04:00
|
|
|
return NULL;
|
2023-06-23 00:00:56 -03:00
|
|
|
}
|
|
|
|
|
2024-04-08 11:58:38 -03:00
|
|
|
LOCK_WEAKREFS(object);
|
|
|
|
PyWeakReference *current = *GET_WEAKREFS_LISTPTR(object);
|
|
|
|
while (current != NULL) {
|
|
|
|
PyObject *curobj = (PyObject *) current;
|
|
|
|
if (_Py_TryIncref(curobj)) {
|
|
|
|
if (PyList_Append(result, curobj)) {
|
|
|
|
UNLOCK_WEAKREFS(object);
|
|
|
|
Py_DECREF(curobj);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Undo our _Py_TryIncref. This is safe to do with the lock
|
|
|
|
// held in free-threaded builds; the list holds a reference to
|
|
|
|
// curobj so we're guaranteed not to invoke the destructor.
|
|
|
|
Py_DECREF(curobj);
|
|
|
|
}
|
|
|
|
}
|
2023-06-23 00:00:56 -03:00
|
|
|
current = current->wr_next;
|
2001-02-01 01:27:45 -04:00
|
|
|
}
|
2024-04-08 11:58:38 -03:00
|
|
|
UNLOCK_WEAKREFS(object);
|
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)
|
|
|
|
{
|
2023-07-25 08:34:49 -03:00
|
|
|
if (PyModule_AddObjectRef(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) {
|
2020-03-24 14:31:19 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2023-07-25 08:34:49 -03:00
|
|
|
if (PyModule_AddObjectRef(module, "ReferenceType",
|
2020-03-24 14:31:19 -03:00
|
|
|
(PyObject *) &_PyWeakref_RefType) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-07-25 08:34:49 -03:00
|
|
|
if (PyModule_AddObjectRef(module, "ProxyType",
|
2020-03-24 14:31:19 -03:00
|
|
|
(PyObject *) &_PyWeakref_ProxyType) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-07-25 08:34:49 -03:00
|
|
|
if (PyModule_AddObjectRef(module, "CallableProxyType",
|
2020-03-24 14:31:19 -03:00
|
|
|
(PyObject *) &_PyWeakref_CallableProxyType) < 0) {
|
|
|
|
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},
|
2024-05-03 12:30:55 -03:00
|
|
|
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
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
|
|
|
}
|