Skip to content

Commit 3ef6ab1

Browse files
authored
[mypyc] Improve str.startswith and str.endswith with tuple argument (#18703)
Followup to #18678 Missed that we can also use `bool_rprimitive` as return type with a value of `2` for errors.
1 parent 09ac3ba commit 3ef6ab1

File tree

3 files changed

+24
-42
lines changed

3 files changed

+24
-42
lines changed

mypyc/lib-rt/str_ops.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int CPyStr_Startswith(PyObject *self, PyObject *subobj) {
173173
"tuple for startswith must only contain str, "
174174
"not %.100s",
175175
Py_TYPE(substring)->tp_name);
176-
return -1;
176+
return 2;
177177
}
178178
int result = PyUnicode_Tailmatch(self, substring, start, end, -1);
179179
if (result) {
@@ -197,7 +197,7 @@ int CPyStr_Endswith(PyObject *self, PyObject *subobj) {
197197
"tuple for endswith must only contain str, "
198198
"not %.100s",
199199
Py_TYPE(substring)->tp_name);
200-
return -1;
200+
return 2;
201201
}
202202
int result = PyUnicode_Tailmatch(self, substring, start, end, 1);
203203
if (result) {

mypyc/primitives/str_ops.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,13 @@
111111
error_kind=ERR_NEVER,
112112
)
113113

114-
# str.startswith(tuple) (return -1/0/1)
114+
# str.startswith(tuple)
115115
method_op(
116116
name="startswith",
117117
arg_types=[str_rprimitive, tuple_rprimitive],
118-
return_type=c_int_rprimitive,
118+
return_type=bool_rprimitive,
119119
c_function_name="CPyStr_Startswith",
120-
truncated_type=bool_rprimitive,
121-
error_kind=ERR_NEG_INT,
120+
error_kind=ERR_MAGIC,
122121
)
123122

124123
# str.endswith(str)
@@ -131,14 +130,13 @@
131130
error_kind=ERR_NEVER,
132131
)
133132

134-
# str.endswith(tuple) (return -1/0/1)
133+
# str.endswith(tuple)
135134
method_op(
136135
name="endswith",
137136
arg_types=[str_rprimitive, tuple_rprimitive],
138-
return_type=c_int_rprimitive,
137+
return_type=bool_rprimitive,
139138
c_function_name="CPyStr_Endswith",
140-
truncated_type=bool_rprimitive,
141-
error_kind=ERR_NEG_INT,
139+
error_kind=ERR_MAGIC,
142140
)
143141

144142
# str.removeprefix(str)

mypyc/test-data/irbuild-str.test

+16-32
Original file line numberDiff line numberDiff line change
@@ -153,55 +153,39 @@ def do_tuple_literal_args(s1: str) -> None:
153153
def do_startswith(s1, s2):
154154
s1 :: str
155155
s2 :: tuple
156-
r0 :: i32
157-
r1 :: bit
158-
r2 :: bool
156+
r0 :: bool
159157
L0:
160158
r0 = CPyStr_Startswith(s1, s2)
161-
r1 = r0 >= 0 :: signed
162-
r2 = truncate r0: i32 to builtins.bool
163-
return r2
159+
return r0
164160
def do_endswith(s1, s2):
165161
s1 :: str
166162
s2 :: tuple
167-
r0 :: i32
168-
r1 :: bit
169-
r2 :: bool
163+
r0 :: bool
170164
L0:
171165
r0 = CPyStr_Endswith(s1, s2)
172-
r1 = r0 >= 0 :: signed
173-
r2 = truncate r0: i32 to builtins.bool
174-
return r2
166+
return r0
175167
def do_tuple_literal_args(s1):
176168
s1, r0, r1 :: str
177169
r2 :: tuple[str, str]
178170
r3 :: object
179-
r4 :: i32
180-
r5 :: bit
181-
r6, x :: bool
182-
r7, r8 :: str
183-
r9 :: tuple[str, str]
184-
r10 :: object
185-
r11 :: i32
186-
r12 :: bit
187-
r13, y :: bool
171+
r4, x :: bool
172+
r5, r6 :: str
173+
r7 :: tuple[str, str]
174+
r8 :: object
175+
r9, y :: bool
188176
L0:
189177
r0 = 'a'
190178
r1 = 'b'
191179
r2 = (r0, r1)
192180
r3 = box(tuple[str, str], r2)
193181
r4 = CPyStr_Startswith(s1, r3)
194-
r5 = r4 >= 0 :: signed
195-
r6 = truncate r4: i32 to builtins.bool
196-
x = r6
197-
r7 = 'a'
198-
r8 = 'b'
199-
r9 = (r7, r8)
200-
r10 = box(tuple[str, str], r9)
201-
r11 = CPyStr_Endswith(s1, r10)
202-
r12 = r11 >= 0 :: signed
203-
r13 = truncate r11: i32 to builtins.bool
204-
y = r13
182+
x = r4
183+
r5 = 'a'
184+
r6 = 'b'
185+
r7 = (r5, r6)
186+
r8 = box(tuple[str, str], r7)
187+
r9 = CPyStr_Endswith(s1, r8)
188+
y = r9
205189
return 1
206190

207191
[case testStrToBool]

0 commit comments

Comments
 (0)