summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-10-08 13:11:23 +1100
committerDamien George <damien@micropython.org>2025-11-22 00:06:59 +1100
commitfd1ddc3f12b40e99faaf24dd099fd512fddcb62c (patch)
tree12fc4905df5d0e7fada2e47c11f64426bdbd3318 /shared
parente06ac9ce089c5eda9cbd9ec035cdf56fe75be0ab (diff)
shared/runtime/pyexec: Call mp_hal_stdio_mode_orig/raw as appropriate.
This ensures that ctrl-C works on the unix port when executing code at the REPL. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'shared')
-rw-r--r--shared/runtime/pyexec.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c
index 910b39c08..d2a1403e3 100644
--- a/shared/runtime/pyexec.c
+++ b/shared/runtime/pyexec.c
@@ -536,10 +536,20 @@ MP_REGISTER_ROOT_POINTER(vstr_t * repl_line);
#else // MICROPY_REPL_EVENT_DRIVEN
+#if !MICROPY_HAL_HAS_STDIO_MODE_SWITCH
+// If the port doesn't need any stdio mode switching calls then provide trivial ones.
+static inline void mp_hal_stdio_mode_raw(void) {
+}
+static inline void mp_hal_stdio_mode_orig(void) {
+}
+#endif
+
int pyexec_raw_repl(void) {
vstr_t line;
vstr_init(&line, 32);
+ mp_hal_stdio_mode_raw();
+
raw_repl_reset:
mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
@@ -553,6 +563,7 @@ raw_repl_reset:
if (vstr_len(&line) == 2 && vstr_str(&line)[0] == CHAR_CTRL_E) {
int ret = do_reader_stdin(vstr_str(&line)[1]);
if (ret & PYEXEC_FORCED_EXIT) {
+ mp_hal_stdio_mode_orig();
return ret;
}
vstr_reset(&line);
@@ -565,6 +576,7 @@ raw_repl_reset:
mp_hal_stdout_tx_str("\r\n");
vstr_clear(&line);
pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
+ mp_hal_stdio_mode_orig();
return 0;
} else if (c == CHAR_CTRL_C) {
// clear line
@@ -585,13 +597,17 @@ raw_repl_reset:
// exit for a soft reset
mp_hal_stdout_tx_str("\r\n");
vstr_clear(&line);
+ mp_hal_stdio_mode_orig();
return PYEXEC_FORCED_EXIT;
}
+ // Switch to original terminal mode to execute code, eg to support keyboard interrupt (SIGINT).
+ mp_hal_stdio_mode_orig();
int ret = parse_compile_execute(&line, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR);
if (ret & PYEXEC_FORCED_EXIT) {
return ret;
}
+ mp_hal_stdio_mode_raw();
}
}
@@ -599,6 +615,8 @@ int pyexec_friendly_repl(void) {
vstr_t line;
vstr_init(&line, 32);
+ mp_hal_stdio_mode_raw();
+
friendly_repl_reset:
mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION);
mp_hal_stdout_tx_str("; " MICROPY_BANNER_MACHINE);
@@ -640,6 +658,7 @@ friendly_repl_reset:
mp_hal_stdout_tx_str("\r\n");
vstr_clear(&line);
pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
+ mp_hal_stdio_mode_orig();
return 0;
} else if (ret == CHAR_CTRL_B) {
// reset friendly REPL
@@ -653,6 +672,7 @@ friendly_repl_reset:
// exit for a soft reset
mp_hal_stdout_tx_str("\r\n");
vstr_clear(&line);
+ mp_hal_stdio_mode_orig();
return PYEXEC_FORCED_EXIT;
} else if (ret == CHAR_CTRL_E) {
// paste mode
@@ -697,10 +717,13 @@ friendly_repl_reset:
}
}
+ // Switch to original terminal mode to execute code, eg to support keyboard interrupt (SIGINT).
+ mp_hal_stdio_mode_orig();
ret = parse_compile_execute(&line, parse_input_kind, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
if (ret & PYEXEC_FORCED_EXIT) {
return ret;
}
+ mp_hal_stdio_mode_raw();
}
}