@@ -39,6 +39,7 @@ static PyGC_Head generation2 = {&generation2, &generation2, 0};
3939static int generation = 0 ; /* current generation being collected */
4040
4141/* collection frequencies, XXX tune these */
42+ static int enabled = 1 ; /* automatic collection enabled? */
4243static int threshold0 = 100 ; /* net new containers before collection */
4344static int threshold1 = 10 ; /* generation0 collections before collecting 1 */
4445static 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
526580static 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
558612static 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
574628static 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
590644static 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
607661static 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)
617671static 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
627684static 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