Coverage Report

Created: 2025-10-12 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/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)gc_set_threshold, METH_VARARGS, 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 *args)
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
    switch (PyTuple_GET_SIZE(args)) {
244
0
        case 1:
245
0
            if (!PyArg_ParseTuple(args, "i:set_threshold", &threshold0)) {
246
0
                goto exit;
247
0
            }
248
0
            break;
249
0
        case 2:
250
0
            if (!PyArg_ParseTuple(args, "ii:set_threshold", &threshold0, &threshold1)) {
251
0
                goto exit;
252
0
            }
253
0
            group_right_1 = 1;
254
0
            break;
255
0
        case 3:
256
0
            if (!PyArg_ParseTuple(args, "iii:set_threshold", &threshold0, &threshold1, &threshold2)) {
257
0
                goto exit;
258
0
            }
259
0
            group_right_1 = 1;
260
0
            group_right_2 = 1;
261
0
            break;
262
0
        default:
263
0
            PyErr_SetString(PyExc_TypeError, "gc.set_threshold requires 1 to 3 arguments");
264
0
            goto exit;
265
0
    }
266
0
    return_value = gc_set_threshold_impl(module, threshold0, group_right_1, threshold1, group_right_2, threshold2);
267
268
0
exit:
269
0
    return return_value;
270
0
}
271
272
PyDoc_STRVAR(gc_get_threshold__doc__,
273
"get_threshold($module, /)\n"
274
"--\n"
275
"\n"
276
"Return the current collection thresholds.");
277
278
#define GC_GET_THRESHOLD_METHODDEF    \
279
    {"get_threshold", (PyCFunction)gc_get_threshold, METH_NOARGS, gc_get_threshold__doc__},
280
281
static PyObject *
282
gc_get_threshold_impl(PyObject *module);
283
284
static PyObject *
285
gc_get_threshold(PyObject *module, PyObject *Py_UNUSED(ignored))
286
0
{
287
0
    return gc_get_threshold_impl(module);
288
0
}
289
290
PyDoc_STRVAR(gc_get_count__doc__,
291
"get_count($module, /)\n"
292
"--\n"
293
"\n"
294
"Return a three-tuple of the current collection counts.");
295
296
#define GC_GET_COUNT_METHODDEF    \
297
    {"get_count", (PyCFunction)gc_get_count, METH_NOARGS, gc_get_count__doc__},
298
299
static PyObject *
300
gc_get_count_impl(PyObject *module);
301
302
static PyObject *
303
gc_get_count(PyObject *module, PyObject *Py_UNUSED(ignored))
304
0
{
305
0
    return gc_get_count_impl(module);
306
0
}
307
308
PyDoc_STRVAR(gc_get_referrers__doc__,
309
"get_referrers($module, /, *objs)\n"
310
"--\n"
311
"\n"
312
"Return the list of objects that directly refer to any of \'objs\'.");
313
314
#define GC_GET_REFERRERS_METHODDEF    \
315
    {"get_referrers", _PyCFunction_CAST(gc_get_referrers), METH_FASTCALL, gc_get_referrers__doc__},
