diff --git a/src/public/py_list.c b/src/public/py_list.c index 1ae0c17a..2d9c1a6f 100644 --- a/src/public/py_list.c +++ b/src/public/py_list.c @@ -127,6 +127,30 @@ static bool _py_list__new__(int argc, py_Ref argv) { return TypeError("list() takes at most 1 argument"); } +static bool _py_list__getitem__(int argc, py_Ref argv) { + PY_CHECK_ARGC(2); + List* self = py_touserdata(py_arg(0)); + py_Ref _1 = py_arg(1); + if(_1->type == tp_int) { + int index = py_toint(py_arg(1)); + if(!pk__normalize_index(&index, self->count)) return false; + *py_retval() = c11__getitem(py_TValue, self, index); + return true; + } else if(_1->type == tp_slice) { + int start, stop, step; + bool ok = pk__parse_int_slice(_1, self->count, &start, &stop, &step); + if(!ok) return false; + py_newlist(py_retval()); + List* list = py_touserdata(py_retval()); + PK_SLICE_LOOP(i, start, stop, step) { + c11_vector__push(py_TValue, list, c11__getitem(py_TValue, self, i)); + } + return true; + } else { + return TypeError("list indices must be integers"); + } +} + py_Type pk_list__register() { pk_VM* vm = pk_current_vm; py_Type type = pk_VM__new_type(vm, "list", tp_object, NULL, false); @@ -137,5 +161,6 @@ py_Type pk_list__register() { py_bindmagic(type, __eq__, _py_list__eq__); py_bindmagic(type, __ne__, _py_list__ne__); py_bindmagic(type, __new__, _py_list__new__); + py_bindmagic(type, __getitem__, _py_list__getitem__); return type; } \ No newline at end of file diff --git a/src/public/py_str.c b/src/public/py_str.c index 98e09512..d1fbb42c 100644 --- a/src/public/py_str.c +++ b/src/public/py_str.c @@ -167,9 +167,10 @@ static bool _py_str__getitem__(int argc, py_Ref argv) { py_Ref _1 = py_arg(1); if(_1->type == tp_int) { int index = py_toint(py_arg(1)); - pk__normalize_index(&index, self.size); + if(!pk__normalize_index(&index, self.size)) return false; c11_sv res = c11_sv__u8_getitem(self, index); py_newstrn(py_retval(), res.data, res.size); + return true; } else if(_1->type == tp_slice) { int start, stop, step; bool ok = pk__parse_int_slice(_1, c11_sv__u8_length(self), &start, &stop, &step); @@ -179,9 +180,8 @@ static bool _py_str__getitem__(int argc, py_Ref argv) { c11_string__delete(res); return true; } else { - return TypeError("str indices must be integers"); + return TypeError("string indices must be integers"); } - return true; } #define DEF_STR_CMP_OP(op, __f, __cond) \