diff options
Diffstat (limited to 'py/objarray.c')
-rw-r--r-- | py/objarray.c | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/py/objarray.c b/py/objarray.c index 4f36561153..42dbfcda05 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -124,17 +124,22 @@ static void array_set_el(mp_obj_array_t *o, int index, mp_obj_t val_in) { static void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_array_t *o = o_in; if (o->typecode == BYTEARRAY_TYPECODE) { - print(env, "bytearray([", o->typecode); + print(env, "bytearray(b", o->typecode); + mp_str_print_quoted(print, env, o->items, o->len); } else { - print(env, "array('%c', [", o->typecode); - } - for (int i = 0; i < o->len; i++) { - if (i > 0) { - print(env, ", "); + print(env, "array('%c'", o->typecode); + if (o->len > 0) { + print(env, ", [", o->typecode); + for (int i = 0; i < o->len; i++) { + if (i > 0) { + print(env, ", "); + } + print(env, "%d", array_get_el(o, i)); + } + print(env, "]"); } - print(env, "%d", array_get_el(o, i)); } - print(env, "])"); + print(env, ")"); } static mp_obj_t array_construct(char typecode, mp_obj_t initializer) { @@ -164,20 +169,17 @@ static mp_obj_t array_construct(char typecode, mp_obj_t initializer) { } static mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { - switch (n_args) { - case 2: - { - // TODO check args - uint l; - const byte *s = mp_obj_str_get_data(args[0], &l); - mp_obj_t initializer = args[1]; - return array_construct(*s, initializer); - } - - default: - nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "unexpected # of arguments, %d given", n_args)); + if (n_args < 1 || n_args > 2) { + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "unexpected # of arguments, %d given", n_args)); + } + // TODO check args + uint l; + const byte *typecode = mp_obj_str_get_data(args[0], &l); + if (n_args == 1) { + return array_new(*typecode, 0); } - return NULL; + + return array_construct(*typecode, args[1]); } // This is top-level factory function, not virtual method @@ -269,6 +271,17 @@ mp_obj_t mp_obj_new_bytearray(uint n, void *items) { return o; } +// Create bytearray which references specified memory area +mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) { + mp_obj_array_t *o = m_new_obj(mp_obj_array_t); + o->base.type = &array_type; + o->typecode = BYTEARRAY_TYPECODE; + o->free = 0; + o->len = n; + o->items = items; + return o; +} + /******************************************************************************/ /* array iterator */ |