8000 gh-114058: Fix flaky globals to constant test (#115423) · python/cpython@57e4c81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57e4c81

Browse files
gh-114058: Fix flaky globals to constant test (#115423)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 3fd2ad8 commit 57e4c81

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import _testinternalcapi
99

10+
from test.support import script_helper
11+
1012

1113
@contextlib.contextmanager
1214
def temporary_optimizer(opt):
@@ -659,7 +661,7 @@ def dummy(x):
659661

660662
opt = _testinternalcapi.get_uop_optimizer()
661663
with temporary_optimizer(opt):
662-
testfunc(20)
664+
testfunc(32)
663665

664666
ex = get_first_executor(testfunc)
665667
self.assertIsNotNone(ex)
@@ -677,10 +679,10 @@ def testfunc(n):
677679

678680
opt = _testinternalcapi.get_uop_optimizer()
679681
with temporary_optimizer(opt):
680-
res = testfunc(20)
682+
res = testfunc(32)
681683

682684
ex = get_first_executor(testfunc)
683-
self.assertEqual(res, 19 * 2)
685+
self.assertEqual(res, 62)
684686
self.assertIsNotNone(ex)
685687
uops = {opname for opname, _, _ in ex}
686688
self.assertNotIn("_GUARD_BOTH_INT", uops)
@@ -699,7 +701,7 @@ def testfunc(n):
699701

700702
opt = _testinternalcapi.get_uop_optimizer()
701703
with temporary_optimizer(opt):
702-
res = testfunc(20)
704+
res = testfunc(32)
703705

704706
ex = get_first_executor(testfunc)
705707
self.assertEqual(res, 4)
@@ -716,7 +718,7 @@ def testfunc(n):
716718

717719
opt = _testinternalcapi.get_uop_optimizer()
718720
with temporary_optimizer(opt):
719-
testfunc(20)
721+
testfunc(32)
720722

721723
ex = get_first_executor(testfunc)
722724
self.assertIsNotNone(ex)
@@ -740,7 +742,7 @@ def testfunc(n):
740742

741743
def dummy(x):
742744
return x + 2
743-
testfunc(10)
745+
testfunc(32)
744746

745747
ex = get_first_executor(testfunc)
746748
# Honestly as long as it doesn't crash it's fine.
@@ -749,20 +751,39 @@ def dummy(x):
749751
# This test is a little implementation specific.
750752

751753
def test_promote_globals_to_constants(self):
754+
755+
result = script_helper.run_python_until_end('-c', textwrap.dedent("""
756+
import _testinternalcapi
757+
import opcode
758+
759+
def get_first_executor(func):
760+
code = func.__code__
761+
co_code = code.co_code
762+
JUMP_BACKWARD = opcode.opmap["JUMP_BACKWARD"]
763+
for i in range(0, len(co_code), 2):
764+
if co_code[i] == JUMP_BACKWARD:
765+
try:
766+
return _testinternalcapi.get_executor(code, i)
767+
except ValueError:
768+
pass
769+
return None
770+
752771
def testfunc(n):
753772
for i in range(n):
754773
x = range(i)
755774
return x
756775
757776
opt = _testinternalcapi.get_uop_optimizer()
758-
with temporary_optimizer(opt):
759-
testfunc(20)
777+
_testinternalcapi.set_optimizer(opt)
778+
testfunc(64)
760779
761780
ex = get_first_executor(testfunc)
762-
self.assertIsNotNone(ex)
781+
assert ex is not None
763782
uops = {opname for opname, _, _ in ex}
764-
self.assertNotIn("_LOAD_GLOBAL_BUILTIN", uops)
765-
self.assertIn("_LOAD_CONST_INLINE_BORROW_WITH_NULL", uops)
783+
assert "_LOAD_GLOBAL_BUILTINS" not in uops
784+
assert "_LOAD_CONST_INLINE_BORROW_WITH_NULL" in uops
785+
"""))
786+
self.assertEqual(result[0].rc, 0, result)
766787

767788

768789

0 commit comments

Comments
 (0)
0