Skip to content

Commit c56ca35

Browse files
committed
add tests
1 parent e7e3f40 commit c56ca35

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lib/test/test_opcache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,7 @@ def binary_subscr_str_int_non_compact():
17921792
self.assertEqual(a[idx], expected)
17931793

17941794
binary_subscr_str_int_non_compact()
1795+
self.assert_specialized(binary_subscr_str_int_non_compact, "BINARY_OP_SUBSCR_USTR_INT")
17951796
self.assert_no_opcode(binary_subscr_str_int_non_compact, "BINARY_OP_SUBSCR_STR_INT")
17961797

17971798
def binary_subscr_getitems():

0 commit comments

Comments
 (0)