Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-93353: Add test.support.late_deletion() #93774

Merged
merged 3 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
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
Next Next commit
gh-93353: Add test.support.late_deletion()
  • Loading branch information
vstinner committed Jun 13, 2022
commit 0355bea8c6fa5ba392ec8a1c6cce51f2e3a8d32e
35 changes: 35 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2213,3 +2213,38 @@ def requires_venv_with_pip():
# True if Python is built with the Py_DEBUG macro defined: if
# Python is built in debug mode (./configure --with-pydebug).
Py_DEBUG = hasattr(sys, 'gettotalrefcount')


def late_deletion(obj):
"""
Keep a Python alive as long as possible.

Create a reference cycle and store the cycle in an object deleted late in
Python finalization. Try to keep the object alive until the very last
garbage collection.

The function keeps a strong reference by design. It should be called in a
subprocess to not mark a test as "leaking a reference".
"""

# Late CPython finalization:
# - finalize_interp_clear()
# - _PyInterpreterState_Clear()
# - clear os.register_at_fork() callbacks
# - clear codecs.register() callbacks

ref_cycle = [obj]
ref_cycle.append(ref_cycle)

# PyInterpreterState.codec_search_path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would like a bit text here explaining that codec is one of the last modules cleared, but no biggie if you don't care.

Otherwise, LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rephased the doc. Is it better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes :)

import codecs
def search_func(encoding):
return None
search_func.reference = ref_cycle
codecs.register(search_func)

# PyInterpreterState.before_forkers
def atfork_func():
pass
atfork_func.reference = ref_cycle
os.register_at_fork(before=atfork_func)
14 changes: 4 additions & 10 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,19 +1440,13 @@ def test_ast_fini(self):
code = textwrap.dedent("""
import ast
import codecs
from test import support

# 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.
# Codec search functions are only cleared at the end of
# interpreter_clear().
def search_func(encoding):
return None
search_func.a = x
codecs.register(search_func)

# Store the tree somewhere to survive until the last GC collection
support.late_deletion(tree)
""")
assert_python_ok("-c", code)

Expand Down