Coverage Report

Created: 2025-07-04 06:49

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