2001-02-01 06:27:45 +01:00
|
|
|
#include "Python.h"
|
2023-11-18 08:09:23 +01: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 06:27:45 +01:00
|
|
|
|
|
|
|
#define GET_WEAKREFS_LISTPTR(o) \
|
2020-04-06 14:07:02 +02:00
|
|
|
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
|
2001-02-01 06:27:45 +01:00
|
|
|
|
2014-01-07 21:41:53 +01:00
|
|
|
/*[clinic input]
|
2013-10-19 09:09:25 +02:00
|
|
|
module _weakref
|
2014-01-07 21:41:53 +01:00
|
|
|
[clinic start generated code]*/
|
2014-01-28 14:00:08 +01:00
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
|
2013-11-24 00:37:55 +01:00
|
|
|
|
2015-04-03 22:53:51 +02:00
|
|
|
#include "clinic/_weakref.c.h"
|
|
|
|
|
2014-01-07 21:41:53 +01:00
|
|
|
/*[clinic input]
|
2013-10-19 09:09:25 +02:00
|
|
|
_weakref.getweakrefcount -> Py_ssize_t
|
|
|
|
|
|
|
|
object: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Return the number of weak references to 'object'.
|
2014-01-07 21:41:53 +01:00
|
|
|
[clinic start generated code]*/
|
2013-10-19 09:09:25 +02:00
|
|
|
|
|
|
|
static Py_ssize_t
|
2016-07-07 16:35:15 +02:00
|
|
|
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
|
2024-04-08 16:58:38 +02:00
|
|
|
/*[clinic end generated code: output=301806d59558ff3e input=7d4d04fcaccf64d5]*/
|
2013-10-19 09:09:25 +02:00
|
|
|
{
|
2024-04-08 16:58:38 +02:00
|
|
|
return _PyWeakref_GetWeakrefCount(object);
|
2001-02-01 06:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-27 14:19:20 +01:00
|
|
|
static int
|
2024-07-23 19:06:03 +02:00
|
|
|
is_dead_weakref(PyObject *value, void *unused)
|
2016-12-27 14:19:20 +01:00
|
|
|
{
|
|
|
|
if (!PyWeakref_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "not a weakref");
|
|
|
|
return -1;
|
|
|
|
}
|
2023-11-19 02:43:51 +01:00
|
|
|
return _PyWeakref_IS_DEAD(value);
|
2016-12-27 14:19:20 +01: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 19:06:03 +02:00
|
|
|
if (_PyDict_DelItemIf(dct, key, is_dead_weakref, NULL) < 0) {
|
|
|
|
return NULL;
|
2016-12-27 14:19:20 +01:00
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-07 10:57:45 +01: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 06:20:18 +01:00
|
|
|
|
2001-02-01 06:27:45 +01:00
|
|
|
static PyObject *
|
2024-04-08 16:58:38 +02:00
|
|
|
_weakref_getweakrefs(PyObject *module, PyObject *object)
|
|
|
|
/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
|
2001-02-01 06:27:45 +01:00
|
|
|
{
|
2023-06-23 05:00:56 +02:00
|
|
|
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
|
|
|
|
return PyList_New(0);
|
2001-08-16 16:11:30 +02:00
|
|
|
}
|
2023-06-23 05:00:56 +02:00
|
|
|
|
2024-04-08 16:58:38 +02:00
|
|
|
PyObject *result = PyList_New(0);
|
2023-06-23 05:00:56 +02:00
|
|
|
if (result == NULL) {
|
2023-11-19 02:43:51 +01:00
|
|
|
return NULL;
|
2023-06-23 05:00:56 +02:00
|
|
|
}
|
|
|
|
|
2024-04-08 16:58:38 +02: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 05:00:56 +02:00
|
|
|
current = current->wr_next;
|
2001-02-01 06:27:45 +01:00
|
|
|
}
|
2024-04-08 16:58:38 +02:00
|
|
|
UNLOCK_WEAKREFS(object);
|
2001-02-01 06:27:45 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-07 10:57:45 +01: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 06:20:18 +01:00
|
|
|
|
2001-02-01 06:27:45 +01:00
|
|
|
static PyObject *
|
2022-03-07 10:57:45 +01:00
|
|
|
_weakref_proxy_impl(PyObject *module, PyObject *object, PyObject *callback)
|
|
|
|
/*[clinic end generated code: output=d68fa4ad9ea40519 input=4808adf22fd137e7]*/
|
2001-02-01 06:27:45 +01:00
|
|
|
{
|
2022-03-07 10:57:45 +01:00
|
|
|
return PyWeakref_NewProxy(object, callback);
|
2001-02-01 06:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef
|
|
|
|
weakref_functions[] = {
|
2013-10-19 09:09:25 +02:00
|
|
|
_WEAKREF_GETWEAKREFCOUNT_METHODDEF
|
2016-12-27 14:19:20 +01:00
|
|
|
_WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF
|
2022-03-07 10:57:45 +01:00
|
|
|
_WEAKREF_GETWEAKREFS_METHODDEF
|
|
|
|
_WEAKREF_PROXY_METHODDEF
|
2001-02-01 06:27:45 +01:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
2020-03-24 18:31:19 +01:00
|
|
|
static int
|
|
|
|
weakref_exec(PyObject *module)
|
|
|
|
{
|
2023-07-25 13:34:49 +02:00
|
|
|
if (PyModule_AddObjectRef(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) {
|
2020-03-24 18:31:19 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2023-07-25 13:34:49 +02:00
|
|
|
if (PyModule_AddObjectRef(module, "ReferenceType",
|
2020-03-24 18:31:19 +01:00
|
|
|
(PyObject *) &_PyWeakref_RefType) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-07-25 13:34:49 +02:00
|
|
|
if (PyModule_AddObjectRef(module, "ProxyType",
|
2020-03-24 18:31:19 +01:00
|
|
|
(PyObject *) &_PyWeakref_ProxyType) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-07-25 13:34:49 +02:00
|
|
|
if (PyModule_AddObjectRef(module, "CallableProxyType",
|
2020-03-24 18:31:19 +01:00
|
|
|
(PyObject *) &_PyWeakref_CallableProxyType) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct PyModuleDef_Slot weakref_slots[] = {
|
|
|
|
{Py_mod_exec, weakref_exec},
|
2023-05-05 23:11:27 +02:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2024-05-03 17:30:55 +02:00
|
|
|
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
2020-03-24 18:31:19 +01:00
|
|
|
{0, NULL}
|
|
|
|
};
|
2001-02-01 06:27:45 +01:00
|
|
|
|
2008-06-11 07:26:20 +02:00
|
|
|
static struct PyModuleDef weakrefmodule = {
|
2017-11-28 16:56:10 +01:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"_weakref",
|
|
|
|
"Weak-reference support module.",
|
2020-03-24 18:31:19 +01:00
|
|
|
0,
|
2017-11-28 16:56:10 +01:00
|
|
|
weakref_functions,
|
2020-03-24 18:31:19 +01:00
|
|
|
weakref_slots,
|
2017-11-28 16:56:10 +01:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2008-06-11 07:26:20 +02:00
|
|
|
};
|
|
|
|
|
2002-08-02 04:27:13 +02:00
|
|
|
PyMODINIT_FUNC
|
2008-06-11 07:26:20 +02:00
|
|
|
PyInit__weakref(void)
|
2001-02-01 06:27:45 +01:00
|
|
|
{
|
2020-03-24 18:31:19 +01:00
|
|
|
return PyModuleDef_Init(&weakrefmodule);
|
2001-02-01 06:27:45 +01:00
|
|
|
}
|