From fb36eab9cf99a966f37edb917ac7527bdb38534d Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 23 Oct 2024 18:37:46 +1100 Subject: [PATCH] ports/all: Run atexit during soft-reset. Signed-off-by: Andrew Leech --- ports/esp32/main.c | 5 +++++ ports/esp8266/main.c | 4 ++++ ports/mimxrt/main.c | 4 ++++ ports/nrf/main.c | 6 ++++++ ports/qemu/main.c | 4 ++++ ports/renesas-ra/main.c | 5 +++++ ports/rp2/main.c | 4 ++++ ports/samd/main.c | 4 ++++ ports/stm32/main.c | 5 +++++ ports/unix/main.c | 6 ++---- ports/zephyr/main.c | 5 +++++ 11 files changed, 48 insertions(+), 4 deletions(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 03dc0807a025c..f360a044f2599 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -49,6 +49,7 @@ #include "py/repl.h" #include "py/gc.h" #include "py/mphal.h" +#include "py/objmodule.h" #include "shared/readline/readline.h" #include "shared/runtime/pyexec.h" #include "shared/timeutils/timeutils.h" @@ -182,6 +183,10 @@ void mp_task(void *pvParameter) { mp_thread_deinit(); #endif + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif + gc_sweep_all(); // Free any native code pointers that point to iRAM. diff --git a/ports/esp8266/main.c b/ports/esp8266/main.c index 2dd7c1dece3a4..cd6540a7a925d 100644 --- a/ports/esp8266/main.c +++ b/ports/esp8266/main.c @@ -35,6 +35,7 @@ #include "py/mperrno.h" #include "py/mphal.h" #include "py/gc.h" +#include "py/objmodule.h" #include "extmod/misc.h" #include "extmod/modmachine.h" @@ -88,6 +89,9 @@ static void mp_reset(void) { } void soft_reset(void) { + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif gc_sweep_all(); mp_hal_stdout_tx_str("MPY: soft reboot\r\n"); mp_hal_delay_us(10000); // allow UART to flush output diff --git a/ports/mimxrt/main.c b/ports/mimxrt/main.c index adb071a7fee8f..282789875ca23 100644 --- a/ports/mimxrt/main.c +++ b/ports/mimxrt/main.c @@ -30,6 +30,7 @@ #include "py/gc.h" #include "py/mperrno.h" #include "py/stackctrl.h" +#include "py/objmodule.h" #include "shared/readline/readline.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" @@ -159,6 +160,9 @@ int main(void) { machine_uart_deinit_all(); machine_pwm_deinit_all(); soft_timer_deinit(); + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif gc_sweep_all(); mp_deinit(); } diff --git a/ports/nrf/main.c b/ports/nrf/main.c index 29550bd77a748..1ca023c3fa934 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -35,6 +35,7 @@ #include "py/lexer.h" #include "py/parse.h" #include "py/obj.h" +#include "py/objmodule.h" #include "py/runtime.h" #include "py/stackctrl.h" #include "py/gc.h" @@ -292,6 +293,11 @@ void NORETURN _start(void) { pwm_deinit_all(); #endif + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif + + gc_sweep_all(); mp_deinit(); printf("MPY: soft reboot\n"); diff --git a/ports/qemu/main.c b/ports/qemu/main.c index 042106580407d..6b9a8c919ae60 100644 --- a/ports/qemu/main.c +++ b/ports/qemu/main.c @@ -31,6 +31,7 @@ #include "py/stackctrl.h" #include "py/gc.h" #include "py/mperrno.h" +#include "py/objmodule.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" @@ -60,6 +61,9 @@ int main(int argc, char **argv) { mp_printf(&mp_plat_print, "MPY: soft reboot\n"); + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif gc_sweep_all(); mp_deinit(); } diff --git a/ports/renesas-ra/main.c b/ports/renesas-ra/main.c index febb7b6d7aaa3..bba7254826bf4 100644 --- a/ports/renesas-ra/main.c +++ b/ports/renesas-ra/main.c @@ -33,6 +33,7 @@ #include "py/gc.h" #include "py/mperrno.h" #include "py/mphal.h" +#include "py/objmodule.h" #include "shared/readline/readline.h" #include "shared/runtime/pyexec.h" #include "shared/runtime/softtimer.h" @@ -435,6 +436,10 @@ int main(void) { pyb_thread_deinit(); #endif + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif + MICROPY_BOARD_END_SOFT_RESET(&state); gc_sweep_all(); diff --git a/ports/rp2/main.c b/ports/rp2/main.c index d6bf448267152..676907988fb47 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -32,6 +32,7 @@ #include "py/gc.h" #include "py/mperrno.h" #include "py/mphal.h" +#include "py/objmodule.h" #include "extmod/modbluetooth.h" #include "extmod/modnetwork.h" #include "shared/readline/readline.h" @@ -241,6 +242,9 @@ int main(int argc, char **argv) { #if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE mp_usbd_deinit(); #endif + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif // Hook for resetting anything right at the end of a soft reset command. MICROPY_BOARD_END_SOFT_RESET(); diff --git a/ports/samd/main.c b/ports/samd/main.c index 2bbaf63e6e4c5..c5ab8e7ca2dfe 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -29,6 +29,7 @@ #include "py/gc.h" #include "py/mperrno.h" #include "py/stackctrl.h" +#include "py/objmodule.h" #include "shared/readline/readline.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" @@ -98,6 +99,9 @@ void samd_main(void) { #if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE mp_usbd_deinit(); #endif + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif gc_sweep_all(); #if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART sercom_deinit_all(); diff --git a/ports/stm32/main.c b/ports/stm32/main.c index df483602dc901..529cd9e51e3ba 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -32,6 +32,7 @@ #include "py/gc.h" #include "py/mperrno.h" #include "py/mphal.h" +#include "py/objmodule.h" #include "shared/readline/readline.h" #include "shared/runtime/pyexec.h" #include "shared/runtime/softtimer.h" @@ -698,6 +699,10 @@ void stm32_main(uint32_t reset_mode) { MP_STATE_PORT(pyb_stdio_uart) = NULL; #endif + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif + MICROPY_BOARD_END_SOFT_RESET(&state); gc_sweep_all(); diff --git a/ports/unix/main.c b/ports/unix/main.c index 58fa3ff589a55..e8f8960a0525a 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -43,6 +43,7 @@ #include "py/builtin.h" #include "py/repl.h" #include "py/gc.h" +#include "py/objmodule.h" #include "py/objstr.h" #include "py/cstack.h" #include "py/mphal.h" @@ -753,10 +754,7 @@ MP_NOINLINE int main_(int argc, char **argv) { #endif #if MICROPY_PY_SYS_ATEXIT - // Beware, the sys.settrace callback should be disabled before running sys.atexit. - if (mp_obj_is_callable(MP_STATE_VM(sys_exitfunc))) { - mp_call_function_0(MP_STATE_VM(sys_exitfunc)); - } + mp_sys_atexit_execute(); #endif #if MICROPY_PY_MICROPYTHON_MEM_INFO diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index 9fae2b3a873e5..4f482a1319cc1 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -48,6 +48,7 @@ #include "py/gc.h" #include "py/mphal.h" #include "py/stackctrl.h" +#include "py/objmodule.h" #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" #include "shared/readline/readline.h" @@ -172,6 +173,10 @@ int real_main(void) { gc_collect(); #endif + #if MICROPY_PY_SYS_ATEXIT + mp_sys_atexit_execute(); + #endif + gc_sweep_all(); mp_deinit();