Skip to content

Commit fb54736

Browse files
stinosdpgeorge
authored andcommitted
py/objarray: Add decode method to bytearray.
Reuse the implementation for bytes since it works the same way regardless of the underlying type. This method gets added for CPython compatibility of bytearray, but to keep the code simple and small array.array now also has a working decode method, which is non-standard but doesn't hurt.
1 parent c769da1 commit fb54736

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

py/objarray.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,9 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui
530530
STATIC const mp_rom_map_elem_t array_locals_dict_table[] = {
531531
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) },
532532
{ MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) },
533+
#if MICROPY_CPYTHON_COMPAT
534+
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&bytes_decode_obj) },
535+
#endif
533536
};
534537

535538
STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);

py/objstr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@ MP_DECLARE_CONST_FUN_OBJ_1(str_isalpha_obj);
102102
MP_DECLARE_CONST_FUN_OBJ_1(str_isdigit_obj);
103103
MP_DECLARE_CONST_FUN_OBJ_1(str_isupper_obj);
104104
MP_DECLARE_CONST_FUN_OBJ_1(str_islower_obj);
105+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj);
105106

106107
#endif // MICROPY_INCLUDED_PY_OBJSTR_H

tests/basics/bytearray_decode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
try:
2+
print(bytearray(b'').decode())
3+
print(bytearray(b'abc').decode())
4+
except AttributeError:
5+
print("SKIP")
6+
raise SystemExit

0 commit comments

Comments
 (0)