Coverage Report

Created: 2026-09-03 06:36

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