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]
|
2023-11-18 21:43:51 -04:00
|
|
|
@critical_section object
|
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)
|
2023-11-18 21:43:51 -04:00
|
|
|
/*[clinic end generated code: output=301806d59558ff3e input=6535a580f1d0ebdc]*/
|
2013-10-19 04:09:25 -03:00
|
|
|
{
|
2023-11-18 21:43:51 -04:00
|
|
|
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
|
2013-10-19 04:09:25 -03:00
|
|
|
return 0;
|
2023-11-18 21:43:51 -04:00
|
|
|
}
|
|
|
|
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
|
|
|
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
|
2023-11-18 03:09:23 -04:00
|
|
|
return count;
|
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-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]*/
|
|
|
|
{
|
|
|
|
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]
|
2023-11-18 21:43:51 -04:00
|
|
|
@critical_section object
|
2022-03-07 05:57:45 -04:00
|
|
|
_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 *
|
2023-11-18 21:43:51 -04:00
|
|
|
_weakref_getweakrefs_impl(PyObject *module, PyObject *object)
|
|
|
|
/*[clinic end generated code: output=5ec268989fb8f035 input=3dea95b8f5b31bbb]*/
|
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);
|
|
|
|
|
2023-11-18 21:43:51 -04:00
|
|
|
PyObject *result = PyList_New(count);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
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},
|
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
|
|
|
}
|