diff options
Diffstat (limited to 'py')
46 files changed, 151 insertions, 123 deletions
diff --git a/py/argcheck.c b/py/argcheck.c index bfaa5764f..4b849d1fe 100644 --- a/py/argcheck.c +++ b/py/argcheck.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/builtin.c b/py/builtin.c index 250a3558e..5ff40be0c 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -1,9 +1,9 @@ #include <stdio.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "objstr.h" @@ -245,7 +245,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) { mp_obj_t max_obj = NULL; mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { - if (max_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, max_obj, item)) { + if (max_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, max_obj, item) == mp_const_true)) { max_obj = item; } } @@ -257,7 +257,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) { // given many args mp_obj_t max_obj = args[0]; for (int i = 1; i < n_args; i++) { - if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i])) { + if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i]) == mp_const_true) { max_obj = args[i]; } } @@ -274,7 +274,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) { mp_obj_t min_obj = NULL; mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { - if (min_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, item, min_obj)) { + if (min_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, item, min_obj) == mp_const_true)) { min_obj = item; } } @@ -286,7 +286,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) { // given many args mp_obj_t min_obj = args[0]; for (int i = 1; i < n_args; i++) { - if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj)) { + if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj) == mp_const_true) { min_obj = args[i]; } } diff --git a/py/builtinevex.c b/py/builtinevex.c index ae8253737..bc3adb625 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -1,8 +1,8 @@ #include <stdint.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "lexer.h" #include "lexerunix.h" diff --git a/py/builtinimport.c b/py/builtinimport.c index 07978e61d..f4e089b5d 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -1,16 +1,12 @@ #include <stdint.h> -#include <stdlib.h> #include <stdio.h> #include <string.h> #include <assert.h> -#ifdef __MINGW32__ -// For alloca() -#include <malloc.h> -#endif +#include <alloca.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "lexer.h" #include "lexerunix.h" @@ -3,7 +3,6 @@ #include <limits.h> #include <setjmp.h> -#include "mpconfig.h" typedef struct _nlr_buf_t nlr_buf_t; struct _nlr_buf_t { diff --git a/py/nlrsetjmp.c b/py/nlrsetjmp.c index a97c8634b..a8756d9d8 100644 --- a/py/nlrsetjmp.c +++ b/py/nlrsetjmp.c @@ -1,5 +1,6 @@ #include <setjmp.h> #include <stdio.h> +#include "mpconfig.h" #include "nlr.h" #if MICROPY_NLR_SETJMP @@ -2,9 +2,9 @@ #include <stdarg.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" @@ -169,9 +169,10 @@ typedef mp_obj_t (*mp_fun_var_t)(uint n, const mp_obj_t *); typedef mp_obj_t (*mp_fun_kw_t)(uint n, const mp_obj_t *, mp_map_t *); typedef enum { - PRINT_STR, - PRINT_REPR, - PRINT_EXC, // Special format for printing exception in unhandled exception message + PRINT_STR = 0, + PRINT_REPR = 1, + PRINT_EXC = 2, // Special format for printing exception in unhandled exception message + PRINT_EXC_SUBCLASS = 4, // Internal flag for printing exception subclasses } mp_print_kind_t; typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind); @@ -424,6 +425,7 @@ mp_float_t mp_obj_int_as_float(mp_obj_t self_in); machine_int_t mp_obj_int_get_checked(mp_obj_t self_in); // exception +#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new) bool mp_obj_is_exception_type(mp_obj_t self_in); bool mp_obj_is_exception_instance(mp_obj_t self_in); bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type); @@ -431,6 +433,7 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in); void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block); void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values); mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in); +mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args); // str mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data); @@ -508,6 +511,8 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args, uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2); +const char *mp_obj_fun_get_name(mp_obj_t fun); +const char *mp_obj_code_get_name(const byte *code_info); mp_obj_t mp_identity(mp_obj_t self); MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj); diff --git a/py/objarray.c b/py/objarray.c index ea3765576..428e61972 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -1,9 +1,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objbool.c b/py/objbool.c index 56020914d..c479fd362 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -1,8 +1,8 @@ #include <stdlib.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objboundmeth.c b/py/objboundmeth.c index 55b75e42c..346118d51 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -1,8 +1,8 @@ #include <string.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objcell.c b/py/objcell.c index 7fdf07b04..35b44e996 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -1,6 +1,6 @@ +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" @@ -20,14 +20,13 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) { self->obj = obj; } -#if 0 +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED STATIC void cell_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_cell_t *o = o_in; - print(env, "<cell "); + print(env, "<cell %p ", o->obj); if (o->obj == MP_OBJ_NULL) { print(env, "(nil)"); } else { - //print(env, "%p", o->obj); mp_obj_print_helper(print, env, o->obj, PRINT_REPR); } print(env, ">"); @@ -36,8 +35,10 @@ STATIC void cell_print(void (*print)(void *env, const char *fmt, ...), void *env const mp_obj_type_t cell_type = { { &mp_type_type }, - .name = MP_QSTR_, // should never need to print cell type - //.print = cell_print, + .name = MP_QSTR_, // cell representation is just value in < > +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED + .print = cell_print, +#endif }; mp_obj_t mp_obj_new_cell(mp_obj_t obj) { diff --git a/py/objclosure.c b/py/objclosure.c index 09371b034..2b83cab71 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -1,8 +1,8 @@ #include <string.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "objtuple.h" @@ -38,10 +38,10 @@ mp_obj_t closure_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t * } } -#if 0 +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_closure_t *o = o_in; - print(env, "<closure %p, n_closed=%u ", o, o->n_closed); + print(env, "<closure %s at %p, n_closed=%u ", mp_obj_fun_get_name(o->fun), o, o->n_closed); for (int i = 0; i < o->n_closed; i++) { if (o->closed[i] == MP_OBJ_NULL) { print(env, "(nil)"); @@ -57,7 +57,9 @@ STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void * const mp_obj_type_t closure_type = { { &mp_type_type }, .name = MP_QSTR_closure, - //.print = closure_print, +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED + .print = closure_print, +#endif .call = closure_call, }; diff --git a/py/objcomplex.c b/py/objcomplex.c index 924420964..45cff6a18 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "parsenum.h" diff --git a/py/objdict.c b/py/objdict.c index 0654a198e..334798c37 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -2,9 +2,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "objtuple.h" diff --git a/py/objexcept.c b/py/objexcept.c index 160cf09fd..abf56ab03 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -2,12 +2,13 @@ #include <stdarg.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "objtuple.h" +#include "objtype.h" #include "runtime.h" #include "runtime0.h" @@ -30,12 +31,17 @@ const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_exception_t *o = o_in; - if (kind == PRINT_REPR) { + mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS; + bool is_subclass = kind & PRINT_EXC_SUBCLASS; + if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) { print(env, "%s", qstr_str(o->base.type->name)); - } else if (kind == PRINT_EXC) { - print(env, "%s: ", qstr_str(o->base.type->name)); } - if (kind == PRINT_STR || kind == PRINT_EXC) { + + if (k == PRINT_EXC) { + print(env, ": "); + } + + if (k == PRINT_STR || k == PRINT_EXC) { if (o->args == NULL || o->args->len == 0) { print(env, ""); return; @@ -47,7 +53,7 @@ STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ... tuple_print(print, env, o->args, kind); } -STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { +mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { mp_obj_type_t *type = type_in; if (n_kw != 0) { @@ -266,38 +272,39 @@ bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) { return mp_binary_op(MP_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true; } -void mp_obj_exception_clear_traceback(mp_obj_t self_in) { - // make sure self_in is an exception instance - // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that) - if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) { - mp_obj_exception_t *self = self_in; - - // just set the traceback to the null object - // we don't want to call any memory management functions here - self->traceback = MP_OBJ_NULL; +// traceback handling functions + +#define GET_NATIVE_EXCEPTION(self, self_in) \ + /* make sure self_in is an exception instance */ \ + assert(mp_obj_is_exception_instance(self_in)); \ + mp_obj_exception_t *self; \ + if (mp_obj_is_native_exception_instance(self_in)) { \ + self = self_in; \ + } else { \ + self = ((mp_obj_instance_t*)self_in)->subobj[0]; \ } + +void mp_obj_exception_clear_traceback(mp_obj_t self_in) { + GET_NATIVE_EXCEPTION(self, self_in); + // just set the traceback to the null object + // we don't want to call any memory management functions here + self->traceback = MP_OBJ_NULL; } void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) { - // make sure self_in is an exception instance - // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that) - if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new && self_in != &mp_emergency_exception_obj) { - mp_obj_exception_t *self = self_in; - - // for traceback, we are just using the list object for convenience, it's not really a list of Python objects - if (self->traceback == MP_OBJ_NULL) { - self->traceback = mp_obj_new_list(0, NULL); - } - mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file); - mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line); - mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block); + GET_NATIVE_EXCEPTION(self, self_in); + + // for traceback, we are just using the list object for convenience, it's not really a list of Python objects + if (self->traceback == MP_OBJ_NULL) { + self->traceback = mp_obj_new_list(0, NULL); } + mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file); + mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line); + mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block); } void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values) { - // make sure self_in is an exception instance - assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new); - mp_obj_exception_t *self = self_in; + GET_NATIVE_EXCEPTION(self, self_in); if (self->traceback == MP_OBJ_NULL) { *n = 0; diff --git a/py/objfilter.c b/py/objfilter.c index 3eacdfc9b..37cb269ad 100644 --- a/py/objfilter.c +++ b/py/objfilter.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objfloat.c b/py/objfloat.c index 924916057..14d861b66 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -4,9 +4,9 @@ #include <assert.h> #include <math.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "parsenum.h" diff --git a/py/objfun.c b/py/objfun.c index 94f6d26d7..4690dc6c8 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -1,15 +1,11 @@ #include <stdbool.h> -#include <stdlib.h> #include <string.h> #include <assert.h> -#ifdef __MINGW32__ -// For alloca() -#include <malloc.h> -#endif +#include <alloca.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "objtuple.h" @@ -126,12 +122,17 @@ mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var /******************************************************************************/ /* byte code functions */ -const char *mp_obj_fun_get_name(mp_obj_fun_bc_t *o) { - const byte *code_info = o->bytecode; +const char *mp_obj_code_get_name(const byte *code_info) { qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24); return qstr_str(block_name); } +const char *mp_obj_fun_get_name(mp_obj_t fun_in) { + mp_obj_fun_bc_t *fun = fun_in; + const byte *code_info = fun->bytecode; + return mp_obj_code_get_name(code_info); +} + #if MICROPY_CPYTHON_COMPAT STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_fun_bc_t *o = o_in; @@ -178,6 +179,8 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, ui bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args, uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2) { mp_obj_fun_bc_t *self = self_in; + DEBUG_printf("mp_obj_fun_prepare_simple_args: given: %d pos, %d kw, expected: %d pos (%d default)\n", + n_args, n_kw, self->n_pos_args, self->n_def_args); assert(n_kw == 0); assert(self->n_kwonly_args == 0); @@ -190,8 +193,12 @@ bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, co if (n_args > self->n_pos_args) { goto arg_error; } else { - extra_args -= self->n_pos_args - n_args; - n_extra_args += self->n_pos_args - n_args; + if (n_args >= self->n_pos_args - self->n_def_args) { + extra_args -= self->n_pos_args - n_args; + n_extra_args += self->n_pos_args - n_args; + } else { + fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args); + } } *out_args1 = args; *out_args1_len = n_args; diff --git a/py/objgenerator.c b/py/objgenerator.c index 895c03cc2..d7d6dcaaf 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" @@ -68,7 +68,8 @@ typedef struct _mp_obj_gen_instance_t { } mp_obj_gen_instance_t; void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { - print(env, "<generator object 'fun-name' at %p>", self_in); + mp_obj_gen_instance_t *self = self_in; + print(env, "<generator object '%s' at %p>", mp_obj_code_get_name(self->code_info), self_in); } mp_obj_t gen_instance_getiter(mp_obj_t self_in) { diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c index faa8321c3..15676b452 100644 --- a/py/objgetitemiter.c +++ b/py/objgetitemiter.c @@ -1,8 +1,8 @@ #include <stdlib.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objint.c b/py/objint.c index 110876e00..bec4e920f 100644 --- a/py/objint.c +++ b/py/objint.c @@ -3,9 +3,9 @@ #include <assert.h> #include <string.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "parsenum.h" diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 7d71c5a69..d319e2746 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -2,9 +2,9 @@ #include <stdint.h> #include <string.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "mpz.h" diff --git a/py/objint_mpz.c b/py/objint_mpz.c index b94dcfee3..bfc7def35 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -3,9 +3,9 @@ #include <stdio.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "parsenumbase.h" #include "obj.h" diff --git a/py/objlist.c b/py/objlist.c index 0c55f524d..d886affff 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -1,9 +1,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objmap.c b/py/objmap.c index 45e65549d..c6f8f1657 100644 --- a/py/objmap.c +++ b/py/objmap.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objmodule.c b/py/objmodule.c index 15ccd68ac..d4a45b84a 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "objmodule.h" diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index 423998358..99a1d7bb9 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -1,7 +1,7 @@ #include <string.h> -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "nlr.h" #include "qstr.h" #include "obj.h" diff --git a/py/objnone.c b/py/objnone.c index fc55132f3..49b726bdd 100644 --- a/py/objnone.c +++ b/py/objnone.c @@ -1,8 +1,8 @@ #include <stdlib.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objobject.c b/py/objobject.c index 74a4926b3..62fd0c4c9 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -1,8 +1,8 @@ #include <stdlib.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objproperty.c b/py/objproperty.c index dd4eedebd..220388df0 100644 --- a/py/objproperty.c +++ b/py/objproperty.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objrange.c b/py/objrange.c index 8c0eadd32..acfeed7d9 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -1,8 +1,8 @@ #include <stdlib.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objset.c b/py/objset.c index db3683f60..1d10f94e8 100644 --- a/py/objset.c +++ b/py/objset.c @@ -2,9 +2,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objslice.c b/py/objslice.c index 924927db5..0488eb422 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objstr.c b/py/objstr.c index aab1a4492..fc7415fe1 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2,9 +2,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objstringio.c b/py/objstringio.c index a3a756630..8b3dfaf61 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -1,9 +1,9 @@ #include <stdio.h> #include <string.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/objtuple.c b/py/objtuple.c index e6b902d74..792c65193 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -1,9 +1,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/objtype.c b/py/objtype.c index 56a5dd937..0eb09e583 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -3,13 +3,14 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" #include "runtime.h" +#include "objtype.h" #if 0 // print debugging info #define DEBUG_PRINT (1) @@ -19,21 +20,13 @@ #endif /******************************************************************************/ -// class object -// creating an instance of a class makes one of these objects - -typedef struct _mp_obj_class_t { - mp_obj_base_t base; - mp_map_t members; - mp_obj_t subobj[]; - // TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them -} mp_obj_class_t; +// instance object #define is_native_type(type) ((type)->make_new != class_make_new) STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args); STATIC mp_obj_t mp_obj_new_class(mp_obj_t class, uint subobjs) { - mp_obj_class_t *o = m_new_obj_var(mp_obj_class_t, mp_obj_t, subobjs); + mp_obj_instance_t *o = m_new_obj_var(mp_obj_instance_t, mp_obj_t, subobjs); o->base.type = class; mp_map_init(&o->members, 0); mp_seq_clear(o->subobj, 0, subobjs, sizeof(*o->subobj)); @@ -71,7 +64,7 @@ STATIC int class_count_native_bases(const mp_obj_type_t *type, const mp_obj_type // it was - because instance->subobj[0] is of that type. The only exception is when // object is not yet constructed, then we need to know base native type to construct // instance->subobj[0]. This case is handled via class_count_native_bases() though. -STATIC void mp_obj_class_lookup(mp_obj_class_t *o, const mp_obj_type_t *type, qstr attr, machine_uint_t meth_offset, mp_obj_t *dest) { +STATIC void mp_obj_class_lookup(mp_obj_instance_t *o, const mp_obj_type_t *type, qstr attr, machine_uint_t meth_offset, mp_obj_t *dest) { assert(dest[0] == NULL); assert(dest[1] == NULL); for (;;) { @@ -138,7 +131,7 @@ STATIC void mp_obj_class_lookup(mp_obj_class_t *o, const mp_obj_type_t *type, qs } STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { - mp_obj_class_t *self = self_in; + mp_obj_instance_t *self = self_in; qstr meth = (kind == PRINT_STR) ? MP_QSTR___str__ : MP_QSTR___repr__; mp_obj_t member[2] = {MP_OBJ_NULL}; mp_obj_class_lookup(self, self->base.type, meth, offsetof(mp_obj_type_t, print), member); @@ -148,7 +141,15 @@ STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *en } if (member[0] == MP_OBJ_SENTINEL) { - mp_obj_print_helper(print, env, self->subobj[0], kind); + // Handle Exception subclasses specially + if (mp_obj_is_native_exception_instance(self->subobj[0])) { + if (kind != PRINT_STR) { + print(env, "%s", qstr_str(self->base.type->name)); + } + mp_obj_print_helper(print, env, self->subobj[0], kind | PRINT_EXC_SUBCLASS); + } else { + mp_obj_print_helper(print, env, self->subobj[0], kind); + } return; } @@ -170,7 +171,7 @@ STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const m uint num_native_bases = class_count_native_bases(self, &native_base); assert(num_native_bases < 2); - mp_obj_class_t *o = mp_obj_new_class(self_in, num_native_bases); + mp_obj_instance_t *o = mp_obj_new_class(self_in, num_native_bases); // look for __init__ function mp_obj_t init_fn[2] = {MP_OBJ_NULL}; @@ -188,7 +189,7 @@ STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const m // now call Python class __init__ function with all args mp_obj_t init_ret; if (n_args == 0 && n_kw == 0) { - init_ret = mp_call_function_n_kw(init_fn[0], 1, 0, (mp_obj_t*)&o); + init_ret = mp_call_function_n_kw(init_fn[0], 1, 0, (mp_obj_t*)(void*)&o); } else { mp_obj_t *args2 = m_new(mp_obj_t, 1 + n_args + 2 * n_kw); args2[0] = o; @@ -219,7 +220,7 @@ STATIC const qstr unary_op_method_name[] = { }; STATIC mp_obj_t class_unary_op(int op, mp_obj_t self_in) { - mp_obj_class_t *self = self_in; + mp_obj_instance_t *self = self_in; qstr op_name = unary_op_method_name[op]; /* Still try to lookup native slot if (op_name == 0) { @@ -303,7 +304,7 @@ STATIC void class_convert_return_attr(mp_obj_t self, mp_obj_t member, mp_obj_t * STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { // Note: For ducktyping, CPython does not look in the instance members or use // __getattr__ or __getattribute__. It only looks in the class dictionary. - mp_obj_class_t *lhs = lhs_in; + mp_obj_instance_t *lhs = lhs_in; qstr op_name = binary_op_method_name[op]; /* Still try to lookup native slot if (op_name == 0) { @@ -327,7 +328,7 @@ STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // logic: look in obj members then class locals (TODO check this against CPython) - mp_obj_class_t *self = self_in; + mp_obj_instance_t *self = self_in; mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); if (elem != NULL) { @@ -380,7 +381,7 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { - mp_obj_class_t *self = self_in; + mp_obj_instance_t *self = self_in; #if MICROPY_ENABLE_PROPERTY // for property, we need to do a lookup first in the class dict @@ -414,7 +415,7 @@ STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { } STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { - mp_obj_class_t *self = self_in; + mp_obj_instance_t *self = self_in; mp_obj_t member[2] = {MP_OBJ_NULL}; uint meth_args; if (value == MP_OBJ_NULL) { @@ -447,7 +448,7 @@ STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } STATIC mp_obj_t class_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { - mp_obj_class_t *self = self_in; + mp_obj_instance_t *self = self_in; mp_obj_t member[2] = {MP_OBJ_NULL}; mp_obj_class_lookup(self, self->base.type, MP_QSTR___call__, offsetof(mp_obj_type_t, call), member); if (member[0] == MP_OBJ_NULL) { diff --git a/py/objtype.h b/py/objtype.h new file mode 100644 index 000000000..c3176c328 --- /dev/null +++ b/py/objtype.h @@ -0,0 +1,8 @@ +// instance object +// creating an instance of a class makes one of these objects +typedef struct _mp_obj_instance_t { + mp_obj_base_t base; + mp_map_t members; + mp_obj_t subobj[]; + // TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them +} mp_obj_instance_t; diff --git a/py/objzip.c b/py/objzip.c index 4c7070b7c..c3bffd830 100644 --- a/py/objzip.c +++ b/py/objzip.c @@ -1,8 +1,8 @@ #include <stdlib.h> #include <assert.h> -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/opmethods.c b/py/opmethods.c index a745ec122..b029eef7c 100644 --- a/py/opmethods.c +++ b/py/opmethods.c @@ -1,6 +1,6 @@ +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime.h" diff --git a/py/runtime.c b/py/runtime.c index b56740a02..2928de8a7 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -2,9 +2,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "objtuple.h" diff --git a/py/sequence.c b/py/sequence.c index f91bf43c7..9fce1c51a 100644 --- a/py/sequence.c +++ b/py/sequence.c @@ -2,9 +2,9 @@ #include <stdbool.h> #include <string.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "runtime0.h" diff --git a/py/showbc.c b/py/showbc.c index e3032e8d9..b5b5ce44a 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -1,8 +1,8 @@ #include <stdio.h> #include <assert.h> -#include "misc.h" #include "mpconfig.h" +#include "misc.h" #include "qstr.h" #include "bc0.h" diff --git a/py/stream.c b/py/stream.c index 72e6ab509..113eaa46e 100644 --- a/py/stream.c +++ b/py/stream.c @@ -1,8 +1,8 @@ #include <string.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "stream.h" @@ -2,9 +2,9 @@ #include <string.h> #include <assert.h> +#include "mpconfig.h" #include "nlr.h" #include "misc.h" -#include "mpconfig.h" #include "qstr.h" #include "obj.h" #include "emitglue.h" |
