From 7f1da0a03b1559080ca4d054bd38f104bde168e6 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Thu, 15 Dec 2016 13:00:19 +1100
Subject: [PATCH] py: Add MICROPY_KBD_EXCEPTION config option to provide
 mp_kbd_exception.

Defining and initialising mp_kbd_exception is boiler-plate code and so the
core runtime can provide it, instead of each port needing to do it
themselves.

The exception object is placed in the VM state rather than on the heap.
---
 py/mpconfig.h | 5 +++++
 py/mpstate.h  | 5 +++++
 py/runtime.c  | 9 +++++++++
 3 files changed, 19 insertions(+)

diff --git a/py/mpconfig.h b/py/mpconfig.h
index ea90ec984..7a71ebd95 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -446,6 +446,11 @@
 #   endif
 #endif
 
+// Whether to provide the mp_kbd_exception object
+#ifndef MICROPY_KBD_EXCEPTION
+#define MICROPY_KBD_EXCEPTION (0)
+#endif
+
 // Prefer to raise KeyboardInterrupt asynchronously (from signal or interrupt
 // handler) - if supported by a particular port.
 #ifndef MICROPY_ASYNC_KBD_INTR
diff --git a/py/mpstate.h b/py/mpstate.h
index 439ed6606..91fb68b3a 100644
--- a/py/mpstate.h
+++ b/py/mpstate.h
@@ -118,6 +118,11 @@ typedef struct _mp_state_vm_t {
     #endif
     #endif
 
+    #if MICROPY_KBD_EXCEPTION
+    // exception object of type KeyboardInterrupt
+    mp_obj_exception_t mp_kbd_exception;
+    #endif
+
     // dictionary with loaded modules (may be exposed as sys.modules)
     mp_obj_dict_t mp_loaded_modules_dict;
 
diff --git a/py/runtime.c b/py/runtime.c
index e7e35a081..8b4420926 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -68,6 +68,15 @@ void mp_init(void) {
     mp_init_emergency_exception_buf();
 #endif
 
+    #if MICROPY_KBD_EXCEPTION
+    // initialise the exception object for raising KeyboardInterrupt
+    MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt;
+    MP_STATE_VM(mp_kbd_exception).traceback_alloc = 0;
+    MP_STATE_VM(mp_kbd_exception).traceback_len = 0;
+    MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
+    MP_STATE_VM(mp_kbd_exception).args = mp_const_empty_tuple;
+    #endif
+
     // call port specific initialization if any
 #ifdef MICROPY_PORT_INIT_FUNC
     MICROPY_PORT_INIT_FUNC;
-- 
GitLab