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-98686: Quicken everything #98687

Merged
merged 12 commits into from
Nov 2, 2022
Prev Previous commit
Next Next commit
Fix test_embed
  • Loading branch information
brandtbucher committed Oct 15, 2022
commit f9f9c3ac4aa9265d958267d5fcf6f282f08a9049
45 changes: 27 additions & 18 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,32 +340,41 @@ def test_finalize_structseq(self):
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)

def test_quickened_static_code_gets_unquickened_at_Py_FINALIZE(self):
def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self):
# https://github.com/python/cpython/issues/92031

# Do these imports outside of the code string to avoid using
# importlib too much from within the code string, so that
# _handle_fromlist doesn't get quickened until we intend it to.
from dis import _all_opmap
resume = _all_opmap["RESUME"]
from test.test_dis import QUICKENING_WARMUP_DELAY

code = textwrap.dedent(f"""\
code = textwrap.dedent("""\
import dis
import importlib._bootstrap
import opcode
import test.test_dis

def is_specialized(f):
for instruction in dis.get_instructions(f, adaptive=True):
opname = instruction.opname
if (
opname in opcode._specialized_instructions
# Exclude superinstructions:
and "__" not in opname
# Exclude adaptive instructions:
and not opname.endswith("_ADAPTIVE")
# Exclude "quick" instructions:
and not opname.endswith("_QUICK")
):
return True
return False

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()
# "copy" the code to un-quicken it:
func.__code__ = func.__code__.replace()

assert not is_specialized(func), "specialized instructions found"

for i in range({QUICKENING_WARMUP_DELAY}):
for i in range(test.test_dis.QUICKENING_WARMUP_DELAY):
func(importlib._bootstrap, ["x"], lambda *args: None)

# Assert quickening worked
if set(code._co_code_adaptive[:2]) != set([{resume}, 0]):
raise AssertionError()
assert is_specialized(func), "no specialized instructions found"

print("Tests passed")
""")
Expand Down