[Python-checkins] cpython: Don't run garbage collection on interpreter exit if it was explicitly disabled

lukasz.langa python-checkins at python.org
Sat Sep 10 00:49:00 EDT 2016


https://hg.python.org/cpython/rev/5c7eb6da72a3
changeset:   103550:5c7eb6da72a3
user:        Łukasz Langa <lukasz at langa.pl>
date:        Fri Sep 09 21:47:46 2016 -0700
summary:
  Don't run garbage collection on interpreter exit if it was explicitly disabled
by the user.

files:
  Include/objimpl.h    |  3 ++-
  Modules/gcmodule.c   |  9 +++++++++
  Python/pylifecycle.c |  6 +++---
  3 files changed, 14 insertions(+), 4 deletions(-)


diff --git a/Include/objimpl.h b/Include/objimpl.h
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -224,11 +224,12 @@
  * ==========================
  */
 
-/* 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 */
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1597,6 +1597,15 @@
 }
 
 Py_ssize_t
+_PyGC_CollectIfEnabled(void)
+{
+    if (!enabled)
+        return 0;
+
+    return PyGC_Collect();
+}
+
+Py_ssize_t
 _PyGC_CollectNoFail(void)
 {
     Py_ssize_t n;
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -600,12 +600,12 @@
      * 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 @@
      * XXX Python code getting called.
      */
 #if 0
-    PyGC_Collect();
+    _PyGC_CollectIfEnabled();
 #endif
 
     /* Disable tracemalloc after all Python objects have been destroyed,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list