Commit 7a4ddd24 authored by Damien George's avatar Damien George
Browse files

Add SystemExit exception and use it in unix/ and stmhal/ ports.

Addresses issue #598.
parent ee3fd46f
...@@ -344,6 +344,7 @@ extern const mp_obj_type_t mp_type_RuntimeError; ...@@ -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_StopIteration;
extern const mp_obj_type_t mp_type_SyntaxError; 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_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_TypeError;
extern const mp_obj_type_t mp_type_ValueError; extern const mp_obj_type_t mp_type_ValueError;
extern const mp_obj_type_t mp_type_ZeroDivisionError; extern const mp_obj_type_t mp_type_ZeroDivisionError;
......
...@@ -159,7 +159,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \ ...@@ -159,7 +159,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \
// List of all exceptions, arranged as in the table at: // List of all exceptions, arranged as in the table at:
// http://docs.python.org/3.3/library/exceptions.html // http://docs.python.org/3.3/library/exceptions.html
MP_DEFINE_EXCEPTION_BASE(BaseException) MP_DEFINE_EXCEPTION_BASE(BaseException)
//MP_DEFINE_EXCEPTION(SystemExit, BaseException) MP_DEFINE_EXCEPTION(SystemExit, BaseException)
//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException) //MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
MP_DEFINE_EXCEPTION(GeneratorExit, BaseException) MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
MP_DEFINE_EXCEPTION(Exception, BaseException) MP_DEFINE_EXCEPTION(Exception, BaseException)
......
...@@ -102,6 +102,7 @@ Q(OverflowError) ...@@ -102,6 +102,7 @@ Q(OverflowError)
Q(RuntimeError) Q(RuntimeError)
Q(SyntaxError) Q(SyntaxError)
Q(SystemError) Q(SystemError)
Q(SystemExit)
Q(TypeError) Q(TypeError)
Q(UnboundLocalError) Q(UnboundLocalError)
Q(ValueError) Q(ValueError)
......
...@@ -556,7 +556,6 @@ STATIC NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) { ...@@ -556,7 +556,6 @@ STATIC NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
if (n_args > 0) { if (n_args > 0) {
rc = mp_obj_get_int(args[0]); rc = mp_obj_get_int(args[0]);
} }
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError, nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
"sys.exit(%d) called, is not fully implemented", rc));
} }
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
...@@ -123,6 +123,11 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, ...@@ -123,6 +123,11 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
return 0; return 0;
} else { } else {
// uncaught exception // 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); mp_obj_print_exception((mp_obj_t)nlr.ret_val);
return 1; return 1;
} }
...@@ -383,7 +388,7 @@ STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) { ...@@ -383,7 +388,7 @@ STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
if (n_args > 0) { if (n_args > 0) {
rc = mp_obj_get_int(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); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment