From 07205ec323ca98d1c23af51e099119954d0fe7a4 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 13 Jan 2014 02:31:00 +0000 Subject: [PATCH 1/4] added zip() --- py/misc.h | 1 + py/mpqstrraw.h | 1 + py/obj.h | 5 ++++ py/objtuple.c | 20 ++++++++++--- py/objzip.c | 59 +++++++++++++++++++++++++++++++++++++++ py/py.mk | 1 + py/runtime.c | 1 + tests/basics/tests/zip.py | 2 ++ 8 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 py/objzip.c create mode 100644 tests/basics/tests/zip.py diff --git a/py/misc.h b/py/misc.h index 1bf4d8f291..22158c71a7 100644 --- a/py/misc.h +++ b/py/misc.h @@ -21,6 +21,7 @@ typedef unsigned int uint; #define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num)))) #define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num)) #define m_del_obj(type, ptr) (m_del(type, ptr, 1)) +#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num))) void *m_malloc(int num_bytes); void *m_malloc0(int num_bytes); diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h index e4404a36f0..e33194fd45 100644 --- a/py/mpqstrraw.h +++ b/py/mpqstrraw.h @@ -60,6 +60,7 @@ Q(set) Q(sum) Q(tuple) Q(type) +Q(zip) Q(append) Q(pop) diff --git a/py/obj.h b/py/obj.h index a9a5827ee7..8a2cba62bb 100644 --- a/py/obj.h +++ b/py/obj.h @@ -285,6 +285,7 @@ mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_im // tuple extern const mp_obj_type_t tuple_type; void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items); +void mp_obj_tuple_del(mp_obj_t self_in); // list extern const mp_obj_type_t list_type; @@ -306,6 +307,10 @@ void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item); extern const mp_obj_type_t slice_type; void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step); +// zip +extern const mp_obj_type_t zip_type; + + // functions typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM) mp_obj_base_t base; diff --git a/py/objtuple.c b/py/objtuple.c index 7685cc449f..5c1169ed55 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -116,8 +116,10 @@ mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items) { mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n); o->base.type = &tuple_type; o->len = n; - for (int i = 0; i < n; i++) { - o->items[i] = items[i]; + if (items) { + for (int i = 0; i < n; i++) { + o->items[i] = items[i]; + } } return o; } @@ -138,8 +140,18 @@ mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items) { void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items) { assert(MP_OBJ_IS_TYPE(self_in, &tuple_type)); mp_obj_tuple_t *self = self_in; - *len = self->len; - *items = &self->items[0]; + if (len) { + *len = self->len; + } + if (items) { + *items = &self->items[0]; + } +} + +void mp_obj_tuple_del(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &tuple_type)); + mp_obj_tuple_t *self = self_in; + m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self); } /******************************************************************************/ diff --git a/py/objzip.c b/py/objzip.c new file mode 100644 index 0000000000..2ab80af0b5 --- /dev/null +++ b/py/objzip.c @@ -0,0 +1,59 @@ +#include +#include + +#include "misc.h" +#include "mpconfig.h" +#include "obj.h" +#include "runtime.h" + +typedef struct _mp_obj_zip_t { + mp_obj_base_t base; + int n_iters; + mp_obj_t iters[]; +} mp_obj_zip_t; + +static mp_obj_t zip_getiter(mp_obj_t self_in) { + return self_in; +} + +static mp_obj_t zip_iternext(mp_obj_t self_in); + +static mp_obj_t zip_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + /* NOTE: args are backwards */ + mp_obj_zip_t *o = m_new_obj_var(mp_obj_zip_t, mp_obj_t, n_args); + o->base.type = &zip_type; + o->n_iters = n_args; + for (int i = 0; i < n_args; i++) { + o->iters[i] = rt_getiter(args[n_args-i-1]); + } + return o; +} + +const mp_obj_type_t zip_type = { + { &mp_const_type }, + "zip", + .make_new = zip_make_new, + .iternext = zip_iternext, + .getiter = zip_getiter, +}; + +static mp_obj_t zip_iternext(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &zip_type)); + mp_obj_zip_t *self = self_in; + mp_obj_t *items; + if (self->n_iters == 0) { + return mp_const_stop_iteration; + } + mp_obj_t o = mp_obj_new_tuple(self->n_iters, NULL); + mp_obj_tuple_get(o, NULL, &items); + + for (int i = 0; i < self->n_iters; i++) { + mp_obj_t next = rt_iternext(self->iters[i]); + if (next == mp_const_stop_iteration) { + mp_obj_tuple_del(o); + return mp_const_stop_iteration; + } + items[i] = next; + } + return o; +} diff --git a/py/py.mk b/py/py.mk index a44a8bad02..95f9c07671 100644 --- a/py/py.mk +++ b/py/py.mk @@ -97,6 +97,7 @@ PY_O_BASENAME = \ vm.o \ showbc.o \ repl.o \ + objzip.o \ # prepend the build destination prefix to the py object files diff --git a/py/runtime.c b/py/runtime.c index 2af86b6abd..02f965dd63 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -113,6 +113,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type); mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type); mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type); + mp_map_add_qstr(&map_builtins, MP_QSTR_zip, (mp_obj_t)&zip_type); // built-in user functions; TODO covert all to &mp_builtin_xxx's mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs)); diff --git a/tests/basics/tests/zip.py b/tests/basics/tests/zip.py new file mode 100644 index 0000000000..c0109094f4 --- /dev/null +++ b/tests/basics/tests/zip.py @@ -0,0 +1,2 @@ +print(list(zip())) +print(list(zip([1], {2,3}))) From 5c7683955994b965270598559e979c581ddc1950 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 13 Jan 2014 05:12:50 +0000 Subject: [PATCH 2/4] sorted --- py/builtin.c | 26 ++++++++++++++++++++++++++ py/builtin.h | 1 + py/mpqstrraw.h | 1 + py/objlist.c | 3 ++- py/runtime.c | 1 + py/vm.c | 15 +++++++++++---- tests/basics/tests/sorted.py | 2 ++ 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/basics/tests/sorted.py diff --git a/py/builtin.c b/py/builtin.c index 078f4b49c3..f73d41b0d8 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -296,3 +296,29 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { } return value; } + +extern mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs); +static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { + mp_obj_t *args_items = NULL; + uint args_len = 0; + + assert(MP_OBJ_IS_TYPE(args, &tuple_type)); + mp_obj_tuple_get(args, &args_len, &args_items); + assert(args_len >= 1); + if (args_len > 1) { + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, + "must use keyword argument for key function")); + } + mp_obj_t iterable = rt_getiter(args_items[0]); + mp_obj_t self = rt_build_list(0, NULL); + mp_obj_t item; + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { + rt_list_append(self, item); + } + + mp_obj_t new_args = rt_build_tuple(1, &self); + list_sort(new_args, kwargs); + + return self; +} +MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, mp_builtin_sorted); diff --git a/py/builtin.h b/py/builtin.h index db7d517a06..a69f1166ac 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -23,3 +23,4 @@ mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj); diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h index e33194fd45..c3cda84b4d 100644 --- a/py/mpqstrraw.h +++ b/py/mpqstrraw.h @@ -57,6 +57,7 @@ Q(pow) Q(print) Q(range) Q(set) +Q(sorted) Q(sum) Q(tuple) Q(type) diff --git a/py/objlist.c b/py/objlist.c index fa8ec67d09..d5ea7f47b2 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -248,13 +248,14 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r } } -static mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) { +mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) { mp_obj_t *args_items = NULL; uint args_len = 0; assert(MP_OBJ_IS_TYPE(args, &tuple_type)); mp_obj_tuple_get(args, &args_len, &args_items); assert(args_len >= 1); + assert(MP_OBJ_IS_TYPE(args_items[0], &list_type)); if (args_len > 1) { nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "list.sort takes no positional arguments")); diff --git a/py/runtime.c b/py/runtime.c index 02f965dd63..9815452fc3 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -134,6 +134,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow)); mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print)); mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range)); + mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum)); next_unique_code_id = 1; // 0 indicates "no code" diff --git a/py/vm.c b/py/vm.c index baef6988c5..5c74b43caa 100644 --- a/py/vm.c +++ b/py/vm.c @@ -440,10 +440,17 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** case MP_BC_CALL_FUNCTION: DECODE_UINT; - assert((unum & 0xff00) == 0); // n_keyword - unum &= 0xff; // n_positional - sp += unum; - *sp = rt_call_function_n(*sp, unum, sp - unum); + if ((unum & 0xff00) == 0) { + // no keywords + unum &= 0xff; // n_positional + sp += unum; + *sp = rt_call_function_n(*sp, unum, sp - unum); + } else { + // keywords + int argsize = (unum & 0xff) + ((unum >> 7) & 0x1fe); + sp += argsize; + *sp = rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp - argsize); + } break; case MP_BC_CALL_METHOD: diff --git a/tests/basics/tests/sorted.py b/tests/basics/tests/sorted.py new file mode 100644 index 0000000000..bbec319460 --- /dev/null +++ b/tests/basics/tests/sorted.py @@ -0,0 +1,2 @@ +print(sorted(set(range(100)))) +print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2))) From 2ded68db77d92f4f277ae688c22ac0be32dc4f31 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 13 Jan 2014 19:52:28 +0000 Subject: [PATCH 3/4] Cleaned up sorted() as per Damien's suggestions. --- py/builtin.c | 9 +-------- py/obj.h | 1 + 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/py/builtin.c b/py/builtin.c index f73d41b0d8..2d61cd603f 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -297,7 +297,6 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { return value; } -extern mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs); static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { mp_obj_t *args_items = NULL; uint args_len = 0; @@ -309,13 +308,7 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "must use keyword argument for key function")); } - mp_obj_t iterable = rt_getiter(args_items[0]); - mp_obj_t self = rt_build_list(0, NULL); - mp_obj_t item; - while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { - rt_list_append(self, item); - } - + mp_obj_t self = list_type.make_new((mp_obj_t)&list_type, 1, args_items); mp_obj_t new_args = rt_build_tuple(1, &self); list_sort(new_args, kwargs); diff --git a/py/obj.h b/py/obj.h index 8a2cba62bb..d1db0dde0f 100644 --- a/py/obj.h +++ b/py/obj.h @@ -292,6 +292,7 @@ extern const mp_obj_type_t list_type; mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg); void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items); void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); +mp_obj_t list_sort(mp_obj_t args, struct _mp_map_t *kwargs); // dict extern const mp_obj_type_t dict_type; From 88cb1e60e0b780d71e9c2d7b0acafa71ba3ea318 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Mon, 13 Jan 2014 19:55:18 +0000 Subject: [PATCH 4/4] Made sorted() raise an exception instead of aborting when given no arguments; moved around some things in objfun.c as a consequence --- py/builtin.c | 2 +- py/obj.h | 2 +- py/objfun.c | 43 +++++++++++++++++++++++++++++-------------- py/objlist.c | 2 +- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/py/builtin.c b/py/builtin.c index 2d61cd603f..53ae93ad95 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -314,4 +314,4 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { return self; } -MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, mp_builtin_sorted); +MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted); diff --git a/py/obj.h b/py/obj.h index d1db0dde0f..dbf9efe20e 100644 --- a/py/obj.h +++ b/py/obj.h @@ -59,7 +59,7 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name) -#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, 0, (~((machine_uint_t)0)), (mp_fun_kw_t)fun_name) +#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, n_args_min, (~((machine_uint_t)0)), (mp_fun_kw_t)fun_name) // These macros are used to declare and define constant staticmethond and classmethod objects // You can put "static" in front of the definitions to make them local diff --git a/py/objfun.c b/py/objfun.c index afac3889fd..eb24ea8763 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -17,21 +17,44 @@ // mp_obj_fun_native_t defined in obj.h +void check_nargs(mp_obj_fun_native_t *self, int n_args, int n_kw) { + if (n_kw && !self->is_kw) { + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, + "function does not take keyword arguments")); + } + + if (self->n_args_min == self->n_args_max) { + if (n_args != self->n_args_min) { + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, + "function takes %d positional arguments but %d were given", + (const char*)(machine_int_t)self->n_args_min, + (const char*)(machine_int_t)n_args)); + } + } else { + if (n_args < self->n_args_min) { + nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, + "() missing %d required positional arguments: ", + (const char*)(machine_int_t)(self->n_args_min - n_args))); + } else if (n_args > self->n_args_max) { + nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, + " expected at most %d arguments, got %d", + (void*)(machine_int_t)self->n_args_max, (void*)(machine_int_t)n_args)); + } + } +} + mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args); // args are in reverse order in the array mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { mp_obj_fun_native_t *self = self_in; + // check number of arguments + check_nargs(self, n_args, 0); if (self->is_kw) { return fun_native_call_n_kw(self_in, n_args, 0, args); } if (self->n_args_min == self->n_args_max) { // function requires a fixed number of arguments - // check number of arguments - if (n_args != self->n_args_min) { - nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args_min, (const char*)(machine_int_t)n_args)); - } - // dispatch function call switch (self->n_args_min) { case 0: @@ -54,12 +77,6 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { } else { // function takes a variable number of arguments - if (n_args < self->n_args_min) { - nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "() missing %d required positional arguments: ", (const char*)(machine_int_t)(self->n_args_min - n_args))); - } else if (n_args > self->n_args_max) { - nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, " expected at most %d arguments, got %d", (void*)(machine_int_t)self->n_args_max, (void*)(machine_int_t)n_args)); - } - // TODO really the args need to be passed in as a Python tuple, as the form f(*[1,2]) can be used to pass var args mp_obj_t *args_ordered = m_new(mp_obj_t, n_args); for (int i = 0; i < n_args; i++) { @@ -76,9 +93,7 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args) { mp_obj_fun_native_t *self = self_in; - if (!self->is_kw) { - nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "function does not take keyword arguments")); - } + check_nargs(self, n_args, n_kw); mp_obj_t *vargs = mp_obj_new_tuple_reverse(n_args, args + 2*n_kw); mp_map_t *kw_args = mp_map_new(n_kw); diff --git a/py/objlist.c b/py/objlist.c index d5ea7f47b2..f806dfae8f 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -381,7 +381,7 @@ static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert); static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop); static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove); static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse); -static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort); +static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, list_sort); static const mp_method_t list_type_methods[] = { { "append", &list_append_obj },