Skip to content

Commit 7a4ddd2

Browse files
committed
Add SystemExit exception and use it in unix/ and stmhal/ ports.
Addresses issue adafruit#598.
1 parent ee3fd46 commit 7a4ddd2

5 files changed

Lines changed: 10 additions & 4 deletions

File tree

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ extern const mp_obj_type_t mp_type_RuntimeError;
344344
extern const mp_obj_type_t mp_type_StopIteration;
345345
extern const mp_obj_type_t mp_type_SyntaxError;
346346
extern const mp_obj_type_t mp_type_SystemError;
347+
extern const mp_obj_type_t mp_type_SystemExit;
347348
extern const mp_obj_type_t mp_type_TypeError;
348349
extern const mp_obj_type_t mp_type_ValueError;
349350
extern const mp_obj_type_t mp_type_ZeroDivisionError;

py/objexcept.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \
159159
// List of all exceptions, arranged as in the table at:
160160
// http://docs.python.org/3.3/library/exceptions.html
161161
MP_DEFINE_EXCEPTION_BASE(BaseException)
162-
//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
162+
MP_DEFINE_EXCEPTION(SystemExit, BaseException)
163163
//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
164164
MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
165165
MP_DEFINE_EXCEPTION(Exception, BaseException)

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Q(OverflowError)
102102
Q(RuntimeError)
103103
Q(SyntaxError)
104104
Q(SystemError)
105+
Q(SystemExit)
105106
Q(TypeError)
106107
Q(UnboundLocalError)
107108
Q(ValueError)

stmhal/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ STATIC NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
556556
if (n_args > 0) {
557557
rc = mp_obj_get_int(args[0]);
558558
}
559-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError,
560-
"sys.exit(%d) called, is not fully implemented", rc));
559+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
561560
}
562561
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);

unix/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
123123
return 0;
124124
} else {
125125
// uncaught exception
126+
// check for SystemExit
127+
mp_obj_t exc = (mp_obj_t)nlr.ret_val;
128+
if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
129+
exit(mp_obj_get_int(mp_obj_exception_get_value(exc)));
130+
}
126131
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
127132
return 1;
128133
}
@@ -383,7 +388,7 @@ STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
383388
if (n_args > 0) {
384389
rc = mp_obj_get_int(args[0]);
385390
}
386-
exit(rc);
391+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
387392
}
388393
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
389394

0 commit comments

Comments
 (0)