Commit f8b9d3c4 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

str: Throw TypeError for invalid index type and clean up comments.

parent e606cb65
......@@ -27,13 +27,11 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
const char *lhs_str = qstr_str(lhs->qstr);
switch (op) {
// string access
// XXX a massive hack!
// TODO: need predicate to check for int-like type (bools are such for example)
// ["no", "yes"][1 == 2] is common idiom
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
// TODO: This implements byte string access for single index so far
// TODO: Handle negative indexes.
return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]);
} else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) {
......@@ -50,8 +48,9 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start));
} else {
// Throw TypeError here
// Message doesn't match CPython, but we don't have so much bytes as they
// to spend them on verbose wording
nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "index must be int"));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment