Skip to content

Commit 9749b2f

Browse files
committed
objstr: Make sure that bytes are indexed as bytes, not as unicode.
Fixes adafruit#795.
1 parent 6e6bccc commit 9749b2f

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

py/objstr.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, u
353353
}
354354
#endif
355355

356-
STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
356+
// This is used for both bytes and 8-bit strings. This is not used for unicode strings.
357+
STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
357358
mp_obj_type_t *type = mp_obj_get_type(self_in);
358359
GET_STR_DATA_LEN(self_in, self_data, self_len);
359360
if (value == MP_OBJ_SENTINEL) {
@@ -368,11 +369,11 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
368369
return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start);
369370
}
370371
#endif
371-
const byte *p = str_index_to_ptr(type, self_data, self_len, index, false);
372+
mp_uint_t index_val = mp_get_index(type, self_len, index, false);
372373
if (type == &mp_type_bytes) {
373-
return MP_OBJ_NEW_SMALL_INT(*p);
374+
return MP_OBJ_NEW_SMALL_INT(self_data[index_val]);
374375
} else {
375-
return mp_obj_new_str((char*)p, 1, true);
376+
return mp_obj_new_str((char*)&self_data[index_val], 1, true);
376377
}
377378
} else {
378379
return MP_OBJ_NULL; // op not supported
@@ -1704,7 +1705,7 @@ const mp_obj_type_t mp_type_str = {
17041705
.print = str_print,
17051706
.make_new = str_make_new,
17061707
.binary_op = mp_obj_str_binary_op,
1707-
.subscr = str_subscr,
1708+
.subscr = bytes_subscr,
17081709
.getiter = mp_obj_new_str_iterator,
17091710
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
17101711
.locals_dict = (mp_obj_t)&str_locals_dict,
@@ -1718,7 +1719,7 @@ const mp_obj_type_t mp_type_bytes = {
17181719
.print = str_print,
17191720
.make_new = bytes_make_new,
17201721
.binary_op = mp_obj_str_binary_op,
1721-
.subscr = str_subscr,
1722+
.subscr = bytes_subscr,
17221723
.getiter = mp_obj_new_bytes_iterator,
17231724
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
17241725
.locals_dict = (mp_obj_t)&str_locals_dict,

tests/basics/bytes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ def gen():
3737
for i in range(4):
3838
yield i
3939
print(bytes(gen()))
40+
41+
# Make sure bytes are not mistreated as unicode
42+
x = b"\xff\x8e\xfe}\xfd\x7f"
43+
print(len(x))
44+
print(x[0], x[1], x[2], x[3])

0 commit comments

Comments
 (0)