From 7a4ddd24281a7e21eeaa697644418015cf4dd650 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 24 May 2014 23:32:19 +0100 Subject: [PATCH] Add SystemExit exception and use it in unix/ and stmhal/ ports. Addresses issue #598. --- py/obj.h | 1 + py/objexcept.c | 2 +- py/qstrdefs.h | 1 + stmhal/main.c | 3 +-- unix/main.c | 7 ++++++- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/py/obj.h b/py/obj.h index 2df597de75..2a9ed3acd3 100644 --- a/py/obj.h +++ b/py/obj.h @@ -344,6 +344,7 @@ extern const mp_obj_type_t mp_type_RuntimeError; extern const mp_obj_type_t mp_type_StopIteration; extern const mp_obj_type_t mp_type_SyntaxError; extern const mp_obj_type_t mp_type_SystemError; +extern const mp_obj_type_t mp_type_SystemExit; extern const mp_obj_type_t mp_type_TypeError; extern const mp_obj_type_t mp_type_ValueError; extern const mp_obj_type_t mp_type_ZeroDivisionError; diff --git a/py/objexcept.c b/py/objexcept.c index 0006554a85..7583d07431 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -159,7 +159,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \ // List of all exceptions, arranged as in the table at: // http://docs.python.org/3.3/library/exceptions.html MP_DEFINE_EXCEPTION_BASE(BaseException) -//MP_DEFINE_EXCEPTION(SystemExit, BaseException) +MP_DEFINE_EXCEPTION(SystemExit, BaseException) //MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException) MP_DEFINE_EXCEPTION(GeneratorExit, BaseException) MP_DEFINE_EXCEPTION(Exception, BaseException) diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 1de0989190..2d4ddfea29 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -102,6 +102,7 @@ Q(OverflowError) Q(RuntimeError) Q(SyntaxError) Q(SystemError) +Q(SystemExit) Q(TypeError) Q(UnboundLocalError) Q(ValueError) diff --git a/stmhal/main.c b/stmhal/main.c index a0ee4dab44..41ebaf3bb5 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -556,7 +556,6 @@ STATIC NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) { if (n_args > 0) { rc = mp_obj_get_int(args[0]); } - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError, - "sys.exit(%d) called, is not fully implemented", rc)); + nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc))); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit); diff --git a/unix/main.c b/unix/main.c index 23dd4be631..884ee9d32d 100644 --- a/unix/main.c +++ b/unix/main.c @@ -123,6 +123,11 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, return 0; } else { // uncaught exception + // check for SystemExit + mp_obj_t exc = (mp_obj_t)nlr.ret_val; + if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) { + exit(mp_obj_get_int(mp_obj_exception_get_value(exc))); + } mp_obj_print_exception((mp_obj_t)nlr.ret_val); return 1; } @@ -383,7 +388,7 @@ STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) { if (n_args > 0) { rc = mp_obj_get_int(args[0]); } - exit(rc); + nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc))); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);