Debian Codegraph @

Modules/_testcapi/gc.c

raw
1 #include "parts.h"
2
3 static PyObject*
4 test_gc_control(PyObject *self, PyObject *Py_UNUSED(ignored))
5 {
6 int orig_enabled = PyGC_IsEnabled();
7 const char* msg = "ok";
8 int old_state;
9
10 old_state = PyGC_Enable();
11 msg = "Enable(1)";
12 if (old_state != orig_enabled) {
13 goto failed;
14 }
15 msg = "IsEnabled(1)";
16 if (!PyGC_IsEnabled()) {
17 goto failed;
18 }
19
20 old_state = PyGC_Disable();
21 msg = "disable(2)";
22 if (!old_state) {
23 goto failed;
24 }
25 msg = "IsEnabled(2)";
26 if (PyGC_IsEnabled()) {
27 goto failed;
28 }
29
30 old_state = PyGC_Enable();
31 msg = "enable(3)";
32 if (old_state) {
33 goto failed;
34 }
35 msg = "IsEnabled(3)";
36 if (!PyGC_IsEnabled()) {
37 goto failed;
38 }
39
40 if (!orig_enabled) {
41 old_state = PyGC_Disable();
42 msg = "disable(4)";
43 if (old_state) {
44 goto failed;
45 }
46 msg = "IsEnabled(4)";
47 if (PyGC_IsEnabled()) {
48 goto failed;
49 }
50 }
51
52 Py_RETURN_NONE;
53
54 failed:
55 /* Try to clean up if we can. */
56 if (orig_enabled) {
57 PyGC_Enable();
58 } else {
59 PyGC_Disable();
60 }
61 PyErr_Format(PyExc_ValueError, "GC control failed in %s", msg);
62 return NULL;
63 }
64
65 static PyObject *
66 without_gc(PyObject *Py_UNUSED(self), PyObject *obj)
67 {
68 PyTypeObject *tp = (PyTypeObject*)obj;
69 if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
70 return PyErr_Format(PyExc_TypeError, "heap type expected, got %R", obj);
71 }
72 if (PyType_IS_GC(tp)) {
73 // Don't try this at home, kids:
74 tp->tp_flags -= Py_TPFLAGS_HAVE_GC;
75 tp->tp_free = PyObject_Free;
76 tp->tp_traverse = NULL;
77 tp->tp_clear = NULL;
78 }
79 assert(!PyType_IS_GC(tp));
80 return Py_NewRef(obj);
81 }
82
83 static void
84 slot_tp_del(PyObject *self)
85 {
86 PyObject *del, *res;
87
88 /* Temporarily resurrect the object. */
89 assert(Py_REFCNT(self) == 0);
90 Py_SET_REFCNT(self, 1);
91
92 /* Save the current exception, if any. */
93 PyObject *exc = PyErr_GetRaisedException();
94
95 PyObject *tp_del = PyUnicode_InternFromString("__tp_del__");
96 if (tp_del == NULL) {
97 PyErr_FormatUnraisable("Exception ignored while deallocating");
98 PyErr_SetRaisedException(exc);
99 return;
100 }
101 /* Execute __del__ method, if any. */
102 del = _PyType_LookupRef(Py_TYPE(self), tp_del);
103 Py_DECREF(tp_del);
104 if (del != NULL) {
105 res = PyObject_CallOneArg(del, self);
106 Py_DECREF(del);
107 if (res == NULL) {
108 PyErr_FormatUnraisable("Exception ignored while calling "
109 "deallocator %R", del);
110 }
111 else {
112 Py_DECREF(res);
113 }
114 }
115
116 /* Restore the saved exception. */
117 PyErr_SetRaisedException(exc);
118
119 /* Undo the temporary resurrection; can't use DECREF here, it would
120 * cause a recursive call.
121 */
122 assert(Py_REFCNT(self) > 0);
123 Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
124 if (Py_REFCNT(self) == 0) {
125 /* this is the normal path out */
126 return;
127 }
128
129 /* __del__ resurrected it! Make it look like the original Py_DECREF
130 * never happened.
131 */
132 {
133 _Py_ResurrectReference(self);
134 }
135 assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
136 }
137
138 static PyObject *
139 with_tp_del(PyObject *self, PyObject *args)
140 {
141 PyObject *obj;
142 PyTypeObject *tp;
143
144 if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
145 return NULL;
146 tp = (PyTypeObject *) obj;
147 if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
148 PyErr_Format(PyExc_TypeError,
149 "heap type expected, got %R", obj);
150 return NULL;
151 }
152 tp->tp_del = slot_tp_del;
153 return Py_NewRef(obj);
154 }
155
156
157 struct gc_visit_state_basic {
158 PyObject *target;
159 int found;
160 };
161
162 static int
163 gc_visit_callback_basic(PyObject *obj, void *arg)
164 {
165 struct gc_visit_state_basic *state = (struct gc_visit_state_basic *)arg;
166 if (obj == state->target) {
167 state->found = 1;
168 return 0;
169 }
170 return 1;
171 }
172
173 static PyObject *
174 test_gc_visit_objects_basic(PyObject *Py_UNUSED(self),
175 PyObject *Py_UNUSED(ignored))
176 {
177 PyObject *obj;
178 struct gc_visit_state_basic state;
179
180 obj = PyList_New(0);
181 if (obj == NULL) {
182 return NULL;
183 }
184 state.target = obj;
185 state.found = 0;
186
187 PyUnstable_GC_VisitObjects(gc_visit_callback_basic, &state);
188 Py_DECREF(obj);
189 if (!state.found) {
190 PyErr_SetString(
191 PyExc_AssertionError,
192 "test_gc_visit_objects_basic: Didn't find live list");
193 return NULL;
194 }
195 Py_RETURN_NONE;
196 }
197
198 static int
199 gc_visit_callback_exit_early(PyObject *obj, void *arg)
200 {
201 int *visited_i = (int *)arg;
202 (*visited_i)++;
203 if (*visited_i == 2) {
204 return 0;
205 }
206 return 1;
207 }
208
209 static PyObject *
210 test_gc_visit_objects_exit_early(PyObject *Py_UNUSED(self),
211 PyObject *Py_UNUSED(ignored))
212 {
213 int visited_i = 0;
214 PyUnstable_GC_VisitObjects(gc_visit_callback_exit_early, &visited_i);
215 if (visited_i != 2) {
216 PyErr_SetString(
217 PyExc_AssertionError,
218 "test_gc_visit_objects_exit_early: did not exit when expected");
219 }
220 Py_RETURN_NONE;
221 }
222
223 typedef struct {
224 PyObject_HEAD
225 } ObjExtraData;
226
227 static PyObject *
228 obj_extra_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
229 {
230 size_t extra_size = sizeof(PyObject *);
231 PyObject *obj = PyUnstable_Object_GC_NewWithExtraData(type, extra_size);
232 if (obj == NULL) {
233 return PyErr_NoMemory();
234 }
235 PyObject_GC_Track(obj);
236 return obj;
237 }
238
239 static PyObject **
240 obj_extra_data_get_extra_storage(PyObject *self)
241 {
242 return (PyObject **)((char *)self + Py_TYPE(self)->tp_basicsize);
243 }
244
245 static PyObject *
246 obj_extra_data_get(PyObject *self, void *Py_UNUSED(ignored))
247 {
248 PyObject **extra_storage = obj_extra_data_get_extra_storage(self);
249 PyObject *value = *extra_storage;
250 if (!value) {
251 Py_RETURN_NONE;
252 }
253 return Py_NewRef(value);
254 }
255
256 static int
257 obj_extra_data_set(PyObject *self, PyObject *newval, void *Py_UNUSED(ignored))
258 {
259 PyObject **extra_storage = obj_extra_data_get_extra_storage(self);
260 Py_CLEAR(*extra_storage);
261 if (newval) {
262 *extra_storage = Py_NewRef(newval);
263 }
264 return 0;
265 }
266
267 static PyGetSetDef obj_extra_data_getset[] = {
268 {"extra", obj_extra_data_get, obj_extra_data_set, NULL},
269 {NULL}
270 };
271
272 static int
273 obj_extra_data_traverse(PyObject *self, visitproc visit, void *arg)
274 {
275 PyObject **extra_storage = obj_extra_data_get_extra_storage(self);
276 PyObject *value = *extra_storage;
277 Py_VISIT(value);
278 return 0;
279 }
280
281 static int
282 obj_extra_data_clear(PyObject *self)
283 {
284 PyObject **extra_storage = obj_extra_data_get_extra_storage(self);
285 Py_CLEAR(*extra_storage);
286 return 0;
287 }
288
289 static void
290 obj_extra_data_dealloc(PyObject *self)
291 {
292 PyTypeObject *tp = Py_TYPE(self);
293 PyObject_GC_UnTrack(self);
294 obj_extra_data_clear(self);
295 tp->tp_free(self);
296 Py_DECREF(tp);
297 }
298
299 static PyType_Slot ObjExtraData_Slots[] = {
300 {Py_tp_getset, obj_extra_data_getset},
301 {Py_tp_dealloc, obj_extra_data_dealloc},
302 {Py_tp_traverse, obj_extra_data_traverse},
303 {Py_tp_clear, obj_extra_data_clear},
304 {Py_tp_new, obj_extra_data_new},
305 {Py_tp_free, PyObject_GC_Del},
306 {0, NULL},
307 };
308
309 static PyType_Spec ObjExtraData_TypeSpec = {
310 .name = "_testcapi.ObjExtraData",
311 .basicsize = sizeof(ObjExtraData),
312 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
313 .slots = ObjExtraData_Slots,
314 };
315
316 static PyMethodDef test_methods[] = {
317 {"test_gc_control", test_gc_control, METH_NOARGS},
318 {"test_gc_visit_objects_basic", test_gc_visit_objects_basic, METH_NOARGS, NULL},
319 {"test_gc_visit_objects_exit_early", test_gc_visit_objects_exit_early, METH_NOARGS, NULL},
320 {"without_gc", without_gc, METH_O, NULL},
321 {"with_tp_del", with_tp_del, METH_VARARGS, NULL},
322 {NULL}
323 };
324
325 int _PyTestCapi_Init_GC(PyObject *mod)
326 {
327 if (PyModule_AddFunctions(mod, test_methods) < 0) {
328 return -1;
329 }
330 if (PyModule_AddFunctions(mod, test_methods) < 0) {
331 return -1;
332 }
333
334 PyObject *ObjExtraData_Type = PyType_FromModuleAndSpec(
335 mod, &ObjExtraData_TypeSpec, NULL);
336 if (ObjExtraData_Type == 0) {
337 return -1;
338 }
339 int ret = PyModule_AddType(mod, (PyTypeObject*)ObjExtraData_Type);
340 Py_DECREF(ObjExtraData_Type);
341 if (ret < 0) {
342 return ret;
343 }
344
345 return 0;
346 }