316
317
static PyObject *
318
gc_get_referrers_impl(PyObject *module, PyObject *objs);
319
320
static PyObject *
321
gc_get_referrers(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
322
0
{
323
0
    PyObject *return_value = NULL;
324
0
    PyObject *objs = NULL;
325
326
0
    objs = PyTuple_FromArray(args, nargs);
327
0
    if (objs == NULL) {
328
0
        goto exit;
329
0
    }
330
0
    return_value = gc_get_referrers_impl(module, objs);
331
332
0
exit:
333
    /* Cleanup for objs */
334
0
    Py_XDECREF(objs);
335
336
0
    return return_value;
337
0
}
338
339
PyDoc_STRVAR(gc_get_referents__doc__,
340
"get_referents($module, /, *objs)\n"
341
"--\n"
342
"\n"
343
"Return the list of objects that are directly referred to by \'objs\'.");
344
345
#define GC_GET_REFERENTS_METHODDEF    \
346
    {"get_referents", _PyCFunction_CAST(gc_get_referents), METH_FASTCALL, gc_get_referents__doc__},
347
348
static PyObject *
349
gc_get_referents_impl(PyObject *module, PyObject *objs);
350
351
static PyObject *
352
gc_get_referents(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
353
0
{
354
0
    PyObject *return_value = NULL;
355
0
    PyObject *objs = NULL;
356
357
0
    objs = PyTuple_FromArray(args, nargs);
358
0
    if (objs == NULL) {
359
0
        goto exit;
360
0
    }
361
0
    return_value = gc_get_referents_impl(module, objs);
362
363
0
exit:
364
    /* Cleanup for objs */
365
0
    Py_XDECREF(objs);
366
367
0
    return return_value;
368
0
}
369
370
PyDoc_STRVAR(gc_get_objects__doc__,
371
"get_objects($module, /, generation=None)\n"
372
"--\n"
373
"\n"
374
"Return a list of objects tracked by the collector (excluding the list returned).\n"
375
"\n"
376
"  generation\n"
377
"    Generation to extract the objects from.\n"
378
"\n"
379
"If generation is not None, return only the objects tracked by the collector\n"
380
"that are in that generation.");
381
382
#define GC_GET_OBJECTS_METHODDEF    \
383
    {"get_objects", _PyCFunction_CAST(gc_get_objects), METH_FASTCALL|METH_KEYWORDS, gc_get_objects__doc__},
384
385
static PyObject *
386
gc_get_objects_impl(PyObject *module, Py_ssize_t generation);
387
388
static PyObject *
389
gc_get_objects(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
390
0
{
391
0
    PyObject *return_value = NULL;
392
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
393
394
0
    #define NUM_KEYWORDS 1
395
0
    static struct {
396
0
        PyGC_Head _this_is_not_used;
397
0
        PyObject_VAR_HEAD
398
0
        Py_hash_t ob_hash;
399
0
        PyObject *ob_item[NUM_KEYWORDS];
400
0
    } _kwtuple = {
401
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
402
0
        .ob_hash = -1,
403
0
        .ob_item = { &_Py_ID(generation), },
404
0
    };
405
0
    #undef NUM_KEYWORDS
406
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
407
408
    #else  // !Py_BUILD_CORE
409
    #  define KWTUPLE NULL
410
    #endif  // !Py_BUILD_CORE
411
412
0
    static const char * const _keywords[] = {"generation", NULL};
413
0
    static _PyArg_Parser _parser = {
414
0
        .keywords = _keywords,
415
0
        .fname = "get_objects",
416
0
        .kwtuple = KWTUPLE,
417
0
    };
418
0
    #undef KWTUPLE
419
0
    PyObject *argsbuf[1];
420
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
421
0
    Py_ssize_t generation = -1;
422
423
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
424
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
425
0
    if (!args) {
426
0
        goto exit;
427
0
    }
428
0
    if (!noptargs) {
429
0
        goto skip_optional_pos;
430
0
    }
431
0
    if (!_Py_convert_optional_to_ssize_t(args[0], &generation)) {
432
0
        goto exit;
433
0
    }
434
0
skip_optional_pos:
435
0
    return_value = gc_get_objects_impl(module, generation);
436
437
0
exit:
438
0
    return return_value;
439
0
}
440
441
PyDoc_STRVAR(gc_get_stats__doc__,
442
"get_stats($module, /)\n"
443
"--\n"
444
"\n"
445
"Return a list of dictionaries containing per-generation statistics.");
446
447
#define GC_GET_STATS_METHODDEF    \
448
    {"get_stats", (PyCFunction)gc_get_stats, METH_NOARGS, gc_get_stats__doc__},
449
450
static PyObject *
451
gc_get_stats_impl(PyObject *module);
452
453
static PyObject *
454
gc_get_stats(PyObject *module, PyObject *Py_UNUSED(ignored))
455
0
{
456
0
    return gc_get_stats_impl(module);
457
0
}
458
459
PyDoc_STRVAR(gc_is_tracked__doc__,
460
"is_tracked($module, obj, /)\n"
461
"--\n"
462
"\n"
463
"Returns true if the object is tracked by the garbage collector.\n"
464
"\n"
465
"Simple atomic objects will return false.");
466
467
#define GC_IS_TRACKED_METHODDEF    \
468
    {"is_tracked", (PyCFunction)gc_is_tracked, METH_O, gc_is_tracked__doc__},
469
470
static int
471
gc_is_tracked_impl(PyObject *module, PyObject *obj);
472
473
static PyObject *
474
gc_is_tracked(PyObject *module, PyObject *obj)
475
0
{
476
0
    PyObject *return_value = NULL;
477
0
    int _return_value;
478
479
0
    _return_value = gc_is_tracked_impl(module, obj);
480
0
    if ((_return_value == -1) && PyErr_Occurred()) {
481
0
        goto exit;
482
0
    }
483
0
    return_value = PyBool_FromLong((long)_return_value);
484
485
0
exit:
486
0
    return return_value;
487
0
}
488
489
PyDoc_STRVAR(gc_is_finalized__doc__,
490
"is_finalized($module, obj, /)\n"
491
"--\n"
492
"\n"
493
"Returns true if the object has been already finalized by the GC.");
494
495
#define GC_IS_FINALIZED_METHODDEF    \
496
    {"is_finalized", (PyCFunction)gc_is_finalized, METH_O, gc_is_finalized__doc__},
497
498
static int
499
gc_is_finalized_impl(PyObject *module, PyObject *obj);
500
501
static PyObject *
502
gc_is_finalized(PyObject *module, PyObject *obj)
503
0
{
504
0
    PyObject *return_value = NULL;
505
0
    int _return_value;
506
507
0
    _return_value = gc_is_finalized_impl(module, obj);
508
0
    if ((_return_value == -1) && PyErr_Occurred()) {
509
0
        goto exit;
510
0
    }
511
0
    return_value = PyBool_FromLong((long)_return_value);
512
513
0
exit:
514
0
    return return_value;
515
0
}
516
517
PyDoc_STRVAR(gc_freeze__doc__,
518
"freeze($module, /)\n"
519
"--\n"
520
"\n"
521
"Freeze all current tracked objects and ignore them for future collections.\n"
522
"\n"
523
"This can be used before a POSIX fork() call to make the gc copy-on-write friendly.\n"
524
"Note: collection before a POSIX fork() call may free pages for future allocation\n"
525
"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=19738854607938db input=a9049054013a1b77]*/