Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9d20c3

Browse files
author
Vladimir Marangozov
committed
Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Small stylistic changes by VM: - is_enabled() -> isenabled() - static ... Py_<func> -> static ... gc_<func>
1 parent 5bcb215 commit f9d20c3

3 files changed

Lines changed: 107 additions & 23 deletions

File tree

Doc/lib/libgc.tex

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@ \section{\module{gc} ---
55
\moduleauthor{Neil Schemenauer}{[email protected]}
66
\sectionauthor{Neil Schemenauer}{[email protected]}
77

8-
This module provides an interface to the optional garbage collector.
9-
It provides the ability to disable the collector, tune the collection
8+
This module provides an interface to the optional garbage collector. It
9+
provides the ability to disable the collector, tune the collection
1010
frequency, and set debugging options. It also provides access to
11-
unreachable objects that the collector found but cannot free. Since
12-
the collector supplements the reference counting already used in
13-
Python, you can disable the collector if you are sure your program
14-
does not create reference cycles. The collector can be disabled by
15-
calling \code{gc.set_threshold(0)}. To debug a leaking program call
11+
unreachable objects that the collector found but cannot free. Since the
12+
collector supplements the reference counting already used in Python, you
13+
can disable the collector if you are sure your program does not create
14+
reference cycles. Automatic collection can be disabled by calling
15+
\code{gc.disable()}. To debug a leaking program call
1616
\code{gc.set_debug(gc.DEBUG_LEAK)}.
1717

1818
The \module{gc} module provides the following functions:
1919

20+
\begin{funcdesc}{enable}{}
21+
Enable automatic garbage collection.
22+
\end{funcdesc}
23+
24+
\begin{funcdesc}{disable}{}
25+
Disable automatic garbage collection.
26+
\end{funcdesc}
27+
28+
\begin{funcdesc}{isenabled}{}
29+
Returns true if automatic collection is enabled.
30+
\end{funcdesc}
31+
2032
\begin{funcdesc}{collect}{}
2133
Run a full collection. All generations are examined and the
2234
number of unreachable objects found is returned.

Lib/test/test_gc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def test_function():
7575

7676

7777
def test_all():
78+
79+
enabled = gc.isenabled()
80+
gc.disable()
81+
assert not gc.isenabled()
82+
7883
test_list()
7984
test_dict()
8085
test_tuple()
@@ -84,4 +89,11 @@ def test_all():
8489
test_finalizer()
8590
test_function()
8691

92+
# test gc.enable() even if GC is disabled by default
93+
gc.enable()
94+
assert gc.isenabled()
95+
if not enabled:
96+
gc.disable()
97+
98+
8799
test_all()

Modules/gcmodule.c

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static PyGC_Head generation2 = {&generation2, &generation2, 0};
3939
static int generation = 0; /* current generation being collected */
4040

4141
/* collection frequencies, XXX tune these */
42+
static int enabled = 1; /* automatic collection enabled? */
4243
static int threshold0 = 100; /* net new containers before collection */
4344
static int threshold1 = 10; /* generation0 collections before collecting 1 */
4445
static int threshold2 = 10; /* generation1 collections before collecting 2 */
@@ -492,7 +493,7 @@ _PyGC_Insert(PyObject *op)
492493
abort();
493494
}
494495
#endif
495-
if (threshold0 && allocated > threshold0 && !collecting) {
496+
if (allocated > threshold0 && enabled && threshold0 && !collecting) {
496497
collecting++;
497498
collect_generations();
498499
collecting--;
@@ -516,15 +517,68 @@ _PyGC_Remove(PyObject *op)
516517
}
517518
}
518519

520+
static char gc_enable__doc__[] =
521+
"enable() -> None\n"
522+
"\n"
523+
"Enable automatic garbage collection.\n"
524+
;
525+
526+
static PyObject *
527+
gc_enable(PyObject *self, PyObject *args)
528+
{
529+
530+
if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
531+
return NULL;
532+
533+
enabled = 1;
534+
535+
Py_INCREF(Py_None);
536+
return Py_None;
537+
}
538+
539+
static char gc_disable__doc__[] =
540+
"disable() -> None\n"
541+
"\n"
542+
"Disable automatic garbage collection.\n"
543+
;
544+
545+
static PyObject *
546+
gc_disable(PyObject *self, PyObject *args)
547+
{
548+
549+
if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
550+
return NULL;
551+
552+
enabled = 0;
553+
554+
Py_INCREF(Py_None);
555+
return Py_None;
556+
}
557+
558+
static char gc_isenabled__doc__[] =
559+
"isenabled() -> status\n"
560+
"\n"
561+
"Returns true if automatic garbage collection is enabled.\n"
562+
;
563+
564+
static PyObject *
565+
gc_isenabled(PyObject *self, PyObject *args)
566+
{
567+
568+
if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
569+
return NULL;
570+
571+
return Py_BuildValue("i", enabled);
572+
}
519573

