ยปCore Development>Code coverage>Modules/_weakref.c

Python code coverage for Modules/_weakref.c

#countcontent
1n/a#include "Python.h"
2n/a
3n/a
4n/a#define GET_WEAKREFS_LISTPTR(o) \
5n/a ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
6n/a
7n/a/*[clinic input]
8n/amodule _weakref
9n/a[clinic start generated code]*/
10n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
11n/a
12n/a#include "clinic/_weakref.c.h"
13n/a
14n/a/*[clinic input]
15n/a
16n/a_weakref.getweakrefcount -> Py_ssize_t
17n/a
18n/a object: object
19n/a /
20n/a
21n/aReturn the number of weak references to 'object'.
22n/a[clinic start generated code]*/
23n/a
24n/astatic Py_ssize_t
25n/a_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
26n/a/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
27n/a{
28n/a PyWeakReference **list;
29n/a
30n/a if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
31n/a return 0;
32n/a
33n/a list = GET_WEAKREFS_LISTPTR(object);
34n/a return _PyWeakref_GetWeakrefCount(*list);
35n/a}
36n/a
37n/a
38n/astatic int
39n/ais_dead_weakref(PyObject *value)
40n/a{
41n/a if (!PyWeakref_Check(value)) {
42n/a PyErr_SetString(PyExc_TypeError, "not a weakref");
43n/a return -1;
44n/a }
45n/a return PyWeakref_GET_OBJECT(value) == Py_None;
46n/a}
47n/a
48n/a/*[clinic input]
49n/a
50n/a_weakref._remove_dead_weakref -> object
51n/a
52n/a dct: object(subclass_of='&PyDict_Type')
53n/a key: object
54n/a /
55n/a
56n/aAtomically remove key from dict if it points to a dead weakref.
57n/a[clinic start generated code]*/
58n/a
59n/astatic PyObject *
60n/a_weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
61n/a PyObject *key)
62n/a/*[clinic end generated code: output=d9ff53061fcb875c input=19fc91f257f96a1d]*/
63n/a{
64n/a if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) {
65n/a if (PyErr_ExceptionMatches(PyExc_KeyError))
66n/a /* This function is meant to allow safe weak-value dicts
67n/a with GC in another thread (see issue #28427), so it's
68n/a ok if the key doesn't exist anymore.
69n/a */
70n/a PyErr_Clear();
71n/a else
72n/a return NULL;
73n/a }
74n/a Py_RETURN_NONE;
75n/a}
76n/a
77n/a
78n/aPyDoc_STRVAR(weakref_getweakrefs__doc__,
79n/a"getweakrefs(object) -- return a list of all weak reference objects\n"
80n/a"that point to 'object'.");
81n/a
82n/astatic PyObject *
83n/aweakref_getweakrefs(PyObject *self, PyObject *object)
84n/a{
85n/a PyObject *result = NULL;
86n/a
87n/a if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
88n/a PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
89n/a Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
90n/a
91n/a result = PyList_New(count);
92n/a if (result != NULL) {
93n/a PyWeakReference *current = *list;
94n/a Py_ssize_t i;
95n/a for (i = 0; i < count; ++i) {
96n/a PyList_SET_ITEM(result, i, (PyObject *) current);
97n/a Py_INCREF(current);
98n/a current = current->wr_next;
99n/a }
100n/a }
101n/a }
102n/a else {
103n/a result = PyList_New(0);
104n/a }
105n/a return result;
106n/a}
107n/a
108n/a
109n/aPyDoc_STRVAR(weakref_proxy__doc__,
110n/a"proxy(object[, callback]) -- create a proxy object that weakly\n"
111n/a"references 'object'. 'callback', if given, is called with a\n"
112n/a"reference to the proxy when 'object' is about to be finalized.");
113n/a
114n/astatic PyObject *
115n/aweakref_proxy(PyObject *self, PyObject *args)
116n/a{
117n/a PyObject *object;
118n/a PyObject *callback = NULL;
119n/a PyObject *result = NULL;
120n/a
121n/a if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
122n/a result = PyWeakref_NewProxy(object, callback);
123n/a }
124n/a return result;
125n/a}
126n/a
127n/a
128n/astatic PyMethodDef
129n/aweakref_functions[] = {
130n/a _WEAKREF_GETWEAKREFCOUNT_METHODDEF
131n/a _WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF
132n/a {"getweakrefs", weakref_getweakrefs, METH_O,
133n/a weakref_getweakrefs__doc__},
134n/a {"proxy", weakref_proxy, METH_VARARGS,
135n/a weakref_proxy__doc__},
136n/a {NULL, NULL, 0, NULL}
137n/a};
138n/a
139n/a
140n/astatic struct PyModuleDef weakrefmodule = {
141n/a PyModuleDef_HEAD_INIT,
142n/a "_weakref",
143n/a "Weak-reference support module.",
144n/a -1,
145n/a weakref_functions,
146n/a NULL,
147n/a NULL,
148n/a NULL,
149n/a NULL
150n/a};
151n/a
152n/aPyMODINIT_FUNC
153n/aPyInit__weakref(void)
154n/a{
155n/a PyObject *m;
156n/a
157n/a m = PyModule_Create(&weakrefmodule);
158n/a
159n/a if (m != NULL) {
160n/a Py_INCREF(&_PyWeakref_RefType);
161n/a PyModule_AddObject(m, "ref",
162n/a (PyObject *) &_PyWeakref_RefType);
163n/a Py_INCREF(&_PyWeakref_RefType);
164n/a PyModule_AddObject(m, "ReferenceType",
165n/a (PyObject *) &_PyWeakref_RefType);
166n/a Py_INCREF(&_PyWeakref_ProxyType);
167n/a PyModule_AddObject(m, "ProxyType",
168n/a (PyObject *) &_PyWeakref_ProxyType);
169n/a Py_INCREF(&_PyWeakref_CallableProxyType);
170n/a PyModule_AddObject(m, "CallableProxyType",
171n/a (PyObject *) &_PyWeakref_CallableProxyType);
172n/a }
173n/a return m;
174n/a}