Skip to content

Commit

Permalink
pythongh-116604: Correctly honor the gc status when calling _Py_RunGC (
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal authored and adorilson committed Mar 25, 2024
1 parent 1545be2 commit dae8f5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Lib/test/test_gc.py
Expand Up @@ -1397,6 +1397,31 @@ def __del__(self):
# 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):
Expand Down
@@ -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
4 changes: 4 additions & 0 deletions Python/gc.c
Expand Up @@ -1805,6 +1805,10 @@ _PyObject_GC_Link(PyObject *op)
void
_Py_RunGC(PyThreadState *tstate)
{
GCState *gcstate = get_gc_state();
if (!gcstate->enabled) {
return;
}
gc_collect_main(tstate, GENERATION_AUTO, _Py_GC_REASON_HEAP);
}

Expand Down

0 comments on commit dae8f5a

Please sign in to comment.