]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-141536: Fix a crash when running test_capi *after* test_code in the same process...
authorT. Wouters <thomas@python.org>
Wed, 7 Jan 2026 15:40:33 +0000 (16:40 +0100)
committerGitHub <noreply@github.com>
Wed, 7 Jan 2026 15:40:33 +0000 (16:40 +0100)
Lib/test/test_code.py

index 655f5a9be7fa3181705e5a4417fea288cac62bed..0d5c6e6e77f5d799a7efe7ce01c1b2911f3100cf 100644 (file)
@@ -210,7 +210,7 @@ except ImportError:
     ctypes = None
 from test.support import (cpython_only,
                           check_impl_detail, requires_debug_ranges,
-                          gc_collect, Py_GIL_DISABLED)
+                          gc_collect, Py_GIL_DISABLED, late_deletion)
 from test.support.script_helper import assert_python_ok
 from test.support import threading_helper, import_helper
 from test.support.bytecode_helper import instructions_with_positions
@@ -1555,6 +1555,11 @@ if check_impl_detail(cpython=True) and ctypes is not None:
 
     FREE_FUNC = freefunc(myfree)
     FREE_INDEX = RequestCodeExtraIndex(FREE_FUNC)
+    # Make sure myfree sticks around at least as long as the interpreter,
+    # since we (currently) can't unregister the function and leaving a
+    # dangling pointer will cause a crash on deallocation of code objects if
+    # something else uses co_extras, like test_capi.test_misc.
+    late_deletion(myfree)
 
     class CoExtra(unittest.TestCase):
         def get_func(self):