unix, windows: Use core-provided KeyboardInterrupt exception object.

This commit is contained in:
Damien George 2017-04-10 17:17:22 +10:00
parent 9156c8b460
commit 6c564aa408
6 changed files with 12 additions and 15 deletions

View File

@ -425,9 +425,6 @@ MP_NOINLINE int main_(int argc, char **argv) {
mp_init(); mp_init();
// create keyboard interrupt object
MP_STATE_VM(keyboard_interrupt_obj) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
char *home = getenv("HOME"); char *home = getenv("HOME");
char *path = getenv("MICROPYPATH"); char *path = getenv("MICROPYPATH");
if (path == NULL) { if (path == NULL) {

View File

@ -156,6 +156,7 @@
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256) #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
#define MICROPY_KBD_EXCEPTION (1)
#define MICROPY_ASYNC_KBD_INTR (1) #define MICROPY_ASYNC_KBD_INTR (1)
extern const struct _mp_obj_module_t mp_module_machine; extern const struct _mp_obj_module_t mp_module_machine;
@ -283,7 +284,6 @@ void mp_unix_mark_exec(void);
#define MICROPY_PORT_ROOT_POINTERS \ #define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[50]; \ const char *readline_hist[50]; \
mp_obj_t keyboard_interrupt_obj; \
void *mmap_region_head; \ void *mmap_region_head; \
// We need to provide a declaration/definition of alloca() // We need to provide a declaration/definition of alloca()

View File

@ -47,6 +47,7 @@
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
#define MICROPY_WARNINGS (0) #define MICROPY_WARNINGS (0)
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (0) #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (0)
#define MICROPY_KBD_EXCEPTION (1)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
#define MICROPY_STREAMS_NON_BLOCK (0) #define MICROPY_STREAMS_NON_BLOCK (0)
@ -99,7 +100,6 @@ extern const struct _mp_obj_module_t mp_module_os;
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_os }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_os }, \
#define MICROPY_PORT_ROOT_POINTERS \ #define MICROPY_PORT_ROOT_POINTERS \
mp_obj_t keyboard_interrupt_obj;
////////////////////////////////////////// //////////////////////////////////////////
// Do not change anything beyond this line // Do not change anything beyond this line

View File

@ -40,20 +40,20 @@
STATIC void sighandler(int signum) { STATIC void sighandler(int signum) {
if (signum == SIGINT) { if (signum == SIGINT) {
#if MICROPY_ASYNC_KBD_INTR #if MICROPY_ASYNC_KBD_INTR
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj)); mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
sigset_t mask; sigset_t mask;
sigemptyset(&mask); sigemptyset(&mask);
// On entry to handler, its signal is blocked, and unblocked on // On entry to handler, its signal is blocked, and unblocked on
// normal exit. As we instead perform longjmp, unblock it manually. // normal exit. As we instead perform longjmp, unblock it manually.
sigprocmask(SIG_SETMASK, &mask, NULL); sigprocmask(SIG_SETMASK, &mask, NULL);
nlr_raise(MP_STATE_VM(keyboard_interrupt_obj)); nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
#else #else
if (MP_STATE_VM(mp_pending_exception) == MP_STATE_VM(keyboard_interrupt_obj)) { if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
// this is the second time we are called, so die straight away // this is the second time we are called, so die straight away
exit(1); exit(1);
} }
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj)); mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj); MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
#endif #endif
} }
} }

View File

@ -104,6 +104,7 @@
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256) #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
#define MICROPY_KBD_EXCEPTION (1)
#define MICROPY_PORT_INIT_FUNC init() #define MICROPY_PORT_INIT_FUNC init()
#define MICROPY_PORT_DEINIT_FUNC deinit() #define MICROPY_PORT_DEINIT_FUNC deinit()
@ -161,8 +162,7 @@ extern const struct _mp_obj_module_t mp_module_time;
#if MICROPY_USE_READLINE == 1 #if MICROPY_USE_READLINE == 1
#define MICROPY_PORT_ROOT_POINTERS \ #define MICROPY_PORT_ROOT_POINTERS \
char *readline_hist[50]; \ char *readline_hist[50];
mp_obj_t keyboard_interrupt_obj;
#endif #endif
#define MP_STATE_PORT MP_STATE_VM #define MP_STATE_PORT MP_STATE_VM

View File

@ -79,12 +79,12 @@ void mp_hal_stdio_mode_orig(void) {
// the thread created for handling it might not be running yet so we'd miss the notification. // the thread created for handling it might not be running yet so we'd miss the notification.
BOOL WINAPI console_sighandler(DWORD evt) { BOOL WINAPI console_sighandler(DWORD evt) {
if (evt == CTRL_C_EVENT) { if (evt == CTRL_C_EVENT) {
if (MP_STATE_VM(mp_pending_exception) == MP_STATE_VM(keyboard_interrupt_obj)) { if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
// this is the second time we are called, so die straight away // this is the second time we are called, so die straight away
exit(1); exit(1);
} }
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj)); mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj); MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
return TRUE; return TRUE;
} }
return FALSE; return FALSE;