summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2024-03-12 23:38:20 (GMT)
committerGitHub <noreply@github.com>2024-03-12 23:38:20 (GMT)
commitdcfb21d8b4c930c4295cd9a75814316e5b103683 (patch)
tree4a13d74cfee81fd202df627021f77939d46f8935
parentf9d1ec8e803012a521f0673da8c5cba1e9862bf5 (diff)
downloadcpython-dcfb21d8b4c930c4295cd9a75814316e5b103683.zip
cpython-dcfb21d8b4c930c4295cd9a75814316e5b103683.tar.gz
cpython-dcfb21d8b4c930c4295cd9a75814316e5b103683.tar.bz2
[3.12] gh-116604: Correctly honor the gc status when calling _Py_RunGC (GH-116628) (#116653)
-rw-r--r--Lib/test/test_gc.py25
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2024-03-11-22-24-59.gh-issue-116604.LCEzAT.rst3
-rw-r--r--Modules/gcmodule.c3
3 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index db7cb9ac..81bb5bb 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -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
index 0000000..516edfa
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2024-03-11-22-24-59.gh-issue-116604.LCEzAT.rst
@@ -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
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 149a6a0..c9b5aab 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -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;