Merge pull request #2896 from theacodes/add-bytearray-decode

Add bytearray.decode() for CPython compatibility
This commit is contained in:
Scott Shawcroft 2020-05-18 14:38:45 -07:00 committed by GitHub
commit 9811c1fe4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -17,10 +17,11 @@ CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CSLAVE = 0
CIRCUITPY_PIXELBUF = 1
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
SUPEROPT_GC = 0
CFLAGS_INLINE_LIMIT = 55
CFLAGS_INLINE_LIMIT = 50
# Include these Python libraries in firmware.

View File

@ -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