gh-112529: Implement GC for free-threaded builds (#114262)

* gh-112529: Implement GC for free-threaded builds

This implements a mark and sweep GC for the free-threaded builds of
CPython. The implementation relies on mimalloc to find GC tracked
objects (i.e., "containers").
This commit is contained in:
Sam Gross 2024-01-25 13:27:36 -05:00 committed by GitHub
parent 4850410b60
commit b52fc70d1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 1952 additions and 22 deletions

View file

@ -1,7 +1,7 @@
import unittest
import unittest.mock
from test.support import (verbose, refcount_test,
cpython_only, requires_subprocess)
cpython_only, requires_subprocess, Py_GIL_DISABLED)
from test.support.import_helper import import_module
from test.support.os_helper import temp_dir, TESTFN, unlink
from test.support.script_helper import assert_python_ok, make_script
@ -815,6 +815,15 @@ class GCTests(unittest.TestCase):
self.assertEqual(gc.get_freeze_count(), 0)
def test_get_objects(self):
gc.collect()
l = []
l.append(l)
self.assertTrue(
any(l is element for element in gc.get_objects())
)
@unittest.skipIf(Py_GIL_DISABLED, 'need generational GC')
def test_get_objects_generations(self):
gc.collect()
l = []
l.append(l)
@ -1225,7 +1234,7 @@ class GCCallbackTests(unittest.TestCase):
p.stderr.close()
# Verify that stderr has a useful error message:
self.assertRegex(stderr,
br'gc\.c:[0-9]+: gc_decref: Assertion "gc_get_refs\(g\) > 0" failed.')
br'gc.*\.c:[0-9]+: .*: Assertion "gc_get_refs\(.+\) .*" failed.')
self.assertRegex(stderr,
br'refcount is too small')
# "address : 0x7fb5062efc18"