Skip to content

gh-92031: Improve test for unquickening static code #92440

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

Merged
merged 3 commits into from
May 11, 2022
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
33 changes: 24 additions & 9 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,34 @@ def test_finalize_structseq(self):
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)

@support.skip_if_pgo_task
def test_quickened_static_code_gets_unquickened_at_Py_FINALIZE(self):
# https://github.com/python/cpython/issues/92031
code = """if 1:
from importlib._bootstrap import _handle_fromlist
import dis
for name in dis.opmap:
# quicken this frozen code object.
_handle_fromlist(dis, [name], lambda *args: None)
from dis import _all_opmap
resume = _all_opmap["RESUME"]
resume_quick = _all_opmap["RESUME_QUICK"]
Copy link
Member

Choose a reason for hiding this comment

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

Why not putting this code inside the code string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since this is testing parts of importlib, I'd like to use as little import machinery as possible, so there's no chance that imports prematurely specialize the _handle_fromlist.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment to explain it, since it's not obvious the first time that I read the code: the code starts with "Assert initially unquickened".

code = f"""if 1:
import importlib._bootstrap
func = importlib._bootstrap._handle_fromlist
code = func.__code__

# Assert initially unquickened.
# Use sets to account for byte order.
if set(code._co_code_adaptive[:2]) != set([{resume}, 0]):
raise AssertionError()

# Warmup
for i in range(30):
func(importlib._bootstrap, ["x"], lambda *args: None)

# Assert quickening worked
if set(code._co_code_adaptive[:2]) != set([{resume_quick}, 0]):
raise AssertionError()

print("Tests passed")
"""
run = self.run_embedded_interpreter
for i in range(50):
out, err = run("test_repeated_init_exec", code, timeout=60)
out, err = run("test_repeated_init_exec", code)
self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)

def test_ucnhash_capi_reset(self):
# bpo-47182: unicodeobject.c:ucnhash_capi was not reset on shutdown.
Expand Down