8000 GH-129386: Use symbolic constants for specialization tests (GH-129415) · python/cpython@002c4e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 002c4e2

Browse files
authored
GH-129386: Use symbolic constants for specialization tests (GH-129415)
1 parent 99ed302 commit 002c4e2

File tree

8 files changed

+152
-132
lines changed

8 files changed

+152
-132
lines changed

Lib/test/test_call.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from test.support import (cpython_only, is_wasi, requires_limited_api, Py_DEBUG,
3-
set_recursion_limit, skip_on_s390x, skip_emscripten_stack_overflow)
3+
set_recursion_limit, skip_on_s390x, skip_emscripten_stack_overflow, import_helper)
44
try:
55
import _testcapi
66
except ImportError:
@@ -616,9 +616,6 @@ def testfunction_kw(self, *, kw):
616616
return self
617617

618618

619-
ADAPTIVE_WARMUP_DELAY = 2
620-
621-
622619
@unittest.skipIf(_testcapi is None, "requires _testcapi")
623620
class TestPEP590(unittest.TestCase):
624621

@@ -802,17 +799,18 @@ def __call__(self, *args):
802799

803800
def test_setvectorcall(self):
804801
from _testcapi import function_setvectorcall
802+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
805803
def f(num): return num + 1
806804
assert_equal = self.assertEqual
807805
num = 10
808806
assert_equal(11, f(num))
809807
function_setvectorcall(f)
810-
# make sure specializer is triggered by running > 50 times
811-
for _ in range(10 * ADAPTIVE_WARMUP_DELAY):
808+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
812809
assert_equal("overridden", f(num))
813810

814811
def test_setvectorcall_load_attr_specialization_skip(self):
815812
from _testcapi import function_setvectorcall
813+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
816814

817815
class X:
818816
def __getattribute__(self, attr):
@@ -824,11 +822,12 @@ def __getattribute__(self, attr):
824822
function_setvectorcall(X.__getattribute__)
825823
# make sure specialization doesn't trigger
826824
# when vectorcall is overridden
827-
for _ in range(ADAPTIVE_WARMUP_DELAY):
825+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
828826
assert_equal("overridden", x.a)
829827

830828
def test_setvectorcall_load_attr_specialization_deopt(self):
831829
from _testcapi import function_setvectorcall
830+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
832831

833832
class X:
834833
def __getattribute__(self, attr):
@@ -840,12 +839,12 @@ def get_a(x):
840839
assert_equal = self.assertEqual
841840
x = X()
842841
# trigger LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN specialization
843-
for _ in range(ADAPTIVE_WARMUP_DELAY):
842+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
844843
assert_equal("a", get_a(x))
845844
function_setvectorcall(X.__getattribute__)
846845
# make sure specialized LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN
847846
# gets deopted due to overridden vectorcall
848-
for _ in range(ADAPTIVE_WARMUP_DELAY):
847+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
849848
assert_equal("overridden", get_a(x))
850849

851850
@requires_limited_api