520-
static char collect__doc__[] =
574+
static char gc_collect__doc__[] =
521575
"collect() -> n\n"
522576
"\n"
523577
"Run a full collection. The number of unreachable objects is returned.\n"
524578
;
525579

526580
static PyObject *
527-
Py_collect(PyObject *self, PyObject *args)
581+
gc_collect(PyObject *self, PyObject *args)
528582
{
529583
long n;
530584

@@ -539,7 +593,7 @@ Py_collect(PyObject *self, PyObject *args)
539593
return Py_BuildValue("i", n);
540594
}
541595

542-
static char set_debug__doc__[] =
596+
static char gc_set_debug__doc__[] =
543597
"set_debug(flags) -> None\n"
544598
"\n"
545599
"Set the garbage collection debugging flags. Debugging information is\n"
@@ -556,7 +610,7 @@ static char set_debug__doc__[] =
556610
;
557611

558612
static PyObject *
559-
Py_set_debug(PyObject *self, PyObject *args)
613+
gc_set_debug(PyObject *self, PyObject *args)
560614
{
561615
if (!PyArg_ParseTuple(args, "l:get_debug", &debug))
562616
return NULL;
@@ -565,30 +619,30 @@ Py_set_debug(PyObject *self, PyObject *args)
565619
return Py_None;
566620
}
567621

568-
static char get_debug__doc__[] =
622+
static char gc_get_debug__doc__[] =
569623
"get_debug() -> flags\n"
570624
"\n"
571625
"Get the garbage collection debugging flags.\n"
572626
;
573627

574628
static PyObject *
575-
Py_get_debug(PyObject *self, PyObject *args)
629+
gc_get_debug(PyObject *self, PyObject *args)
576630
{
577631
if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
578632
return NULL;
579633

580634
return Py_BuildValue("i", debug);
581635
}
582636

583-
static char set_thresh__doc__[] =
637+
static char gc_set_thresh__doc__[] =
584638
"set_threshold(threshold0, [threhold1, threshold2]) -> None\n"
585639
"\n"
586640
"Sets the collection thresholds. Setting threshold0 to zero disables\n"
587641
"collection.\n"
588642
;
589643

590644
static PyObject *
591-
Py_set_thresh(PyObject *self, PyObject *args)
645+
gc_set_thresh(PyObject *self, PyObject *args)
592646
{
593647
if (!PyArg_ParseTuple(args, "i|ii:set_threshold", &threshold0,
594648
&threshold1, &threshold2))
@@ -598,14 +652,14 @@ Py_set_thresh(PyObject *self, PyObject *args)
598652
return Py_None;
599653
}
600654

601-
static char get_thresh__doc__[] =
655+
static char gc_get_thresh__doc__[] =
602656
"get_threshold() -> (threshold0, threshold1, threshold2)\n"
603657
"\n"
604658
"Return the current collection thresholds\n"
605659
;
606660

607661
static PyObject *
608-
Py_get_thresh(PyObject *self, PyObject *args)
662+
gc_get_thresh(PyObject *self, PyObject *args)
609663
{
610664
if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
611665
return NULL;
@@ -617,6 +671,9 @@ Py_get_thresh(PyObject *self, PyObject *args)
617671
static char gc__doc__ [] =
618672
"This module provides access to the garbage collector for reference cycles.\n"
619673
"\n"
674+
"enable() -- Enable automatic garbage collection.\n"
675+
"disable() -- Disable automatic garbage collection.\n"
676+
"isenabled() -- Returns true if automatic collection is enabled.\n"
620677
"collect() -- Do a full collection right now.\n"
621678
"set_debug() -- Set debugging flags.\n"
622679
"get_debug() -- Get debugging flags.\n"
@@ -625,11 +682,14 @@ static char gc__doc__ [] =
625682
;
626683

627684
static PyMethodDef GcMethods[] = {
628-
{"set_debug", Py_set_debug, METH_VARARGS, set_debug__doc__},
629-
{"get_debug", Py_get_debug, METH_VARARGS, get_debug__doc__},
630-
{"set_threshold", Py_set_thresh, METH_VARARGS, set_thresh__doc__},
631-
{"get_threshold", Py_get_thresh, METH_VARARGS, get_thresh__doc__},
632-
{"collect", Py_collect, METH_VARARGS, collect__doc__},
685+
{"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
686+
{"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
687+
{"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
688+
{"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
689+
{"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
690+
{"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
691+
{"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
692+
{"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
633693
{NULL, NULL} /* Sentinel */
634694
};
635695

0 commit comments

Comments
 (0)