@@ -1904,6 +1904,26 @@ def testfunc(n):
19041904 self .assertNotIn ("_GUARD_TOS_UNICODE" , uops )
19051905 self .assertIn ("_BINARY_OP_ADD_UNICODE" , uops )
19061906
1907+ def test_binary_subcsr_ustr_int_narrows_to_str (self ):
1908+ def testfunc (n ):
1909+ x = []
1910+ s = "바이트코f드_특수화"
1911+ for _ in range (n ):
1912+ y = s [4 ] # _BINARY_OP_SUBSCR_USTR_INT
1913+ z = "bar" + y # (_GUARD_TOS_UNICODE) + _BINARY_OP_ADD_UNICODE
1914+ x .append (z )
1915+ return x
1916+
1917+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
1918+ self .assertEqual (res , ["barf" ] * TIER2_THRESHOLD )
1919+ self .assertIsNotNone (ex )
1920+ uops = get_opnames (ex )
1921+ self .assertIn ("_BINARY_OP_SUBSCR_USTR_INT" , uops )
1922+ # _BINARY_OP_SUBSCR_USTR_INT narrows the result to 'str' so
1923+ # the unicode guard before _BINARY_OP_ADD_UNICODE is removed.
1924+ self .assertNotIn ("_GUARD_TOS_UNICODE" , uops )
1925+ self .assertIn ("_BINARY_OP_ADD_UNICODE" , uops )
1926+
19071927 def test_binary_op_subscr_str_int (self ):
19081928 def testfunc (n ):
19091929 x = 0
@@ -1924,6 +1944,26 @@ def testfunc(n):
19241944 self .assertLessEqual (count_ops (ex , "_POP_TOP" ), 2 )
19251945 self .assertLessEqual (count_ops (ex , "_POP_TOP_INT" ), 1 )
19261946
1947+ def test_binary_op_subscr_ustr_int (self ):
1948+ def testfunc (n ):
1949+ x = 0
1950+ s = "hello바"
1951+ for _ in range (n ):
1952+ c = s [1 ] # _BINARY_OP_SUBSCR_USTR_INT
1953+ if c == 'e' :
1954+ x += 1
1955+ return x
1956+
1957+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
1958+ self .assertEqual (res , TIER2_THRESHOLD )
1959+ self .assertIsNotNone (ex )
1960+ uops = get_opnames (ex )
1961+ self .assertIn ("_BINARY_OP_SUBSCR_USTR_INT" , uops )
1962+ self .assertIn ("_COMPARE_OP_STR" , uops )
1963+ self .assertIn ("_POP_TOP_NOP" , uops )
1964+ self .assertLessEqual (count_ops (ex , "_POP_TOP" ), 2 )
1965+ self .assertLessEqual (count_ops (ex , "_POP_TOP_INT" ), 1 )
1966+
19271967 def test_call_type_1_guards_removed (self ):
19281968 def testfunc (n ):
19291969 x = 0
0 commit comments