Skip to content

Commit 6c564aa

Browse files
committed
unix, windows: Use core-provided KeyboardInterrupt exception object.
1 parent 9156c8b commit 6c564aa

6 files changed

Lines changed: 12 additions & 15 deletions

File tree

unix/main.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,6 @@ MP_NOINLINE int main_(int argc, char **argv) {
425425

426426
mp_init();
427427

428-
// create keyboard interrupt object
429-
MP_STATE_VM(keyboard_interrupt_obj) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
430-
431428
char *home = getenv("HOME");
432429
char *path = getenv("MICROPYPATH");
433430
if (path == NULL) {

unix/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156

157157
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
158158
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
159+
#define MICROPY_KBD_EXCEPTION (1)
159160
#define MICROPY_ASYNC_KBD_INTR (1)
160161

161162
extern const struct _mp_obj_module_t mp_module_machine;
@@ -283,7 +284,6 @@ void mp_unix_mark_exec(void);
283284

284285
#define MICROPY_PORT_ROOT_POINTERS \
285286
const char *readline_hist[50]; \
286-
mp_obj_t keyboard_interrupt_obj; \
287287
void *mmap_region_head; \
288288

289289
// We need to provide a declaration/definition of alloca()

unix/mpconfigport_minimal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
4848
#define MICROPY_WARNINGS (0)
4949
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (0)
50+
#define MICROPY_KBD_EXCEPTION (1)
5051
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
5152
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
5253
#define MICROPY_STREAMS_NON_BLOCK (0)
@@ -99,7 +100,6 @@ extern const struct _mp_obj_module_t mp_module_os;
99100
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_os }, \
100101

101102
#define MICROPY_PORT_ROOT_POINTERS \
102-
mp_obj_t keyboard_interrupt_obj;
103103

104104
//////////////////////////////////////////
105105
// Do not change anything beyond this line

unix/unix_mphal.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@
4040
STATIC void sighandler(int signum) {
4141
if (signum == SIGINT) {
4242
#if MICROPY_ASYNC_KBD_INTR
43-
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
43+
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
4444
sigset_t mask;
4545
sigemptyset(&mask);
4646
// On entry to handler, its signal is blocked, and unblocked on
4747
// normal exit. As we instead perform longjmp, unblock it manually.
4848
sigprocmask(SIG_SETMASK, &mask, NULL);
49-
nlr_raise(MP_STATE_VM(keyboard_interrupt_obj));
49+
nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
5050
#else
51-
if (MP_STATE_VM(mp_pending_exception) == MP_STATE_VM(keyboard_interrupt_obj)) {
51+
if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
5252
// this is the second time we are called, so die straight away
5353
exit(1);
5454
}
55-
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
56-
MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj);
55+
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
56+
MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
5757
#endif
5858
}
5959
}

windows/mpconfigport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104

105105
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
106106
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
107+
#define MICROPY_KBD_EXCEPTION (1)
107108

108109
#define MICROPY_PORT_INIT_FUNC init()
109110
#define MICROPY_PORT_DEINIT_FUNC deinit()
@@ -161,8 +162,7 @@ extern const struct _mp_obj_module_t mp_module_time;
161162

162163
#if MICROPY_USE_READLINE == 1
163164
#define MICROPY_PORT_ROOT_POINTERS \
164-
char *readline_hist[50]; \
165-
mp_obj_t keyboard_interrupt_obj;
165+
char *readline_hist[50];
166166
#endif
167167

168168
#define MP_STATE_PORT MP_STATE_VM

windows/windows_mphal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ void mp_hal_stdio_mode_orig(void) {
7979
// the thread created for handling it might not be running yet so we'd miss the notification.
8080
BOOL WINAPI console_sighandler(DWORD evt) {
8181
if (evt == CTRL_C_EVENT) {
82-
if (MP_STATE_VM(mp_pending_exception) == MP_STATE_VM(keyboard_interrupt_obj)) {
82+
if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
8383
// this is the second time we are called, so die straight away
8484
exit(1);
8585
}
86-
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
87-
MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj);
86+
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
87+
MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
8888
return TRUE;
8989
}
9090
return FALSE;

0 commit comments

Comments
 (0)