8000 gh-116604: Correctly honor the gc status when calling _Py_RunGC · python/cpython@7e1973c · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e1973c

Browse files
committed
gh-116604: Correctly honor the gc status when calling _Py_RunGC
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
1 parent 3e45030 commit 7e1973c

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Lib/test/test_gc.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,31 @@ def __del__(self):
13971397
# empty __dict__.
13981398
self.assertEqual(x, None)
13991399

1400+
def test_indirect_calls_with_gc_disabled(self):
1401+
junk = []
1402+
i = 0
1403+
detector = GC_Detector()
1404+
while not detector.gc_happened:
1405+
i += 1
1406+
if i > 10000:
1407+
self.fail("gc didn't happen after 10000 iterations")
1408+
junk.append([]) # this will eventually trigger gc
1409+
1410+
try:
1411+
gc.disable()
1412+
junk = []
1413+
i = 0
1414+
detector = GC_Detector()
1415+
while not detector.gc_happened:
1416+
i += 1
1417+
if i > 10000:
1418+
break
1419+
junk.append([]) # this may eventually trigger gc (if it is enabled)
1420+
1421+
self.assertEqual(i, 10001)
1422+
finally:
1423+
gc.enable()
1424+
14001425

14011426
class PythonFinalizationTests(unittest.TestCase):
14021427
def test_ast_fini(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Respect the status of the garbage collector when indirect calls are made via
2+
:c:func:`PyErr_CheckSignals` and the evaluation breaker. Patch by Pablo
3+
Galindo

Python/gc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,10 @@ _PyObject_GC_Link(PyObject *op)
18051805
void
18061806
_Py_RunGC(PyThreadState *tstate)
18071807
{
1808+
GCState *gcstate = get_gc_state();
1809+
if (!gcstate->enabled) {
1810+
return;
1811+
}
18081812
gc_collect_main(tstate, GENERATION_AUTO, _Py_GC_REASON_HEAP);
18091813
}
18101814

0 commit comments

Comments
 (0)
0