Skip to content

Commit 8b19275

Browse files
committed
Fix a crash when running test_capi *after* test_code in the same process.
1 parent a486d45 commit 8b19275

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Lib/test/test_code.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,13 @@ def myfree(ptr):
15551555

15561556
FREE_FUNC = freefunc(myfree)
15571557
FREE_INDEX = RequestCodeExtraIndex(FREE_FUNC)
1558+
# Make sure myfree sticks around at least as long as the interpreter,
1559+
# since we (currently) can't unregister the function and leaving a
1560+
# dangling pointer will cause a crash on deallocation of code objects if
1561+
# something else uses co_extras, like test_capi.test_misc. (Maybe this
1562+
# should use PyInterpreterState_GetDict, but that isn't easily exposed
1563+
# to Python code.)
1564+
setattr(sys, f'_test_code.{myfree!r}', myfree)
15581565

15591566
class CoExtra(unittest.TestCase):
15601567
def get_func(self):

0 commit comments

Comments
 (0)