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