diff options
author | John R. Lenton <jlenton@gmail.com> | 2014-01-05 22:11:01 +0000 |
---|---|---|
committer | John R. Lenton <jlenton@gmail.com> | 2014-01-05 22:11:01 +0000 |
commit | 8428b8f3c94fb4adab6b094c1e0718168ebd8cbd (patch) | |
tree | 06cfd0bcfb68771884761209939c47adc8457657 /py/objlist.c | |
parent | f1c6ad46afa3eaf6df999cd51eb406110b6cac69 (diff) | |
parent | 12e2656472bf53e467c066eda6f3e177a97210ca (diff) | |
download | micropython-8428b8f3c94fb4adab6b094c1e0718168ebd8cbd.tar.gz micropython-8428b8f3c94fb4adab6b094c1e0718168ebd8cbd.zip |
Merge remote-tracking branch 'upstream/master' into list_insert
Diffstat (limited to 'py/objlist.c')
-rw-r--r-- | py/objlist.c | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/py/objlist.c b/py/objlist.c index f5299ddc86..bfe3383f40 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -6,6 +6,7 @@ #include "nlr.h" #include "misc.h" #include "mpconfig.h" +#include "mpqstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" @@ -35,6 +36,29 @@ static void list_print(void (*print)(void *env, const char *fmt, ...), void *env print(env, "]"); } +static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 0: + // return a new, empty list + return rt_build_list(0, NULL); + + case 1: + { + // make list from iterable + mp_obj_t iterable = rt_getiter(args[0]); + mp_obj_t list = rt_build_list(0, NULL); + mp_obj_t item; + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { + rt_list_append(list, item); + } + return list; + } + + default: + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); + } +} + static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_list_t *o = lhs; switch (op) { @@ -81,7 +105,7 @@ static mp_obj_t list_pop(int n_args, const mp_obj_t *args) { assert(MP_OBJ_IS_TYPE(args[0], &list_type)); mp_obj_list_t *self = args[0]; if (self->len == 0) { - nlr_jump(mp_obj_new_exception_msg(rt_q_IndexError, "pop from empty list")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list")); } uint index = mp_get_index(self->base.type, self->len, n_args == 1 ? mp_obj_new_int(-1) : args[1]); mp_obj_t ret = self->items[index]; @@ -170,7 +194,7 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) { } } - nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "Object not in list.")); + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list")); } static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) { @@ -211,6 +235,7 @@ const mp_obj_type_t list_type = { { &mp_const_type }, "list", list_print, // print + list_make_new, // make_new NULL, // call_n NULL, // unary_op list_binary_op, // binary_op @@ -290,6 +315,7 @@ static const mp_obj_type_t list_it_type = { { &mp_const_type }, "list_iterator", NULL, // print + NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op |