diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index 5bc2d6935fc07..9e3d494868017 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -130,10 +130,7 @@ PyObject *CPyStr_GetItem(PyObject *str, CPyTagged index) { return NULL; } } else { - PyObject *index_obj = CPyTagged_AsObject(index); - PyObject *result = PyObject_GetItem(str, index_obj); - Py_DECREF(index_obj); - return result; + return NULL; } } @@ -218,7 +215,11 @@ PyObject *CPyStr_Build(Py_ssize_t len, ...) { Py_ssize_t itemlen = PyUnicode_GET_LENGTH(item); if (itemlen != 0) { #if CPY_3_13_FEATURES - PyUnicode_CopyCharacters(res, res_offset, item, 0, itemlen); + if (PyUnicode_CopyCharacters(res, res_offset, item, 0, itemlen) < 0) { + va_end(args); + Py_DECREF(res); + return NULL; + } #else _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); #endif @@ -419,6 +420,9 @@ int CPyStr_Startswith(PyObject *self, PyObject *subobj) { return 2; } int result = PyUnicode_Tailmatch(self, substring, start, end, -1); + if (result < 0) { + return 2; + } if (result) { return 1; } @@ -443,6 +447,9 @@ int CPyStr_Endswith(PyObject *self, PyObject *subobj) { return 2; } int result = PyUnicode_Tailmatch(self, substring, start, end, 1); + if (result < 0) { + return 2; + } if (result) { return 1; } @@ -455,6 +462,9 @@ int CPyStr_Endswith(PyObject *self, PyObject *subobj) { PyObject *CPyStr_Removeprefix(PyObject *self, PyObject *prefix) { Py_ssize_t end = PyUnicode_GET_LENGTH(self); int match = PyUnicode_Tailmatch(self, prefix, 0, end, -1); + if (match < 0) { + return NULL; + } if (match) { Py_ssize_t prefix_end = PyUnicode_GET_LENGTH(prefix); return PyUnicode_Substring(self, prefix_end, end); @@ -465,6 +475,9 @@ PyObject *CPyStr_Removeprefix(PyObject *self, PyObject *prefix) { PyObject *CPyStr_Removesuffix(PyObject *self, PyObject *suffix) { Py_ssize_t end = PyUnicode_GET_LENGTH(self); int match = PyUnicode_Tailmatch(self, suffix, 0, end, 1); + if (match < 0) { + return NULL; + } if (match) { Py_ssize_t suffix_end = PyUnicode_GET_LENGTH(suffix); return PyUnicode_Substring(self, 0, end - suffix_end); diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index f313f0a8386c1..27cf79e62352d 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -265,7 +265,7 @@ arg_types=[str_rprimitive, str_rprimitive], return_type=str_rprimitive, c_function_name="CPyStr_Removeprefix", - error_kind=ERR_NEVER, + error_kind=ERR_MAGIC, ) # str.removesuffix(str) @@ -274,7 +274,7 @@ arg_types=[str_rprimitive, str_rprimitive], return_type=str_rprimitive, c_function_name="CPyStr_Removesuffix", - error_kind=ERR_NEVER, + error_kind=ERR_MAGIC, ) # str.split(...) and str.rsplit(...) diff --git a/mypyc/test-data/exceptions.test b/mypyc/test-data/exceptions.test index b8d8b4727363c..0bc6c2010f1bc 100644 --- a/mypyc/test-data/exceptions.test +++ b/mypyc/test-data/exceptions.test @@ -24,6 +24,25 @@ L3: r2 = :: int return r2 +[case testStrRemovePrefixSuffixError] +def f(s: str, prefix: str, suffix: str) -> str: + return s.removeprefix(prefix).removesuffix(suffix) +[out] +def f(s, prefix, suffix): + s, prefix, suffix, r0, r1, r2 :: str +L0: + r0 = CPyStr_Removeprefix(s, prefix) + if is_error(r0) goto L3 (error at f:2) else goto L1 +L1: + r1 = CPyStr_Removesuffix(r0, suffix) + dec_ref r0 + if is_error(r1) goto L3 (error at f:2) else goto L2 +L2: + return r1 +L3: + r2 = :: str + return r2 + [case testListAppendAndSetItemError] from typing import List def f(x: List[int], y: int, z: int) -> None: diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index 4bf8fa38cf3bc..f26b160d816c1 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -91,6 +91,12 @@ assert match_tuple_literal_args('abc', 'z', 'c') == (False, True) assert remove_prefix_suffix('', '') == ('', '') assert remove_prefix_suffix('abc', 'a') == ('bc', 'abc') assert remove_prefix_suffix('abc', 'c') == ('abc', 'ab') +assert remove_prefix_suffix('abc', '') == ('abc', 'abc') +assert remove_prefix_suffix('abc', 'abc') == ('', '') +assert remove_prefix_suffix('abc', 'abcd') == ('abc', 'abc') +assert remove_prefix_suffix('abc', 'b') == ('abc', 'abc') +assert remove_prefix_suffix('\u1234abc\U0001f600', '\u1234') == ('abc\U0001f600', '\u1234abc\U0001f600') +assert remove_prefix_suffix('\u1234abc\U0001f600', '\U0001f600') == ('\u1234abc\U0001f600', '\u1234abc') [case testStringEquality] def eq(a: str, b: str) -> bool: