diff --git a/py/objexcept.c b/py/objexcept.c index 976535754b..24fb564e23 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -155,6 +155,12 @@ void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kin mp_obj_tuple_print(print, MP_OBJ_FROM_PTR(o->args), kind); } +void mp_obj_exception_initialize0(mp_obj_exception_t *o_exc, const mp_obj_type_t *type) { + o_exc->base.type = type; + o_exc->args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj; + mp_obj_exception_clear_traceback(o_exc); +} + mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false); @@ -583,6 +589,12 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) { // just set the traceback to the empty traceback object // we don't want to call any memory management functions here self->traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj; + #if MICROPY_CPYTHON_EXCEPTION_CHAIN + self->cause = 0; + self->context = 0; + self->suppress_context = false; + self->marked = false; + #endif } void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) { diff --git a/py/objexcept.h b/py/objexcept.h index 77b338e951..1230fdf18a 100644 --- a/py/objexcept.h +++ b/py/objexcept.h @@ -43,6 +43,7 @@ typedef struct _mp_obj_exception_t { void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind); void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); +void mp_obj_exception_initialize0(mp_obj_exception_t *o_exc, const mp_obj_type_t *type); mp_obj_exception_t *mp_obj_exception_get_native(mp_obj_t self_in); #define MP_DEFINE_EXCEPTION(exc_name, base_name) \ diff --git a/py/objgenerator.c b/py/objgenerator.c index 2256911e23..7c3ec99307 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -40,10 +40,11 @@ // Instance of GeneratorExit exception - needed by generator.close() #if MICROPY_CONST_GENERATOREXIT_OBJ const +mp_obj_exception_t mp_static_GeneratorExit_obj = {{&mp_type_GeneratorExit}, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj, (mp_obj_traceback_t *)&mp_const_empty_traceback_obj}; #else static +mp_obj_exception_t mp_static_GeneratorExit_obj; #endif -mp_obj_exception_t mp_static_GeneratorExit_obj = {{&mp_type_GeneratorExit}, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj, (mp_obj_traceback_t *)&mp_const_empty_traceback_obj}; /******************************************************************************/ /* generator wrapper */ @@ -370,9 +371,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_ins static mp_obj_t generatorexit(void) { #if MICROPY_CPYTHON_EXCEPTION_CHAIN MP_STATIC_ASSERT(!MICROPY_CONST_GENERATOREXIT_OBJ); - mp_static_GeneratorExit_obj.context = NULL; - mp_static_GeneratorExit_obj.cause = NULL; - mp_static_GeneratorExit_obj.suppress_context = false; + mp_obj_exception_initialize0(&mp_static_GeneratorExit_obj, &mp_type_GeneratorExit); #endif return MP_OBJ_FROM_PTR(&mp_static_GeneratorExit_obj); } diff --git a/py/runtime.c b/py/runtime.c index 9779456d0c..9227594d83 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -78,14 +78,10 @@ void mp_init(void) { #if MICROPY_KBD_EXCEPTION // initialise the exception object for raising KeyboardInterrupt - MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt; - MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj; - MP_STATE_VM(mp_kbd_exception).traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj; + mp_obj_exception_initialize0(&MP_STATE_VM(mp_kbd_exception), &mp_type_KeyboardInterrupt); #endif - MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException; - MP_STATE_VM(mp_reload_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj; - MP_STATE_VM(mp_reload_exception).traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj; + mp_obj_exception_initialize0(&MP_STATE_VM(mp_reload_exception), &mp_type_ReloadException); // call port specific initialization if any #ifdef MICROPY_PORT_INIT_FUNC