Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add an unit test
  • Loading branch information
vstinner committed May 21, 2021
commit 454e996d221aaa73bdaae217e0a59684fe77bdee
36 changes: 35 additions & 1 deletion Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,36 @@ def __del__(self):
# empty __dict__.
self.assertEqual(x, None)


class PythonFinalizationTests(unittest.TestCase):
def test_ast_fini(self):
# bpo-44184: Regression test for subtype_dealloc() when deallocating
# an AST instance also destroy its AST type: subtype_dealloc() must
# not access the type memory after deallocating the instance, since
# the type memory can be freed as well. The test is also related to
# _PyAST_Fini() which clears references to AST types.
code = textwrap.dedent("""
import ast
import builtins
import sys
import os

# Small AST tree to keep their AST types alive
tree = ast.parse("def f(x, y): return 2*x-y")
x = [tree]
x.append(x)

# Put the cycle somewhere to survive until the last GC collection.
# Fork callbacks are only cleared at the end of
# interpreter_clear().
def callback():
pass
callback.a = x
os.register_at_fork(after_in_child=callback)
""")
assert_python_ok("-c", code)


def test_main():
enabled = gc.isenabled()
gc.disable()
Expand All @@ -1370,7 +1400,11 @@ def test_main():

try:
gc.collect() # Delete 2nd generation garbage
run_unittest(GCTests, GCTogglingTests, GCCallbackTests)
run_unittest(
GCTests,
GCCallbackTests,
GCTogglingTests,
PythonFinalizationTests)
finally:
gc.set_debug(debug)
# test gc.enable() even if GC is disabled by default
Expand Down