Skip to content

Commit 7f1da0a

Browse files
committed
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.
1 parent 979ab4e commit 7f1da0a

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,11 @@
446446
# endif
447447
#endif
448448

449+
// Whether to provide the mp_kbd_exception object
450+
#ifndef MICROPY_KBD_EXCEPTION
451+
#define MICROPY_KBD_EXCEPTION (0)
452+
#endif
453+
449454
// Prefer to raise KeyboardInterrupt asynchronously (from signal or interrupt
450455
// handler) - if supported by a particular port.
451456
#ifndef MICROPY_ASYNC_KBD_INTR

py/mpstate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ typedef struct _mp_state_vm_t {
118118
#endif
119119
#endif
120120

121+
#if MICROPY_KBD_EXCEPTION
122+
// exception object of type KeyboardInterrupt
123+
mp_obj_exception_t mp_kbd_exception;
124+
#endif
125+
121126
// dictionary with loaded modules (may be exposed as sys.modules)
122127
mp_obj_dict_t mp_loaded_modules_dict;
123128

py/runtime.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ void mp_init(void) {
6868
mp_init_emergency_exception_buf();
6969
#endif
7070

71+
#if MICROPY_KBD_EXCEPTION
72+
// initialise the exception object for raising KeyboardInterrupt
73+
MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt;
74+
MP_STATE_VM(mp_kbd_exception).traceback_alloc = 0;
75+
MP_STATE_VM(mp_kbd_exception).traceback_len = 0;
76+
MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
77+
MP_STATE_VM(mp_kbd_exception).args = mp_const_empty_tuple;
78+
#endif
79+
7180
// call port specific initialization if any
7281
#ifdef MICROPY_PORT_INIT_FUNC
7382
MICROPY_PORT_INIT_FUNC;

0 commit comments

Comments
 (0)