From 6c564aa408faf5d2769785b7ffc438a489310c3b Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Mon, 10 Apr 2017 17:17:22 +1000
Subject: [PATCH] unix, windows: Use core-provided KeyboardInterrupt exception
 object.

---
 unix/main.c                 |  3 ---
 unix/mpconfigport.h         |  2 +-
 unix/mpconfigport_minimal.h |  2 +-
 unix/unix_mphal.c           | 10 +++++-----
 windows/mpconfigport.h      |  4 ++--
 windows/windows_mphal.c     |  6 +++---
 6 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/unix/main.c b/unix/main.c
index a1c204cab..edea7900d 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -425,9 +425,6 @@ MP_NOINLINE int main_(int argc, char **argv) {
 
     mp_init();
 
-    // create keyboard interrupt object
-    MP_STATE_VM(keyboard_interrupt_obj) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
-
     char *home = getenv("HOME");
     char *path = getenv("MICROPYPATH");
     if (path == NULL) {
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 7ecfc28c0..4067e1950 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -156,6 +156,7 @@
 
 #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF   (1)
 #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE  (256)
+#define MICROPY_KBD_EXCEPTION       (1)
 #define MICROPY_ASYNC_KBD_INTR      (1)
 
 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 \
     const char *readline_hist[50]; \
-    mp_obj_t keyboard_interrupt_obj; \
     void *mmap_region_head; \
 
 // We need to provide a declaration/definition of alloca()
diff --git a/unix/mpconfigport_minimal.h b/unix/mpconfigport_minimal.h
index 788c8519d..b4d9f8143 100644
--- a/unix/mpconfigport_minimal.h
+++ b/unix/mpconfigport_minimal.h
@@ -47,6 +47,7 @@
 #define MICROPY_ERROR_REPORTING     (MICROPY_ERROR_REPORTING_TERSE)
 #define MICROPY_WARNINGS            (0)
 #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF   (0)
+#define MICROPY_KBD_EXCEPTION       (1)
 #define MICROPY_FLOAT_IMPL          (MICROPY_FLOAT_IMPL_NONE)
 #define MICROPY_LONGINT_IMPL        (MICROPY_LONGINT_IMPL_NONE)
 #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 }, \
 
 #define MICROPY_PORT_ROOT_POINTERS \
-    mp_obj_t keyboard_interrupt_obj;
 
 //////////////////////////////////////////
 // Do not change anything beyond this line
diff --git a/unix/unix_mphal.c b/unix/unix_mphal.c
index 6c6666236..800484498 100644
--- a/unix/unix_mphal.c
+++ b/unix/unix_mphal.c
@@ -40,20 +40,20 @@
 STATIC void sighandler(int signum) {
     if (signum == SIGINT) {
         #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;
         sigemptyset(&mask);
         // On entry to handler, its signal is blocked, and unblocked on
         // normal exit. As we instead perform longjmp, unblock it manually.
         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
-        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
             exit(1);
         }
-        mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
-        MP_STATE_VM(mp_pending_exception) = 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_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
         #endif
     }
 }
diff --git a/windows/mpconfigport.h b/windows/mpconfigport.h
index 844e2618f..b91662af4 100644
--- a/windows/mpconfigport.h
+++ b/windows/mpconfigport.h
@@ -104,6 +104,7 @@
 
 #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF   (1)
 #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE     (256)
+#define MICROPY_KBD_EXCEPTION       (1)
 
 #define MICROPY_PORT_INIT_FUNC      init()
 #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
 #define MICROPY_PORT_ROOT_POINTERS \
-    char *readline_hist[50]; \
-    mp_obj_t keyboard_interrupt_obj;
+    char *readline_hist[50];
 #endif
 
 #define MP_STATE_PORT               MP_STATE_VM
diff --git a/windows/windows_mphal.c b/windows/windows_mphal.c
index 3ad693905..1dd3105d8 100644
--- a/windows/windows_mphal.c
+++ b/windows/windows_mphal.c
@@ -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.
 BOOL WINAPI console_sighandler(DWORD evt) {
     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
             exit(1);
         }
-        mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
-        MP_STATE_VM(mp_pending_exception) = 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_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
         return TRUE;
     }
     return FALSE;
-- 
GitLab