]> granicus.if.org Git - python/commitdiff
Don't run garbage collection on interpreter exit if it was explicitly disabled
authorŁukasz Langa <lukasz@langa.pl>
Sat, 10 Sep 2016 04:47:46 +0000 (21:47 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Sat, 10 Sep 2016 04:47:46 +0000 (21:47 -0700)
by the user.

Include/objimpl.h
Modules/gcmodule.c
Python/pylifecycle.c

index 65b6d91c36ca6750e7276de0243d08cf4a817cb0..519ae516053c356caac84abc9a9679f6f7f3a449 100644 (file)
@@ -224,11 +224,12 @@ PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
  * ==========================
  */
 
-/* C equivalent of gc.collect(). */
+/* C equivalent of gc.collect() which ignores the state of gc.enabled. */
 PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
 
 #ifndef Py_LIMITED_API
 PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
+PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
 #endif
 
 /* Test if a type has a GC head */
index 2575d96d71ca602e2d93383feb4cc086a286e977..754348e20a92d0b9e08c35ace72e784d53eebcda 100644 (file)
@@ -1596,6 +1596,15 @@ PyGC_Collect(void)
     return n;
 }
 
+Py_ssize_t
+_PyGC_CollectIfEnabled(void)
+{
+    if (!enabled)
+        return 0;
+
+    return PyGC_Collect();
+}
+
 Py_ssize_t
 _PyGC_CollectNoFail(void)
 {
index f93afd234da77455328f801e65cf068cae53e12f..5b5cc2b55e8ef8dd652a55728965d5d7f0849929 100644 (file)
@@ -600,12 +600,12 @@ Py_FinalizeEx(void)
      * XXX but I'm unclear on exactly how that one happens.  In any case,
      * XXX I haven't seen a real-life report of either of these.
      */
-    PyGC_Collect();
+    _PyGC_CollectIfEnabled();
 #ifdef COUNT_ALLOCS
     /* With COUNT_ALLOCS, it helps to run GC multiple times:
        each collection might release some types from the type
        list, so they become garbage. */
-    while (PyGC_Collect() > 0)
+    while (_PyGC_CollectIfEnabled() > 0)
         /* nothing */;
 #endif
     /* Destroy all modules */
@@ -632,7 +632,7 @@ Py_FinalizeEx(void)
      * XXX Python code getting called.
      */
 #if 0
-    PyGC_Collect();
+    _PyGC_CollectIfEnabled();
 #endif
 
     /* Disable tracemalloc after all Python objects have been destroyed,