Skip to content

Commit 5ebd5f0

Browse files
committed
objstr: Slice indexing: support bytes properly.
1 parent bfb8819 commit 5ebd5f0

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

py/objstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
332332
}
333333

334334
STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
335+
mp_obj_type_t *type = mp_obj_get_type(self_in);
335336
GET_STR_DATA_LEN(self_in, self_data, self_len);
336337
if (value == MP_OBJ_SENTINEL) {
337338
// load
@@ -341,10 +342,9 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
341342
if (!mp_seq_get_fast_slice_indexes(self_len, index, &start, &stop)) {
342343
assert(0);
343344
}
344-
return mp_obj_new_str(self_data + start, stop - start, false);
345+
return str_new(type, self_data + start, stop - start);
345346
}
346347
#endif
347-
mp_obj_type_t *type = mp_obj_get_type(self_in);
348348
uint index_val = mp_get_index(type, self_len, index, false);
349349
if (type == &mp_type_bytes) {
350350
return MP_OBJ_NEW_SMALL_INT((mp_small_int_t)self_data[index_val]);

tests/basics/string-slice.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@
3030
# No IndexError!
3131
print(""[1:1])
3232
print(""[-1:-1])
33+
34+
# bytes
35+
print(b"123"[0:2])

0 commit comments

Comments
 (0)