diff options
| author | Scott Shawcroft <scott@adafruit.com> | 2020-05-18 14:38:45 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-18 14:38:45 -0700 |
| commit | 9811c1fe4b76d41c95d0d96df84e478b76dec261 (patch) | |
| tree | 1195f64da8f90e70a830caf9ed8d8fe3c074e2e8 /py | |
| parent | 2e950a059cf732ebdeb8bb77251aaef79e4ef8c4 (diff) | |
| parent | e175a64036585a4848892c33620e7b0ac1d40ece (diff) | |
Merge pull request #2896 from theacodes/add-bytearray-decode
Add bytearray.decode() for CPython compatibility
Diffstat (limited to 'py')
| -rw-r--r-- | py/objarray.c | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/py/objarray.c b/py/objarray.c index 9114a63c5..fccb966a2 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -63,6 +63,10 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_bu STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg); STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in); STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); +#if MICROPY_CPYTHON_COMPAT +STATIC mp_obj_t array_decode(size_t n_args, const mp_obj_t *args); +#endif + /******************************************************************************/ // array @@ -546,7 +550,24 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui return 0; } -#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY + +#if MICROPY_CPYTHON_COMPAT && MICROPY_PY_BUILTINS_BYTEARRAY +// Directly lifted from objstr.c +STATIC mp_obj_t array_decode(size_t n_args, const mp_obj_t *args) { + mp_obj_t new_args[2]; + if (n_args == 1) { + new_args[0] = args[0]; + new_args[1] = MP_OBJ_NEW_QSTR(MP_QSTR_utf_hyphen_8); + args = new_args; + n_args++; + } + return mp_obj_str_make_new(&mp_type_str, n_args, args, NULL); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(array_decode_obj, 1, 3, array_decode); +#endif + + +#if MICROPY_PY_ARRAY STATIC const mp_rom_map_elem_t array_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) }, { 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[] = { STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table); #endif +#if MICROPY_PY_BUILTINS_BYTEARRAY +STATIC const mp_rom_map_elem_t bytearray_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) }, + { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) }, +#if MICROPY_CPYTHON_COMPAT + { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&array_decode_obj) }, +#endif +}; + +STATIC MP_DEFINE_CONST_DICT(bytearray_locals_dict, bytearray_locals_dict_table); +#endif + + #if MICROPY_PY_ARRAY const mp_obj_type_t mp_type_array = { { &mp_type_type }, @@ -581,7 +615,7 @@ const mp_obj_type_t mp_type_bytearray = { .binary_op = array_binary_op, .subscr = array_subscr, .buffer_p = { .get_buffer = array_get_buffer }, - .locals_dict = (mp_obj_dict_t*)&array_locals_dict, + .locals_dict = (mp_obj_dict_t*)&bytearray_locals_dict, }; #endif |