Lib/test/test_dis.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,6 @@ def extended_arg_quick():
931931
"""% (extended_arg_quick.__code__.co_firstlineno,
932932
extended_arg_quick.__code__.co_firstlineno + 1,)
933933

934-
ADAPTIVE_WARMUP_DELAY = 2
935-
936934
class DisTestBase(unittest.TestCase):
937935
"Common utilities for DisTests and TestDisTraceback"
938936

@@ -1259,8 +1257,9 @@ def test__try_compile_no_context_exc_on_error(self):
12591257
self.assertIsNone(e.__context__)
12601258

12611259
@staticmethod
1262-
def code_quicken(f, times=ADAPTIVE_WARMUP_DELAY):
1263-
for _ in range(times):
1260+
def code_quicken(f):
1261+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
1262+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
12641263
f()
12651264

12661265
@cpython_only
@@ -1306,7 +1305,7 @@ def test_call_specialize(self):
13061305
@requires_specialization
13071306
def test_loop_quicken(self):
13081307
# Loop can trigger a quicken where the loop is located
1309-
self.code_quicken(loop_test, 4)
1308+
self.code_quicken(loop_test)
13101309
got = self.get_disassembly(loop_test, adaptive=True)
13111310
jit = import_helper.import_module("_testinternalcapi").jit_enabled()
13121311
expected = dis_loop_test_quickened_code.format("JIT" if jit else "NO_JIT")
@@ -1315,8 +1314,9 @@ def test_loop_quicken(self):
13151314
@cpython_only
13161315
@requires_specialization
13171316
def test_loop_with_conditional_at_end_is_quickened(self):
1317+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
13181318
def for_loop_true(x):
1319-
for i in range(10):
1319+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
13201320
if x:
13211321
pass
13221322

@@ -1325,7 +1325,7 @@ def for_loop_true(x):
13251325
self.get_disassembly(for_loop_true, adaptive=True))
13261326

13271327
def for_loop_false(x):
1328-
for i in range(10):
1328+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
13291329
if x:
13301330
pass
13311331

@@ -1335,7 +1335,7 @@ def for_loop_false(x):
13351335

13361336
def while_loop():
13371337
i = 0
1338-
while i < 10:
1338+
while i < _testinternalcapi.SPECIALIZATION_THRESHOLD:
13391339
i += 1
13401340

13411341
while_loop()

Lib/test/test_embed.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ def test_simple_initialization_api(self):
384384
def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self):
385385
# https://github.com/python/cpython/issues/92031
386386

387-
code = textwrap.dedent("""\
387+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
388+
389+
code = textwrap.dedent(f"""\
388390
import dis
389391
import importlib._bootstrap
390392
import opcode
@@ -411,7 +413,7 @@ def is_specialized(f):
411413
412414
assert not is_specialized(func), "specialized instructions found"
413415
414-
for i in range(test.test_dis.ADAPTIVE_WARMUP_DELAY):
416+
for _ in range({_testinternalcapi.SPECIALIZATION_THRESHOLD}):
415417
func(importlib._bootstrap, ["x"], lambda *args: None)
416418
417419
assert is_specialized(func), "no specialized instructions found"

Lib/test/test_monitoring.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from test.support import requires_specialization_ft, script_helper
1515

1616
_testcapi = test.support.import_helper.import_module("_testcapi")
17+
_testinternalcapi = test.support.import_helper.import_module("_testinternalcapi")
1718

1819
PAIR = (0,1)
1920

@@ -897,13 +898,13 @@ def implicit_stop_iteration(iterator=None):
897898
# re-specialize immediately, so that we can we can test the
898899
# unspecialized version of the loop first.
899900
# Note: this assumes that we don't specialize loops over sets.
900-
implicit_stop_iteration(set(range(100)))
901+
implicit_stop_iteration(set(range(_testinternalcapi.SPECIALIZATION_THRESHOLD)))
901902

902903
# This will record a RAISE event for the StopIteration.
903904
self.check_events(implicit_stop_iteration, expected, recorders=recorders)
904905

905906
# Now specialize, so that we see a STOP_ITERATION event.
906-
for _ in range(100):
907+
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN):
907908
implicit_stop_iteration()
908909

909910
# This will record a STOP_ITERATION event for the StopIteration.
@@ -1057,7 +1058,7 @@ def f():
10571058
except ValueError:
10581059
pass
10591060

1060-
for _ in range(100):
1061+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
10611062
f()
10621063
recorders = (
10631064
ReturnRecorder,
@@ -2033,8 +2034,8 @@ def __init__(self, set_event):
20332034
sys.monitoring.set_events(TEST_TOOL, E.PY_RESUME)
20342035

20352036
def make_foo_optimized_then_set_event():
2036-
for i in range(100):
2037-
Foo(i == 99)
2037+
for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD + 1):
2038+
Foo(i == _testinternalcapi.SPECIALIZATION_THRESHOLD)
20382039

20392040
try:
20402041
make_foo_optimized_then_set_event()
@@ -2106,9 +2107,9 @@ def test_func(recorder):
21062107
set_events = sys.monitoring.set_events
21072108
line = E.LINE
21082109
i = 0
2109-
for i in range(551):
2110-
# Turn on events without branching once i reaches 500.
2111-
set_events(TEST_TOOL, line * int(i >= 500))
2110+
for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD + 51):
2111+
# Turn on events without branching once i reaches _testinternalcapi.SPECIALIZATION_THRESHOLD.
2112+
set_events(TEST_TOOL, line * int(i >= _testinternalcapi.SPECIALIZATION_THRESHOLD))
21122113
pass
21132114
pass
21142115
pass

0 commit comments

Comments
 (0)
0