Skip to content

Commit 9811c1f

Browse files
authored
Merge pull request #2896 from theacodes/add-bytearray-decode
Add bytearray.decode() for CPython compatibility
2 parents 2e950a0 + e175a64 commit 9811c1f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ CIRCUITPY_DISPLAYIO = 0
1717
CIRCUITPY_FREQUENCYIO = 0
1818
CIRCUITPY_I2CSLAVE = 0
1919
CIRCUITPY_PIXELBUF = 1
20+
CIRCUITPY_ROTARYIO = 0
2021
CIRCUITPY_RTC = 0
2122

2223
SUPEROPT_GC = 0
23-
CFLAGS_INLINE_LIMIT = 55
24+
CFLAGS_INLINE_LIMIT = 50
2425

2526

2627
# Include these Python libraries in firmware.

py/objarray.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_bu
6363
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
6464
STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in);
6565
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
66+
#if MICROPY_CPYTHON_COMPAT
67+
STATIC mp_obj_t array_decode(size_t n_args, const mp_obj_t *args);
68+
#endif
69+
6670

6771
/******************************************************************************/
6872
// array
@@ -546,7 +550,24 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui
546550
return 0;
547551
}
548552

549-
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
553+
554+
#if MICROPY_CPYTHON_COMPAT && MICROPY_PY_BUILTINS_BYTEARRAY
555+
// Directly lifted from objstr.c
556+
STATIC mp_obj_t array_decode(size_t n_args, const mp_obj_t *args) {
557+
mp_obj_t new_args[2];
558+
if (n_args == 1) {
559+
new_args[0] = args[0];
560+
new_args[1] = MP_OBJ_NEW_QSTR(MP_QSTR_utf_hyphen_8);
561+
args = new_args;
562+
n_args++;
563+
}
564+
return mp_obj_str_make_new(&mp_type_str, n_args, args, NULL);
565+
}
566+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(array_decode_obj, 1, 3, array_decode);
567+
#endif
568+
569+
570+
#if MICROPY_PY_ARRAY
550571
STATIC const mp_rom_map_elem_t array_locals_dict_table[] = {
551572
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) },
552573
{ MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) },
@@ -555,6 +576,19 @@ STATIC const mp_rom_map_elem_t array_locals_dict_table[] = {
555576
STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
556577
#endif
557578

579+
#if MICROPY_PY_BUILTINS_BYTEARRAY
580+
STATIC const mp_rom_map_elem_t bytearray_locals_dict_table[] = {
581+
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) },
582+
{ MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) },
583+
#if MICROPY_CPYTHON_COMPAT
584+
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&array_decode_obj) },
585+
#endif
586+
};
587+
588+
STATIC MP_DEFINE_CONST_DICT(bytearray_locals_dict, bytearray_locals_dict_table);
589+
#endif
590+
591+
558592
#if MICROPY_PY_ARRAY
559593
const mp_obj_type_t mp_type_array = {
560594
{ &mp_type_type },
@@ -581,7 +615,7 @@ const mp_obj_type_t mp_type_bytearray = {
581615
.binary_op = array_binary_op,
582616
.subscr = array_subscr,
583617
.buffer_p = { .get_buffer = array_get_buffer },
584-
.locals_dict = (mp_obj_dict_t*)&array_locals_dict,
618+
.locals_dict = (mp_obj_dict_t*)&bytearray_locals_dict,
585619
};
586620
#endif
587621

0 commit comments

Comments
 (0)