Skip to content

Make raising SystemExit do a soft reset on bare-metal targets #15486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions extmod/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* THE SOFTWARE.
*/

#include "py/builtin.h"
#include "py/runtime.h"

#if MICROPY_PY_MACHINE
Expand All @@ -35,6 +36,10 @@
#include "drivers/dht/dht.h"
#endif

#if !MICROPY_PY_SYS_EXIT
#error MICROPY_PY_MACHINE requires MICROPY_PY_SYS_EXIT
#endif

// The port must provide implementations of these low-level machine functions.

static void mp_machine_idle(void);
Expand All @@ -61,12 +66,6 @@ NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args);
#include MICROPY_PY_MACHINE_INCLUDEFILE
#endif

static mp_obj_t machine_soft_reset(void) {
pyexec_system_exit = PYEXEC_FORCED_EXIT;
mp_raise_type(&mp_type_SystemExit);
}
static MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);

#if MICROPY_PY_MACHINE_BOOTLOADER
NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args) {
mp_machine_bootloader(n_args, args);
Expand Down Expand Up @@ -157,7 +156,7 @@ static const mp_rom_map_elem_t machine_module_globals_table[] = {
#endif

// Reset related functions.
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&mp_sys_exit_obj) },
#if MICROPY_PY_MACHINE_BOOTLOADER
{ MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) },
#endif
Expand Down
3 changes: 0 additions & 3 deletions ports/qemu-arm/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
// This file is never compiled standalone, it's included directly from
// extmod/modmachine.c via MICROPY_PY_MACHINE_INCLUDEFILE.

// This variable is needed for machine.soft_reset(), but the variable is otherwise unused.
int pyexec_system_exit = 0;

static void mp_machine_idle(void) {
// Do nothing.
}
3 changes: 0 additions & 3 deletions ports/unix/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
#define MICROPY_PAGE_MASK (MICROPY_PAGE_SIZE - 1)
#endif

// This variable is needed for machine.soft_reset(), but the variable is otherwise unused.
int pyexec_system_exit = 0;

uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) {
uintptr_t addr = mp_obj_get_int_truncated(addr_o);
if ((addr & (align - 1)) != 0) {
Expand Down
2 changes: 2 additions & 0 deletions py/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ MP_DECLARE_CONST_FUN_OBJ_2(mp_op_getitem_obj);
MP_DECLARE_CONST_FUN_OBJ_3(mp_op_setitem_obj);
MP_DECLARE_CONST_FUN_OBJ_2(mp_op_delitem_obj);

MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj);

// Modules needed by the runtime.
extern const mp_obj_dict_t mp_module_builtins_globals;
extern const mp_obj_module_t mp_module___main__;
Expand Down
6 changes: 1 addition & 5 deletions shared/runtime/pyexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#include "genhdr/mpversion.h"

pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
int pyexec_system_exit = 0;

#if MICROPY_REPL_INFO
static bool repl_display_debugging_info = 0;
Expand Down Expand Up @@ -74,9 +73,6 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
MICROPY_BOARD_BEFORE_PYTHON_EXEC(input_kind, exec_flags);
#endif

// by default a SystemExit exception returns 0
pyexec_system_exit = 0;

nlr_buf_t nlr;
nlr.ret_val = NULL;
if (nlr_push(&nlr) == 0) {
Expand Down Expand Up @@ -146,7 +142,7 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
// check for SystemExit
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) {
// at the moment, the value of SystemExit is unused
ret = pyexec_system_exit;
ret = PYEXEC_FORCED_EXIT;
} else {
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
ret = 0;
Expand Down
5 changes: 0 additions & 5 deletions shared/runtime/pyexec.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ typedef enum {

extern pyexec_mode_kind_t pyexec_mode_kind;

// Set this to the value (eg PYEXEC_FORCED_EXIT) that will be propagated through
// the pyexec functions if a SystemExit exception is raised by the running code.
// It will reset to 0 at the start of each execution (eg each REPL entry).
extern int pyexec_system_exit;

#define PYEXEC_FORCED_EXIT (0x100)

int pyexec_raw_repl(void);
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy