summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/obj.c10
-rw-r--r--py/objstr.c21
2 files changed, 13 insertions, 18 deletions
diff --git a/py/obj.c b/py/obj.c
index 7b8be5889f..e7957e21ff 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -277,12 +277,10 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
// is_slice determines whether the index is a slice index
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice) {
- int i;
- if (MP_OBJ_IS_INT(index)) {
- i = mp_obj_int_get_checked(index);
- } else if (MP_OBJ_IS_TYPE(index, &mp_type_bool)) {
- i = (index == mp_const_true ? 1 : 0);
- } else {
+ machine_int_t i;
+ if (MP_OBJ_IS_SMALL_INT(index)) {
+ i = MP_OBJ_SMALL_INT_VALUE(index);
+ } else if (!mp_obj_get_int_maybe(index, &i)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s indices must be integers, not %s", qstr_str(type->name), mp_obj_get_type_str(index)));
}
diff --git a/py/objstr.c b/py/objstr.c
index 39c24205be..e55a2edd5c 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -220,27 +220,24 @@ STATIC const byte *find_subbytes(const byte *haystack, machine_uint_t hlen, cons
STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
switch (op) {
- case MP_BINARY_OP_SUBSCR:
- if (mp_obj_is_integer(rhs_in)) {
- uint index = mp_get_index(mp_obj_get_type(lhs_in), lhs_len, rhs_in, false);
- if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_bytes)) {
- return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)lhs_data[index]);
- } else {
- return mp_obj_new_str(lhs_data + index, 1, true);
- }
+ case MP_BINARY_OP_SUBSCR: {
#if MICROPY_ENABLE_SLICE
- } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_slice)) {
+ if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_slice)) {
machine_uint_t start, stop;
if (!m_seq_get_fast_slice_indexes(lhs_len, rhs_in, &start, &stop)) {
assert(0);
}
return mp_obj_new_str(lhs_data + start, stop - start, false);
+ }
#endif
+ mp_obj_type_t *type = mp_obj_get_type(lhs_in);
+ uint index = mp_get_index(type, lhs_len, rhs_in, false);
+ if (type == &mp_type_bytes) {
+ return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)lhs_data[index]);
} else {
- // Message doesn't match CPython, but we don't have so much bytes as they
- // to spend them on verbose wording
- nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "index must be int"));
+ return mp_obj_new_str(lhs_data + index, 1, true);
}
+ }
case MP_BINARY_OP_ADD:
case MP_BINARY_OP_INPLACE_ADD: