1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t()
10 #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
11
12 PyDoc_STRVAR(gc_enable__doc__,
13 "enable($module, /)\n"
14 "--\n"
15 "\n"
16 "Enable automatic garbage collection.");
17
18 #define GC_ENABLE_METHODDEF \
19 {"enable", (PyCFunction)gc_enable, METH_NOARGS, gc_enable__doc__},
20
21 static PyObject *
22 gc_enable_impl(PyObject *module);
23
24 static PyObject *
gc_enable(PyObject * module,PyObject * Py_UNUSED (ignored))25 gc_enable(PyObject *module, PyObject *Py_UNUSED(ignored))
26 {
27 return gc_enable_impl(module);
28 }
29
30 PyDoc_STRVAR(gc_disable__doc__,
31 "disable($module, /)\n"
32 "--\n"
33 "\n"
34 "Disable automatic garbage collection.");
35
36 #define GC_DISABLE_METHODDEF \
37 {"disable", (PyCFunction)gc_disable, METH_NOARGS, gc_disable__doc__},
38
39 static PyObject *
40 gc_disable_impl(PyObject *module);
41
42 static PyObject *
gc_disable(PyObject * module,PyObject * Py_UNUSED (ignored))43 gc_disable(PyObject *module, PyObject *Py_UNUSED(ignored))
44 {
45 return gc_disable_impl(module);
46 }
47
48 PyDoc_STRVAR(gc_isenabled__doc__,
49 "isenabled($module, /)\n"
50 "--\n"
51 "\n"
52 "Returns true if automatic garbage collection is enabled.");
53
54 #define GC_ISENABLED_METHODDEF \
55 {"isenabled", (PyCFunction)gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
56
57 static int
58 gc_isenabled_impl(PyObject *module);
59
60 static PyObject *
gc_isenabled(PyObject * module,PyObject * Py_UNUSED (ignored))61 gc_isenabled(PyObject *module, PyObject *Py_UNUSED(ignored))
62 {
63 PyObject *return_value = NULL;
64 int _return_value;
65
66 _return_value = gc_isenabled_impl(module);
67 if ((_return_value == -1) && PyErr_Occurred()) {
68 goto exit;
69 }
70 return_value = PyBool_FromLong((long)_return_value);
71
72 exit:
73 return return_value;
74 }
75
76 PyDoc_STRVAR(gc_collect__doc__,
77 "collect($module, /, generation=2)\n"
78 "--\n"
79 "\n"
80 "Run the garbage collector.\n"
81 "\n"
82 "With no arguments, run a full collection. The optional argument\n"
83 "may be an integer specifying which generation to collect. A ValueError\n"
84 "is raised if the generation number is invalid.\n"
85 "\n"
86 "The number of unreachable objects is returned.");
87
88 #define GC_COLLECT_METHODDEF \
89 {"collect", _PyCFunction_CAST(gc_collect), METH_FASTCALL|METH_KEYWORDS, gc_collect__doc__},
90
91 static Py_ssize_t
92 gc_collect_impl(PyObject *module, int generation);
93
94 static PyObject *
gc_collect(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)95 gc_collect(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
96 {
97 PyObject *return_value = NULL;
98 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
99
100 #define NUM_KEYWORDS 1
101 static struct {
102 PyGC_Head _this_is_not_used;
103 PyObject_VAR_HEAD
104 PyObject *ob_item[NUM_KEYWORDS];
105 } _kwtuple = {
106 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
107 .ob_item = { &_Py_ID(generation), },
108 };
109 #undef NUM_KEYWORDS
110 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
111
112 #else // !Py_BUILD_CORE
113 # define KWTUPLE NULL
114 #endif // !Py_BUILD_CORE
115
116 static const char * const _keywords[] = {"generation", NULL};
117 static _PyArg_Parser _parser = {
118 .keywords = _keywords,
119 .fname = "collect",
120 .kwtuple = KWTUPLE,
121 };
122 #undef KWTUPLE
123 PyObject *argsbuf[1];
124 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
125 int generation = NUM_GENERATIONS - 1;
126 Py_ssize_t _return_value;
127
128 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
129 if (!args) {
130 goto exit;
131 }
132 if (!noptargs) {
133 goto skip_optional_pos;
134 }
135 generation = PyLong_AsInt(args[0]);
136 if (generation == -1 && PyErr_Occurred()) {
137 goto exit;
138 }
139 skip_optional_pos:
140 _return_value = gc_collect_impl(module, generation);
141 if ((_return_value == -1) && PyErr_Occurred()) {
142 goto exit;
143 }
144 return_value = PyLong_FromSsize_t(_return_value);
145
146 exit:
147 return return_value;
148 }
149
150 PyDoc_STRVAR(gc_set_debug__doc__,
151 "set_debug($module, flags, /)\n"
152 "--\n"
153 "\n"
154 "Set the garbage collection debugging flags.\n"
155 "\n"
156 " flags\n"
157 " An integer that can have the following bits turned on:\n"
158 " DEBUG_STATS - Print statistics during collection.\n"
159 " DEBUG_COLLECTABLE - Print collectable objects found.\n"
160 " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects\n"
161 " found.\n"
162 " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
163 " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"
164 "\n"
165 "Debugging information is written to sys.stderr.");
166
167 #define GC_SET_DEBUG_METHODDEF \
168 {"set_debug", (PyCFunction)gc_set_debug, METH_O, gc_set_debug__doc__},
169
170 static PyObject *
171 gc_set_debug_impl(PyObject *module, int flags);
172
173 static PyObject *
gc_set_debug(PyObject * module,PyObject * arg)174 gc_set_debug(PyObject *module, PyObject *arg)
175 {
176 PyObject *return_value = NULL;
177 int flags;
178
179 flags = PyLong_AsInt(arg);
180 if (flags == -1 && PyErr_Occurred()) {
181 goto exit;
182 }
183 return_value = gc_set_debug_impl(module, flags);
184
185 exit:
186 return return_value;
187 }
188
189 PyDoc_STRVAR(gc_get_debug__doc__,
190 "get_debug($module, /)\n"
191 "--\n"
192 "\n"
193 "Get the garbage collection debugging flags.");
194
195 #define GC_GET_DEBUG_METHODDEF \
196 {"get_debug", (PyCFunction)gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
197
198 static int
199 gc_get_debug_impl(PyObject *module);
200
201 static PyObject *
gc_get_debug(PyObject * module,PyObject * Py_UNUSED (ignored))202 gc_get_debug(PyObject *module, PyObject *Py_UNUSED(ignored))
203 {
204 PyObject *return_value = NULL;
205 int _return_value;
206
207 _return_value = gc_get_debug_impl(module);
208 if ((_return_value == -1) && PyErr_Occurred()) {
209 goto exit;
210 }
211 return_value = PyLong_FromLong((long)_return_value);
212
213 exit:
214 return return_value;
215 }
216
217 PyDoc_STRVAR(gc_set_threshold__doc__,
218 "set_threshold(threshold0, [threshold1, [threshold2]])\n"
219 "Set the collection thresholds (the collection frequency).\n"
220 "\n"
221 "Setting \'threshold0\' to zero disables collection.");
222
223 #define GC_SET_THRESHOLD_METHODDEF \
224 {"set_threshold", (PyCFunction)gc_set_threshold, METH_VARARGS, gc_set_threshold__doc__},
225
226 static PyObject *
227 gc_set_threshold_impl(PyObject *module, int threshold0, int group_right_1,
228 int threshold1, int group_right_2, int threshold2);
229
230 static PyObject *
gc_set_threshold(PyObject * module,PyObject * args)231 gc_set_threshold(PyObject *module, PyObject *args)
232 {
233 PyObject *return_value = NULL;
234 int threshold0;
235 int group_right_1 = 0;
236 int threshold1 = 0;
237 int group_right_2 = 0;
238 int threshold2 = 0;
239
240 switch (PyTuple_GET_SIZE(args)) {
241 case 1:
242 if (!PyArg_ParseTuple(args, "i:set_threshold", &threshold0)) {
243 goto exit;
244 }
245 break;
246 case 2:
247 if (!PyArg_ParseTuple(args, "ii:set_threshold", &threshold0, &threshold1)) {
248 goto exit;
249 }
250 group_right_1 = 1;
251 break;
252 case 3:
253 if (!PyArg_ParseTuple(args, "iii:set_threshold", &threshold0, &threshold1, &threshold2)) {
254 goto exit;
255 }
256 group_right_1 = 1;
257 group_right_2 = 1;
258 break;
259 default:
260 PyErr_SetString(PyExc_TypeError, "gc.set_threshold requires 1 to 3 arguments");
261 goto exit;
262 }
263 return_value = gc_set_threshold_impl(module, threshold0, group_right_1, threshold1, group_right_2, threshold2);
264
265 exit:
266 return return_value;
267 }
268
269 PyDoc_STRVAR(gc_get_threshold__doc__,
270 "get_threshold($module, /)\n"
271 "--\n"
272 "\n"
273 "Return the current collection thresholds.");
274
275 #define GC_GET_THRESHOLD_METHODDEF \
276 {"get_threshold", (PyCFunction)gc_get_threshold, METH_NOARGS, gc_get_threshold__doc__},
277
278 static PyObject *
279 gc_get_threshold_impl(PyObject *module);
280
281 static PyObject *
gc_get_threshold(PyObject * module,PyObject * Py_UNUSED (ignored))282 gc_get_threshold(PyObject *module, PyObject *Py_UNUSED(ignored))
283 {
284 return gc_get_threshold_impl(module);
285 }
286
287 PyDoc_STRVAR(gc_get_count__doc__,
288 "get_count($module, /)\n"
289 "--\n"
290 "\n"
291 "Return a three-tuple of the current collection counts.");
292
293 #define GC_GET_COUNT_METHODDEF \
294 {"get_count", (PyCFunction)gc_get_count, METH_NOARGS, gc_get_count__doc__},
295
296 static PyObject *
297 gc_get_count_impl(PyObject *module);
298
299 static PyObject *
gc_get_count(PyObject * module,PyObject * Py_UNUSED (ignored))300 gc_get_count(PyObject *module, PyObject *Py_UNUSED(ignored))
301 {
302 return gc_get_count_impl(module);
303 }
304
305 PyDoc_STRVAR(gc_get_referrers__doc__,
306 "get_referrers($module, /, *objs)\n"
307 "--\n"
308 "\n"
309 "Return the list of objects that directly refer to any of \'objs\'.");
310
311 #define GC_GET_REFERRERS_METHODDEF \
312 {"get_referrers", _PyCFunction_CAST(gc_get_referrers), METH_FASTCALL, gc_get_referrers__doc__},
313
314 static PyObject *
315 gc_get_referrers_impl(PyObject *module, PyObject *args);
316
317 static PyObject *
gc_get_referrers(PyObject * module,PyObject * const * args,Py_ssize_t nargs)318 gc_get_referrers(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
319 {
320 PyObject *return_value = NULL;
321 PyObject *__clinic_args = NULL;
322
323 if (!_PyArg_CheckPositional("get_referrers", nargs, 0, PY_SSIZE_T_MAX)) {
324 goto exit;
325 }
326 __clinic_args = PyTuple_New(nargs - 0);
327 if (!__clinic_args) {
328 goto exit;
329 }
330 for (Py_ssize_t i = 0; i < nargs - 0; ++i) {
331 PyTuple_SET_ITEM(__clinic_args, i, Py_NewRef(args[0 + i]));
332 }
333 return_value = gc_get_referrers_impl(module, __clinic_args);
334
335 exit:
336 Py_XDECREF(__clinic_args);
337 return return_value;
338 }
339
340 PyDoc_STRVAR(gc_get_referents__doc__,
341 "get_referents($module, /, *objs)\n"
342 "--\n"
343 "\n"
344 "Return the list of objects that are directly referred to by \'objs\'.");
345
346 #define GC_GET_REFERENTS_METHODDEF \
347 {"get_referents", _PyCFunction_CAST(gc_get_referents), METH_FASTCALL, gc_get_referents__doc__},
348
349 static PyObject *
350 gc_get_referents_impl(PyObject *module, PyObject *args);
351
352 static PyObject *
gc_get_referents(PyObject * module,PyObject * const * args,Py_ssize_t nargs)353 gc_get_referents(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
354 {
355 PyObject *return_value = NULL;
356 PyObject *__clinic_args = NULL;
357
358 if (!_PyArg_CheckPositional("get_referents", nargs, 0, PY_SSIZE_T_MAX)) {
359 goto exit;
360 }
361 __clinic_args = PyTuple_New(nargs - 0);
362 if (!__clinic_args) {
363 goto exit;
364 }
365 for (Py_ssize_t i = 0; i < nargs - 0; ++i) {
366 PyTuple_SET_ITEM(__clinic_args, i, Py_NewRef(args[0 + i]));
367 }
368 return_value = gc_get_referents_impl(module, __clinic_args);
369
370 exit:
371 Py_XDECREF(__clinic_args);
372 return return_value;
373 }
374
375 PyDoc_STRVAR(gc_get_objects__doc__,
376 "get_objects($module, /, generation=None)\n"
377 "--\n"
378 "\n"
379 "Return a list of objects tracked by the collector (excluding the list returned).\n"
380 "\n"
381 " generation\n"
382 " Generation to extract the objects from.\n"
383 "\n"
384 "If generation is not None, return only the objects tracked by the collector\n"
385 "that are in that generation.");
386
387 #define GC_GET_OBJECTS_METHODDEF \
388 {"get_objects", _PyCFunction_CAST(gc_get_objects), METH_FASTCALL|METH_KEYWORDS, gc_get_objects__doc__},
389
390 static PyObject *
391 gc_get_objects_impl(PyObject *module, Py_ssize_t generation);
392
393 static PyObject *
gc_get_objects(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)394 gc_get_objects(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
395 {
396 PyObject *return_value = NULL;
397 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
398
399 #define NUM_KEYWORDS 1
400 static struct {
401 PyGC_Head _this_is_not_used;
402 PyObject_VAR_HEAD
403 PyObject *ob_item[NUM_KEYWORDS];
404 } _kwtuple = {
405 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
406 .ob_item = { &_Py_ID(generation), },
407 };
408 #undef NUM_KEYWORDS
409 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
410
411 #else // !Py_BUILD_CORE
412 # define KWTUPLE NULL
413 #endif // !Py_BUILD_CORE
414
415 static const char * const _keywords[] = {"generation", NULL};
416 static _PyArg_Parser _parser = {
417 .keywords = _keywords,
418 .fname = "get_objects",
419 .kwtuple = KWTUPLE,
420 };
421 #undef KWTUPLE
422 PyObject *argsbuf[1];
423 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
424 Py_ssize_t generation = -1;
425
426 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
427 if (!args) {
428 goto exit;
429 }
430 if (!noptargs) {
431 goto skip_optional_pos;
432 }
433 if (!_Py_convert_optional_to_ssize_t(args[0], &generation)) {
434 goto exit;
435 }
436 skip_optional_pos:
437 return_value = gc_get_objects_impl(module, generation);
438
439 exit:
440 return return_value;
441 }
442
443 PyDoc_STRVAR(gc_get_stats__doc__,
444 "get_stats($module, /)\n"
445 "--\n"
446 "\n"
447 "Return a list of dictionaries containing per-generation statistics.");
448
449 #define GC_GET_STATS_METHODDEF \
450 {"get_stats", (PyCFunction)gc_get_stats, METH_NOARGS, gc_get_stats__doc__},
451
452 static PyObject *
453 gc_get_stats_impl(PyObject *module);
454
455 static PyObject *
gc_get_stats(PyObject * module,PyObject * Py_UNUSED (ignored))456 gc_get_stats(PyObject *module, PyObject *Py_UNUSED(ignored))
457 {
458 return gc_get_stats_impl(module);
459 }
460
461 PyDoc_STRVAR(gc_is_tracked__doc__,
462 "is_tracked($module, obj, /)\n"
463 "--\n"
464 "\n"
465 "Returns true if the object is tracked by the garbage collector.\n"
466 "\n"
467 "Simple atomic objects will return false.");
468
469 #define GC_IS_TRACKED_METHODDEF \
470 {"is_tracked", (PyCFunction)gc_is_tracked, METH_O, gc_is_tracked__doc__},
471
472 static int
473 gc_is_tracked_impl(PyObject *module, PyObject *obj);
474
475 static PyObject *
gc_is_tracked(PyObject * module,PyObject * obj)476 gc_is_tracked(PyObject *module, PyObject *obj)
477 {
478 PyObject *return_value = NULL;
479 int _return_value;
480
481 _return_value = gc_is_tracked_impl(module, obj);
482 if ((_return_value == -1) && PyErr_Occurred()) {
483 goto exit;
484 }
485 return_value = PyBool_FromLong((long)_return_value);
486
487 exit:
488 return return_value;
489 }
490
491 PyDoc_STRVAR(gc_is_finalized__doc__,
492 "is_finalized($module, obj, /)\n"
493 "--\n"
494 "\n"
495 "Returns true if the object has been already finalized by the GC.");
496
497 #define GC_IS_FINALIZED_METHODDEF \
498 {"is_finalized", (PyCFunction)gc_is_finalized, METH_O, gc_is_finalized__doc__},
499
500 static int
501 gc_is_finalized_impl(PyObject *module, PyObject *obj);
502
503 static PyObject *
gc_is_finalized(PyObject * module,PyObject * obj)504 gc_is_finalized(PyObject *module, PyObject *obj)
505 {
506 PyObject *return_value = NULL;
507 int _return_value;
508
509 _return_value = gc_is_finalized_impl(module, obj);
510 if ((_return_value == -1) && PyErr_Occurred()) {
511 goto exit;
512 }
513 return_value = PyBool_FromLong((long)_return_value);
514
515 exit:
516 return return_value;
517 }
518
519 PyDoc_STRVAR(gc_freeze__doc__,
520 "freeze($module, /)\n"
521 "--\n"
522 "\n"
523 "Freeze all current tracked objects and ignore them for future collections.\n"
524 "\n"
525 "This can be used before a POSIX fork() call to make the gc copy-on-write friendly.\n"
526 "Note: collection before a POSIX fork() call may free pages for future allocation\n"
527 "which can cause copy-on-write.");
528
529 #define GC_FREEZE_METHODDEF \
530 {"freeze", (PyCFunction)gc_freeze, METH_NOARGS, gc_freeze__doc__},
531
532 static PyObject *
533 gc_freeze_impl(PyObject *module);
534
535 static PyObject *
gc_freeze(PyObject * module,PyObject * Py_UNUSED (ignored))536 gc_freeze(PyObject *module, PyObject *Py_UNUSED(ignored))
537 {
538 return gc_freeze_impl(module);
539 }
540
541 PyDoc_STRVAR(gc_unfreeze__doc__,
542 "unfreeze($module, /)\n"
543 "--\n"
544 "\n"
545 "Unfreeze all objects in the permanent generation.\n"
546 "\n"
547 "Put all objects in the permanent generation back into oldest generation.");
548
549 #define GC_UNFREEZE_METHODDEF \
550 {"unfreeze", (PyCFunction)gc_unfreeze, METH_NOARGS, gc_unfreeze__doc__},
551
552 static PyObject *
553 gc_unfreeze_impl(PyObject *module);
554
555 static PyObject *
gc_unfreeze(PyObject * module,PyObject * Py_UNUSED (ignored))556 gc_unfreeze(PyObject *module, PyObject *Py_UNUSED(ignored))
557 {
558 return gc_unfreeze_impl(module);
559 }
560
561 PyDoc_STRVAR(gc_get_freeze_count__doc__,
562 "get_freeze_count($module, /)\n"
563 "--\n"
564 "\n"
565 "Return the number of objects in the permanent generation.");
566
567 #define GC_GET_FREEZE_COUNT_METHODDEF \
568 {"get_freeze_count", (PyCFunction)gc_get_freeze_count, METH_NOARGS, gc_get_freeze_count__doc__},
569
570 static Py_ssize_t
571 gc_get_freeze_count_impl(PyObject *module);
572
573 static PyObject *
gc_get_freeze_count(PyObject * module,PyObject * Py_UNUSED (ignored))574 gc_get_freeze_count(PyObject *module, PyObject *Py_UNUSED(ignored))
575 {
576 PyObject *return_value = NULL;
577 Py_ssize_t _return_value;
578
579 _return_value = gc_get_freeze_count_impl(module);
580 if ((_return_value == -1) && PyErr_Occurred()) {
581 goto exit;
582 }
583 return_value = PyLong_FromSsize_t(_return_value);
584
585 exit:
586 return return_value;
587 }
588 /*[clinic end generated code: output=0a7e91917adcb937 input=a9049054013a1b77]*/
589