]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-116604: Correctly honor the gc status when calling _Py_RunGC (GH-116628...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Tue, 12 Mar 2024 23:38:20 +0000 (23:38 +0000)
committerGitHub <noreply@github.com>
Tue, 12 Mar 2024 23:38:20 +0000 (23:38 +0000)
Lib/test/test_gc.py
Misc/NEWS.d/next/Core and Builtins/2024-03-11-22-24-59.gh-issue-116604.LCEzAT.rst [new file with mode: 0644]
Modules/gcmodule.c

index db7cb9ace6e5f35850a15347b04a3d4668f763fe..81bb5bb288ef311497897715c1a2613b317c0fa2 100644 (file)
@@ -1387,6 +1387,31 @@ class GCTogglingTests(unittest.TestCase):
             # empty __dict__.
             self.assertEqual(x, None)
 
+    def test_indirect_calls_with_gc_disabled(self):
+        junk = []
+        i = 0
+        detector = GC_Detector()
+        while not detector.gc_happened:
+            i += 1
+            if i > 10000:
+                self.fail("gc didn't happen after 10000 iterations")
+            junk.append([])  # this will eventually trigger gc
+
+        try:
+            gc.disable()
+            junk = []
+            i = 0
+            detector = GC_Detector()
+            while not detector.gc_happened:
+                i += 1
+                if i > 10000:
+                    break
+                junk.append([])  # this may eventually trigger gc (if it is enabled)
+
+            self.assertEqual(i, 10001)
+        finally:
+            gc.enable()
+
 
 class PythonFinalizationTests(unittest.TestCase):
     def test_ast_fini(self):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-03-11-22-24-59.gh-issue-116604.LCEzAT.rst b/Misc/NEWS.d/next/Core and Builtins/2024-03-11-22-24-59.gh-issue-116604.LCEzAT.rst
new file mode 100644 (file)
index 0000000..516edfa
--- /dev/null
@@ -0,0 +1,3 @@
+Respect the status of the garbage collector when indirect calls are made via
+:c:func:`PyErr_CheckSignals` and the evaluation breaker. Patch by Pablo
+Galindo
index 149a6a022d08cea9bbfca4c910f005f5a3d75c17..c9b5aab8598f177863f54a5f62b19cd5d6ca2f2d 100644 (file)
@@ -2288,6 +2288,9 @@ void
 _Py_RunGC(PyThreadState *tstate)
 {
     GCState *gcstate = &tstate->interp->gc;
+    if (!gcstate->enabled) {
+        return;
+    } 
     gcstate->collecting = 1;
     gc_collect_generations(tstate);
     gcstate->collecting = 0;