7
7
8
8
import _testinternalcapi
9
9
10
+ from test .support import script_helper
11
+
10
12
11
13
@contextlib .contextmanager
12
14
def temporary_optimizer (opt ):
@@ -659,7 +661,7 @@ def dummy(x):
659
661
660
662
opt = _testinternalcapi .get_uop_optimizer ()
661
663
with temporary_optimizer (opt ):
662
- testfunc (20 )
664
+ testfunc (32 )
663
665
664
666
ex = get_first_executor (testfunc )
665
667
self .assertIsNotNone (ex )
@@ -677,10 +679,10 @@ def testfunc(n):
677
679
678
680
opt = _testinternalcapi .get_uop_optimizer ()
679
681
with temporary_optimizer (opt ):
680
- res = testfunc (20 )
682
+ res = testfunc (32 )
681
683
682
684
ex = get_first_executor (testfunc )
683
- self .assertEqual (res , 19 * 2 )
685
+ self .assertEqual (res , 62 )
684
686
self .assertIsNotNone (ex )
685
687
uops = {opname for opname , _ , _ in ex }
686
688
self .assertNotIn ("_GUARD_BOTH_INT" , uops )
@@ -699,7 +701,7 @@ def testfunc(n):
699
701
700
702
opt = _testinternalcapi .get_uop_optimizer ()
701
703
with temporary_optimizer (opt ):
702
- res = testfunc (20 )
704
+ res = testfunc (32 )
703
705
704
706
ex = get_first_executor (testfunc )
705
707
self .assertEqual (res , 4 )
@@ -716,7 +718,7 @@ def testfunc(n):
716
718
717
719
opt = _testinternalcapi .get_uop_optimizer ()
718
720
with temporary_optimizer (opt ):
719
- testfunc (20 )
721
+ testfunc (32 )
720
722
721
723
ex = get_first_executor (testfunc )
722
724
self .assertIsNotNone (ex )
@@ -740,7 +742,7 @@ def testfunc(n):
740
742
741
743
def dummy (x ):
742
744
return x + 2
743
- testfunc (10 )
745
+ testfunc (32 )
744
746
745
747
ex = get_first_executor (testfunc )
746
748
# Honestly as long as it doesn't crash it's fine.
@@ -749,20 +751,39 @@ def dummy(x):
749
751
# This test is a little implementation specific.
750
752
751
753
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
+
752
771
def testfunc(n):
753
772
for i in range(n):
754
773
x = range(i)
755
774
return x
756
775
757
776
opt = _testinternalcapi.get_uop_optimizer()
758
- with temporary_optimizer (opt ):
759
- testfunc (20 )
777
+ _testinternalcapi.set_optimizer (opt)
778
+ testfunc(64 )
760
779
761
780
ex = get_first_executor(testfunc)
762
- self . assertIsNotNone ( ex )
781
+ assert ex is not None
763
782
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 )
766
787
767
788
768
789
0 commit comments