From c8effff937442be01d36d56c77054bbdee3f3aff Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Sun, 12 Jan 2014 22:34:58 -0800 Subject: [PATCH 1/6] Added public domain implementations of strchr and strstr. --- py/objstr.c | 1 + stm/std.h | 2 ++ stm/string0.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/py/objstr.c b/py/objstr.c index be1f00e686..48a21c9caf 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "nlr.h" #include "misc.h" diff --git a/stm/std.h b/stm/std.h index 587b9f8880..95c606e058 100644 --- a/stm/std.h +++ b/stm/std.h @@ -17,6 +17,8 @@ int strncmp(const char *s1, const char *s2, size_t n); char *strndup(const char *s, size_t n); char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); +char *strchr(const char *s, int c); +char *strstr(const char *haystack, const char *needle); int printf(const char *fmt, ...); int snprintf(char *str, size_t size, const char *fmt, ...); diff --git a/stm/string0.c b/stm/string0.c index 2a5f255971..d67c5f2b17 100644 --- a/stm/string0.c +++ b/stm/string0.c @@ -108,3 +108,31 @@ char *strcat(char *dest, const char *src) { *d = '\0'; return dest; } + +// Public Domain implementation of strchr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function +char *strchr(const char *s, int c) +{ + /* Scan s for the character. When this loop is finished, + s will either point to the end of the string or the + character we were looking for. */ + while (*s != '\0' && *s != (char)c) + s++; + return ((*s == c) ? (char *) s : 0); +} + + +// Public Domain implementation of strstr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function +char *strstr(const char *haystack, const char *needle) +{ + size_t needlelen; + /* Check for the null needle case. */ + if (*needle == '\0') + return (char *) haystack; + needlelen = strlen(needle); + for (; (haystack = strchr(haystack, *needle)) != 0; haystack++) + if (strncmp(haystack, needle, needlelen) == 0) + return (char *) haystack; + return 0; +} From 44332ec9ea504322cae0ca2c982e211e0c8ef67a Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Mon, 13 Jan 2014 08:42:43 -0800 Subject: [PATCH 2/6] Initialize is_kw for dynamically allocated mp_obj_fun_native_t ojects. This should fix issue #171 --- py/objfun.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/py/objfun.c b/py/objfun.c index afac3889fd..b8ebce7a39 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -101,6 +101,7 @@ const mp_obj_type_t fun_native_type = { mp_obj_t rt_make_function_0(mp_fun_0_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = 0; o->n_args_max = 0; o->fun = fun; @@ -110,6 +111,7 @@ mp_obj_t rt_make_function_0(mp_fun_0_t fun) { mp_obj_t rt_make_function_1(mp_fun_1_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = 1; o->n_args_max = 1; o->fun = fun; @@ -119,6 +121,7 @@ mp_obj_t rt_make_function_1(mp_fun_1_t fun) { mp_obj_t rt_make_function_2(mp_fun_2_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = 2; o->n_args_max = 2; o->fun = fun; @@ -128,6 +131,7 @@ mp_obj_t rt_make_function_2(mp_fun_2_t fun) { mp_obj_t rt_make_function_3(mp_fun_3_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = 3; o->n_args_max = 3; o->fun = fun; @@ -137,6 +141,7 @@ mp_obj_t rt_make_function_3(mp_fun_3_t fun) { mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = n_args_min; o->n_args_max = ~((machine_uint_t)0); o->fun = fun; @@ -147,6 +152,7 @@ mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) { mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; + o->is_kw = false; o->n_args_min = n_args_min; o->n_args_max = n_args_max; o->fun = fun; From 2300537c79dd642a7187018334a1a697a415f589 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 13 Jan 2014 19:39:01 +0000 Subject: [PATCH 3/6] Cleanup built-ins, and fix some compiler warnings/errors. --- py/builtin.c | 72 ++++++++++++++++++++++++++++++++-------------- py/builtin.h | 35 +++++++++++----------- py/builtinimport.c | 2 +- py/mpconfig.h | 2 +- py/objint.c | 4 +++ py/objstr.c | 15 ++++++---- py/runtime.c | 32 ++++++++++----------- stm/printf.c | 4 +-- 8 files changed, 101 insertions(+), 65 deletions(-) diff --git a/py/builtin.c b/py/builtin.c index 078f4b49c3..389274f319 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -18,7 +18,7 @@ // args[0] is function from class body // args[1] is class name // args[2:] are base objects -mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) { +static mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) { assert(2 <= n_args); // we differ from CPython: we set the new __locals__ object here @@ -62,7 +62,7 @@ mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__); -mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { +static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { if (o != mp_const_none) { mp_obj_print(o); printf("\n"); @@ -70,6 +70,8 @@ mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { return mp_const_none; } +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__); + mp_obj_t mp_builtin_abs(mp_obj_t o_in) { if (MP_OBJ_IS_SMALL_INT(o_in)) { mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in); @@ -97,7 +99,9 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) { } } -mp_obj_t mp_builtin_all(mp_obj_t o_in) { +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs); + +static mp_obj_t mp_builtin_all(mp_obj_t o_in) { mp_obj_t iterable = rt_getiter(o_in); mp_obj_t item; while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { @@ -108,7 +112,9 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in) { return mp_const_true; } -mp_obj_t mp_builtin_any(mp_obj_t o_in) { +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all); + +static mp_obj_t mp_builtin_any(mp_obj_t o_in) { mp_obj_t iterable = rt_getiter(o_in); mp_obj_t item; while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { @@ -119,7 +125,9 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) { return mp_const_false; } -mp_obj_t mp_builtin_callable(mp_obj_t o_in) { +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any); + +static mp_obj_t mp_builtin_callable(mp_obj_t o_in) { if (mp_obj_is_callable(o_in)) { return mp_const_true; } else { @@ -127,7 +135,9 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) { } } -mp_obj_t mp_builtin_chr(mp_obj_t o_in) { +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable); + +static mp_obj_t mp_builtin_chr(mp_obj_t o_in) { int ord = mp_obj_get_int(o_in); if (0 <= ord && ord <= 0x10ffff) { char *str = m_new(char, 2); @@ -139,7 +149,9 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) { } } -mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr); + +static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) { mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in); mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in); @@ -152,6 +164,8 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { } } +MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod); + static mp_obj_t mp_builtin_hash(mp_obj_t o_in) { // TODO hash will generally overflow small integer; can we safely truncate it? return mp_obj_new_int(mp_obj_hash(o_in)); @@ -165,7 +179,7 @@ static mp_obj_t mp_builtin_iter(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter); -mp_obj_t mp_builtin_len(mp_obj_t o_in) { +static mp_obj_t mp_builtin_len(mp_obj_t o_in) { mp_obj_t len = mp_obj_len_maybe(o_in); if (len == NULL) { nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in))); @@ -174,7 +188,9 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) { } } -mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len); + +static mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) { if (n_args == 1) { // given an iterable mp_obj_t iterable = rt_getiter(args[0]); @@ -201,7 +217,9 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) { } } -mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max); + +static mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) { if (n_args == 1) { // given an iterable mp_obj_t iterable = rt_getiter(args[0]); @@ -228,6 +246,8 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) { } } +MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min); + static mp_obj_t mp_builtin_next(mp_obj_t o) { mp_obj_t ret = rt_iternext(o); if (ret == mp_const_stop_iteration) { @@ -239,7 +259,7 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next); -mp_obj_t mp_builtin_ord(mp_obj_t o_in) { +static mp_obj_t mp_builtin_ord(mp_obj_t o_in) { const char *str = qstr_str(mp_obj_get_qstr(o_in)); if (strlen(str) == 1) { return mp_obj_new_int(str[0]); @@ -248,15 +268,19 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) { } } -mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord); + +static mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) { + assert(2 <= n_args && n_args <= 3); switch (n_args) { case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]); - case 3: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise... - default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "pow expected at most 3 arguments, got %d", n_args)); + default: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise... } } -mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow); + +static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) { for (int i = 0; i < n_args; i++) { if (i > 0) { printf(" "); @@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) { return mp_const_none; } -mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print); + +static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { + assert(1 <= n_args && n_args <= 3); switch (n_args) { case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1); case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1); - case 3: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2])); - default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "range expected at most 3 arguments, got %d", n_args)); + default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2])); } } -mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range); + +static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { + assert(1 <= n_args && n_args <= 2); mp_obj_t value; switch (n_args) { case 1: value = mp_obj_new_int(0); break; - case 2: value = args[1]; break; - default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "sum expected at most 2 arguments, got %d", n_args)); + default: value = args[1]; break; } mp_obj_t iterable = rt_getiter(args[0]); mp_obj_t item; @@ -296,3 +324,5 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { } return value; } + +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); diff --git a/py/builtin.h b/py/builtin.h index db7d517a06..0198d63bfd 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -1,25 +1,24 @@ -// TODO convert all these to objects using MP_DECLARE and MP_DEFINE +mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args); MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj); -mp_obj_t mp_builtin___import__(int n, mp_obj_t *args); -mp_obj_t mp_builtin___repl_print__(mp_obj_t o); -mp_obj_t mp_builtin_abs(mp_obj_t o_in); -mp_obj_t mp_builtin_all(mp_obj_t o_in); -mp_obj_t mp_builtin_any(mp_obj_t o_in); -mp_obj_t mp_builtin_callable(mp_obj_t o_in); -mp_obj_t mp_builtin_chr(mp_obj_t o_in); -mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin___repl_print___obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj); -mp_obj_t mp_builtin_len(mp_obj_t o_in); -mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args); -mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args); -mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_len_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_list_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj); -mp_obj_t mp_builtin_ord(mp_obj_t o_in); -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_ord_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj); diff --git a/py/builtinimport.c b/py/builtinimport.c index 90a0fc3394..33576e3f0e 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -18,7 +18,7 @@ #include "map.h" #include "builtin.h" -mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) { +mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { /* printf("import:\n"); for (int i = 0; i < n; i++) { diff --git a/py/mpconfig.h b/py/mpconfig.h index ada4aa2ea4..5d8c57692e 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -91,7 +91,7 @@ typedef long long mp_longint_impl_t; #define BITS_PER_BYTE (8) #define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD) // machine_int_t value with most significant bit set -#define WORD_MSBIT_HIGH (1 << (BYTES_PER_WORD * 8 - 1)) +#define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1)) // printf format spec to use for machine_int_t and friends #ifndef INT_FMT diff --git a/py/objint.c b/py/objint.c index 26d3c0e337..84c35c508e 100644 --- a/py/objint.c +++ b/py/objint.c @@ -54,11 +54,13 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj // This is called only for non-SMALL_INT mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { assert(0); + return mp_const_none; } // This is called only with strings whose value doesn't fit in SMALL_INT mp_obj_t mp_obj_new_int_from_long_str(const char *s) { assert(0); + return mp_const_none; } mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { @@ -69,6 +71,7 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { } // TODO: Raise exception assert(0); + return mp_const_none; } mp_obj_t mp_obj_new_int(machine_int_t value) { @@ -77,5 +80,6 @@ mp_obj_t mp_obj_new_int(machine_int_t value) { } // TODO: Raise exception assert(0); + return mp_const_none; } #endif diff --git a/py/objstr.c b/py/objstr.c index be1f00e686..f48bde6001 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -169,8 +169,8 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) { const char* haystack = qstr_str(((mp_obj_str_t*)args[0])->qstr); const char* needle = qstr_str(((mp_obj_str_t*)args[1])->qstr); - ssize_t haystack_len = strlen(haystack); - ssize_t needle_len = strlen(needle); + size_t haystack_len = strlen(haystack); + size_t needle_len = strlen(needle); size_t start = 0; size_t end = haystack_len; @@ -183,14 +183,17 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) { } char *p = strstr(haystack + start, needle); - ssize_t pos = -1; - if (p) { - pos = p - haystack; + if (p == NULL) { + // not found + return MP_OBJ_NEW_SMALL_INT(-1); + } else { + // found + machine_int_t pos = p - haystack; if (pos + needle_len > end) { pos = -1; } + return MP_OBJ_NEW_SMALL_INT(pos); } - return MP_OBJ_NEW_SMALL_INT(pos); } mp_obj_t str_strip(int n_args, const mp_obj_t *args) { diff --git a/py/runtime.c b/py/runtime.c index 2af86b6abd..766a321bc4 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -97,7 +97,7 @@ void rt_init(void) { // built-in core functions mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj); - mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, rt_make_function_1(mp_builtin___repl_print__)); + mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj); // built-in types mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type); @@ -114,26 +114,26 @@ void rt_init(void) { 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); - // 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)); - mp_map_add_qstr(&map_builtins, MP_QSTR_all, rt_make_function_1(mp_builtin_all)); - mp_map_add_qstr(&map_builtins, MP_QSTR_any, rt_make_function_1(mp_builtin_any)); - mp_map_add_qstr(&map_builtins, MP_QSTR_callable, rt_make_function_1(mp_builtin_callable)); - mp_map_add_qstr(&map_builtins, MP_QSTR_chr, rt_make_function_1(mp_builtin_chr)); - mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, rt_make_function_2(mp_builtin_divmod)); + // built-in user functions + mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj); - mp_map_add_qstr(&map_builtins, MP_QSTR_len, rt_make_function_1(mp_builtin_len)); - mp_map_add_qstr(&map_builtins, MP_QSTR_max, rt_make_function_var(1, mp_builtin_max)); - mp_map_add_qstr(&map_builtins, MP_QSTR_min, rt_make_function_var(1, mp_builtin_min)); + mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj); - mp_map_add_qstr(&map_builtins, MP_QSTR_ord, rt_make_function_1(mp_builtin_ord)); - 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_sum, rt_make_function_var(1, mp_builtin_sum)); + mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj); next_unique_code_id = 1; // 0 indicates "no code" unique_codes_alloc = 0; diff --git a/stm/printf.c b/stm/printf.c index c0fa82e1b0..732e834526 100644 --- a/stm/printf.c +++ b/stm/printf.c @@ -213,9 +213,9 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { // usable. I expect that this will be replaced with something // more appropriate. char dot = '.'; - double d = va_arg(args, double); + mp_float_t d = va_arg(args, double); int left = (int)d; - int right = (int)((d - (double)(int)d) * 1000000.0); + int right = (int)((d - (mp_float_t)(int)d) * 1000000.0); chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); From f62d33aa1dc5afad9abbaf257531bdc4b0fbfdc0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 13 Jan 2014 19:50:05 +0000 Subject: [PATCH 4/6] Consolidate rt_make_function_[0123] to rt_make_function_n. --- py/objfun.c | 37 ++++--------------------------------- py/objstr.c | 1 - py/runtime.c | 7 +------ py/runtime.h | 6 ++---- stm/audio.c | 6 +++--- stm/lcd.c | 14 +++++++------- stm/main.c | 42 +++++++++++++++++++++--------------------- stm/pybwlan.c | 10 +++++----- stm/timer.c | 8 ++++---- 9 files changed, 47 insertions(+), 84 deletions(-) diff --git a/py/objfun.c b/py/objfun.c index b8ebce7a39..ba3ce6279f 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -98,42 +98,13 @@ const mp_obj_type_t fun_native_type = { .call_n_kw = fun_native_call_n_kw, }; -mp_obj_t rt_make_function_0(mp_fun_0_t fun) { +// fun must have the correct signature for n_args fixed arguments +mp_obj_t rt_make_function_n(int n_args, void *fun) { mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); o->base.type = &fun_native_type; o->is_kw = false; - o->n_args_min = 0; - o->n_args_max = 0; - o->fun = fun; - return o; -} - -mp_obj_t rt_make_function_1(mp_fun_1_t fun) { - mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); - o->base.type = &fun_native_type; - o->is_kw = false; - o->n_args_min = 1; - o->n_args_max = 1; - o->fun = fun; - return o; -} - -mp_obj_t rt_make_function_2(mp_fun_2_t fun) { - mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); - o->base.type = &fun_native_type; - o->is_kw = false; - o->n_args_min = 2; - o->n_args_max = 2; - o->fun = fun; - return o; -} - -mp_obj_t rt_make_function_3(mp_fun_3_t fun) { - mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t); - o->base.type = &fun_native_type; - o->is_kw = false; - o->n_args_min = 3; - o->n_args_max = 3; + o->n_args_min = n_args; + o->n_args_max = n_args; o->fun = fun; return o; } diff --git a/py/objstr.c b/py/objstr.c index a1c35bad3b..f48bde6001 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -3,7 +3,6 @@ #include #include #include -#include #include "nlr.h" #include "misc.h" diff --git a/py/runtime.c b/py/runtime.c index 766a321bc4..f43e804b40 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -587,12 +587,7 @@ mp_obj_t rt_make_function_from_id(int unique_code_id) { fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_stack, c->u_byte.code); break; case MP_CODE_NATIVE: - switch (c->n_args) { - case 0: fun = rt_make_function_0(c->u_native.fun); break; - case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break; - case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break; - default: assert(0); fun = mp_const_none; - } + fun = rt_make_function_n(c->n_args, c->u_native.fun); break; case MP_CODE_INLINE_ASM: fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun); diff --git a/py/runtime.h b/py/runtime.h index ac53e14110..32cb47684f 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -12,10 +12,8 @@ void rt_store_global(qstr qstr, mp_obj_t obj); mp_obj_t rt_unary_op(int op, mp_obj_t arg); mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs); mp_obj_t rt_make_function_from_id(int unique_code_id); -mp_obj_t rt_make_function_0(mp_fun_0_t f); -mp_obj_t rt_make_function_1(mp_fun_1_t f); -mp_obj_t rt_make_function_2(mp_fun_2_t f); -mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t f); +mp_obj_t rt_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments +mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun); mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple); mp_obj_t rt_call_function_0(mp_obj_t fun); diff --git a/stm/audio.c b/stm/audio.c index 34adefbcd6..e2aa32b9fc 100644 --- a/stm/audio.c +++ b/stm/audio.c @@ -91,8 +91,8 @@ void audio_init(void) { // Python interface mp_obj_t m = mp_obj_new_module(qstr_from_str_static("audio")); - rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_1(pyb_audio_dac)); - rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_0(pyb_audio_is_full)); - rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_1(pyb_audio_fill)); + rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_n(1, pyb_audio_dac)); + rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_n(0, pyb_audio_is_full)); + rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_n(1, pyb_audio_fill)); rt_store_name(qstr_from_str_static("audio"), m); } diff --git a/stm/lcd.c b/stm/lcd.c index 70d1a26423..82e42b779d 100644 --- a/stm/lcd.c +++ b/stm/lcd.c @@ -220,13 +220,13 @@ void lcd_init(void) { // Python interface mp_obj_t m = mp_obj_new_module(qstr_from_str_static("lcd")); - rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_2(lcd_draw_pixel_8)); - rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_0(lcd_pix_clear)); - rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_2(lcd_pix_get)); - rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_2(lcd_pix_set)); - rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_2(lcd_pix_reset)); - rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_0(lcd_pix_show)); - rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_1(lcd_print)); + rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8)); + rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_n(0, lcd_pix_clear)); + rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_n(2, lcd_pix_get)); + rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_n(2, lcd_pix_set)); + rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_n(2, lcd_pix_reset)); + rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_n(0, lcd_pix_show)); + rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_n(1, lcd_print)); rt_store_name(qstr_from_str_static("lcd"), m); } diff --git a/stm/main.c b/stm/main.c index cdc4432c09..2085182c29 100644 --- a/stm/main.c +++ b/stm/main.c @@ -812,36 +812,36 @@ soft_reset: // add some functions to the python namespace { - rt_store_name(qstr_from_str_static("help"), rt_make_function_0(pyb_help)); + rt_store_name(qstr_from_str_static("help"), rt_make_function_n(0, pyb_help)); mp_obj_t m = mp_obj_new_module(qstr_from_str_static("pyb")); - rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info)); - rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_0(pyb_sd_test)); - rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_0(pyb_stop)); - rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_0(pyb_standby)); - rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_1(pyb_source_dir)); - rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_1(pyb_main)); - rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_0(pyb_sync)); - rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc)); - rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay)); - rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led)); + rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_n(0, pyb_info)); + rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_n(0, pyb_sd_test)); + rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_n(0, pyb_stop)); + rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_n(0, pyb_standby)); + rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_n(1, pyb_source_dir)); + rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_n(1, pyb_main)); + rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_n(0, pyb_sync)); + rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_n(0, pyb_gc)); + rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_n(1, pyb_delay)); + rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_n(1, pyb_led)); rt_store_attr(m, qstr_from_str_static("switch"), (mp_obj_t)&pyb_switch_obj); - rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set)); - rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_2(pyb_pwm_set)); + rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_n(2, pyb_servo_set)); + rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_n(2, pyb_pwm_set)); rt_store_attr(m, qstr_from_str_static("accel"), (mp_obj_t)&pyb_mma_read_obj); rt_store_attr(m, qstr_from_str_static("mma_read"), (mp_obj_t)&pyb_mma_read_all_obj); rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj); - rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report)); - rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read)); - rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_0(pyb_rng_get)); - rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led)); - rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo)); - rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_2(pyb_I2C)); + rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_n(1, pyb_hid_send_report)); + rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_n(0, pyb_rtc_read)); + rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_n(0, pyb_rng_get)); + rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_n(1, pyb_Led)); + rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_n(1, pyb_Servo)); + rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_n(2, pyb_I2C)); rt_store_attr(m, qstr_from_str_static("gpio"), (mp_obj_t)&pyb_gpio_obj); - rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_2(pyb_Usart)); + rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_n(2, pyb_Usart)); rt_store_name(qstr_from_str_static("pyb"), m); - rt_store_name(qstr_from_str_static("open"), rt_make_function_2(pyb_io_open)); + rt_store_name(qstr_from_str_static("open"), rt_make_function_n(2, pyb_io_open)); } // print a message to the LCD diff --git a/stm/pybwlan.c b/stm/pybwlan.c index 09d642d9c8..91d71a652b 100644 --- a/stm/pybwlan.c +++ b/stm/pybwlan.c @@ -357,11 +357,11 @@ void pyb_wlan_init(void) { mp_obj_t m = mp_obj_new_module(qstr_from_str_static("wlan")); rt_store_attr(m, qstr_from_str_static("connect"), rt_make_function_var(0, pyb_wlan_connect)); - rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_0(pyb_wlan_disconnect)); - rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_0(pyb_wlan_get_ip)); - rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_1(pyb_wlan_get_host)); - rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_2(pyb_wlan_http_get)); - rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_0(pyb_wlan_serve)); + rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_n(0, pyb_wlan_disconnect)); + rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_n(0, pyb_wlan_get_ip)); + rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_n(1, pyb_wlan_get_host)); + rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_n(2, pyb_wlan_http_get)); + rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_n(0, pyb_wlan_serve)); rt_store_name(qstr_from_str_static("wlan"), m); } diff --git a/stm/timer.c b/stm/timer.c index 2605d4b4bc..c665a461d0 100644 --- a/stm/timer.c +++ b/stm/timer.c @@ -72,10 +72,10 @@ void timer_init(void) { // Python interface mp_obj_t m = mp_obj_new_module(qstr_from_str_static("timer")); - rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_1(timer_py_set_callback)); - rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_1(timer_py_set_period)); - rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_1(timer_py_set_prescaler)); - rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_0(timer_py_get_value)); + rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_n(1, timer_py_set_callback)); + rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_n(1, timer_py_set_period)); + rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_n(1, timer_py_set_prescaler)); + rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_n(0, timer_py_get_value)); rt_store_name(qstr_from_str_static("timer"), m); } From 76a90f2f6035a8b3c2c2064fbee658c6015d7cfe Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 13 Jan 2014 22:31:01 +0200 Subject: [PATCH 5/6] Move mp_obj_int_t definition to objint.h, to reuse in long int impls. --- py/objint.c | 11 +---------- py/objint.h | 9 +++++++++ 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 py/objint.h diff --git a/py/objint.c b/py/objint.c index 84c35c508e..c0bf756f1b 100644 --- a/py/objint.c +++ b/py/objint.c @@ -8,16 +8,7 @@ #include "mpconfig.h" #include "mpqstr.h" #include "obj.h" - -typedef struct _mp_obj_int_t { - mp_obj_base_t base; -#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE - mp_longint_impl_t val; -#endif -} mp_obj_int_t; - -void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in); -mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in); +#include "objint.h" // This dispatcher function is expected to be independent of the implementation // of long int diff --git a/py/objint.h b/py/objint.h new file mode 100644 index 0000000000..14cf977be0 --- /dev/null +++ b/py/objint.h @@ -0,0 +1,9 @@ +typedef struct _mp_obj_int_t { + mp_obj_base_t base; +#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE + mp_longint_impl_t val; +#endif +} mp_obj_int_t; + +void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in); +mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in); From ca318bba0d97c66d8fb14a089d8fa269a0e1b424 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 13 Jan 2014 16:29:14 +0200 Subject: [PATCH 6/6] mp_obj_equal(): Compare small and long ints properly. By dispatching to long int methods. --- py/obj.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/py/obj.c b/py/obj.c index 2759437fd7..206bf7a24a 100644 --- a/py/obj.c +++ b/py/obj.c @@ -108,9 +108,15 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) { return val == 0; } else if (o2 == mp_const_true) { return val == 1; - } else { - return false; + } else if (MP_OBJ_IS_TYPE(o2, &int_type)) { + // If o2 is long int, dispatch to its virtual methods + mp_obj_base_t *o = o2; + if (o->type->binary_op != NULL) { + mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1); + return r == mp_const_true ? true : false; + } } + return false; } } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) { return false;