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

Skip to content
< 8000 h1 class="Box-sc-g0xbh4-0 bmcJak prc-PageHeader-Title-LKOsd f2 prc-Heading-Heading-6CmGO" data-component="PH_Title" data-hidden="false">Commit a887437
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 a887437

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Lib/test/test_gc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,28 @@ 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+
gc.disable()
1411+
junk = []
1412+
i = 0
1413+
detector = GC_Detector()
1414+
while not detector.gc_happened:
1415+
i += 1
1416+
if i > 10000:
1417+
break
1418+
junk.append([]) # this will eventually trigger gc
1419+
1420+
self.assertEqual(i, 10001)
1421+
14001422

14011423
class PythonFinalizationTests(unittest.TestCase):
14021424
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