From 35917fc5f4b25375169e5e2417ae659ef0528200 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 4 Mar 2026 14:30:35 +0800 Subject: [PATCH] fix #468 --- src/bindings/py_str.c | 4 +++- src/public/PyList.c | 2 ++ tests/041_str.py | 1 + tests/050_list.py | 1 + 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bindings/py_str.c b/src/bindings/py_str.c index 8c8737f8..de5c556c 100644 --- a/src/bindings/py_str.c +++ b/src/bindings/py_str.c @@ -477,12 +477,14 @@ static bool str_rjust(int argc, py_Ref argv) { return str__widthjust_impl(false, static bool str_find(int argc, py_Ref argv) { if(argc > 3) return TypeError("find() takes at most 3 arguments"); + c11_string* self = pk_tostr(&argv[0]); int start = 0; if(argc == 3) { PY_CHECK_ARG_TYPE(2, tp_int); start = py_toint(py_arg(2)); + if(start < 0) start += c11_sv__u8_length(c11_string__sv(self)); + if(start < 0) start = 0; } - c11_string* self = pk_tostr(&argv[0]); PY_CHECK_ARG_TYPE(1, tp_str); c11_string* sub = pk_tostr(&argv[1]); int res = c11_sv__index2(c11_string__sv(self), c11_string__sv(sub), start); diff --git a/src/public/PyList.c b/src/public/PyList.c index ba937307..a30c412d 100644 --- a/src/public/PyList.c +++ b/src/public/PyList.c @@ -317,6 +317,8 @@ static bool list_index(int argc, py_Ref argv) { if(argc == 3) { PY_CHECK_ARG_TYPE(2, tp_int); start = py_toint(py_arg(2)); + if(start < 0) start += py_list_len(py_arg(0)); + if(start < 0) start = 0; } for(int i = start; i < py_list_len(py_arg(0)); i++) { int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1)); diff --git a/tests/041_str.py b/tests/041_str.py index 556358b4..411990d9 100644 --- a/tests/041_str.py +++ b/tests/041_str.py @@ -169,6 +169,7 @@ assert a.index('23') == 1 assert a.index('2', 1) == 1 assert a.index('1', 0) == 0 +assert a.index('2', -2) == 1 assert a.find('1') == 0 assert a.find('1', 1) == -1 diff --git a/tests/050_list.py b/tests/050_list.py index 109c9901..dbeffbce 100644 --- a/tests/050_list.py +++ b/tests/050_list.py @@ -75,6 +75,7 @@ assert a.index(3) == 2 assert a.index(2, 1) == 1 assert a.index(1, 0) == 0 +assert a.index(2, -2) == 1 a, b = [1, 2] assert a == 1 and b == 2