diff options
| author | Roy Hooper <rhooper@toybox.ca> | 2018-05-14 16:57:50 -0400 |
|---|---|---|
| committer | Roy Hooper <rhooper@toybox.ca> | 2018-05-14 16:57:50 -0400 |
| commit | 567f3e30a77f286713b697a0f8a85e8b31a4a27a (patch) | |
| tree | 2b0b986dbe8711ff0bf7c8cbd474e89c69222e55 | |
| parent | a0954b9e11f9d768908858855fdc96d7814633c6 (diff) | |
Initial implementation of supervisor.reload()
| -rw-r--r-- | docs/library/builtins.rst | 4 | ||||
| -rw-r--r-- | py/modbuiltins.c | 1 | ||||
| -rw-r--r-- | py/mpstate.h | 3 | ||||
| -rw-r--r-- | py/obj.h | 1 | ||||
| -rw-r--r-- | py/objexcept.c | 1 | ||||
| -rw-r--r-- | py/runtime.c | 6 | ||||
| -rw-r--r-- | shared-bindings/supervisor/__init__.c | 23 |
7 files changed, 39 insertions, 0 deletions
diff --git a/docs/library/builtins.rst b/docs/library/builtins.rst index b45b6fe38..83246afc9 100644 --- a/docs/library/builtins.rst +++ b/docs/library/builtins.rst @@ -188,6 +188,10 @@ Exceptions .. exception:: RuntimeError +.. exception:: ReloadException + + `ReloadException` is used internally to deal with soft restarts. + .. exception:: StopIteration .. exception:: SyntaxError diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 5e11b8d18..cea87b181 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -700,6 +700,7 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_IndentationError), MP_ROM_PTR(&mp_type_IndentationError) }, { MP_ROM_QSTR(MP_QSTR_IndexError), MP_ROM_PTR(&mp_type_IndexError) }, { MP_ROM_QSTR(MP_QSTR_KeyboardInterrupt), MP_ROM_PTR(&mp_type_KeyboardInterrupt) }, + { MP_ROM_QSTR(MP_QSTR_ReloadException), MP_ROM_PTR(&mp_type_ReloadException) }, { MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) }, { MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) }, { MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) }, diff --git a/py/mpstate.h b/py/mpstate.h index 19a5d711e..c50947066 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -136,6 +136,9 @@ typedef struct _mp_state_vm_t { mp_obj_exception_t mp_kbd_exception; #endif + // exception object of type ReloadException + mp_obj_exception_t mp_reload_exception; + // dictionary with loaded modules (may be exposed as sys.modules) mp_obj_dict_t mp_loaded_modules_dict; @@ -590,6 +590,7 @@ extern const mp_obj_type_t mp_type_ImportError; extern const mp_obj_type_t mp_type_IndentationError; extern const mp_obj_type_t mp_type_IndexError; extern const mp_obj_type_t mp_type_KeyboardInterrupt; +extern const mp_obj_type_t mp_type_ReloadException; extern const mp_obj_type_t mp_type_KeyError; extern const mp_obj_type_t mp_type_LookupError; extern const mp_obj_type_t mp_type_MemoryError; diff --git a/py/objexcept.c b/py/objexcept.c index 4844c7f92..bcf8493c4 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -249,6 +249,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \ // http://docs.python.org/3/library/exceptions.html MP_DEFINE_EXCEPTION(SystemExit, BaseException) MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException) +MP_DEFINE_EXCEPTION(ReloadException, BaseException) MP_DEFINE_EXCEPTION(GeneratorExit, BaseException) MP_DEFINE_EXCEPTION(Exception, BaseException) #if MICROPY_PY_ASYNC_AWAIT diff --git a/py/runtime.c b/py/runtime.c index cbec82f17..a7ff57a47 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -84,6 +84,12 @@ void mp_init(void) { MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; #endif + MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException; + MP_STATE_VM(mp_reload_exception).traceback_alloc = 0; + MP_STATE_VM(mp_reload_exception).traceback_len = 0; + MP_STATE_VM(mp_reload_exception).traceback_data = NULL; + MP_STATE_VM(mp_reload_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj; + // call port specific initialization if any #ifdef MICROPY_PORT_INIT_FUNC MICROPY_PORT_INIT_FUNC; diff --git a/shared-bindings/supervisor/__init__.c b/shared-bindings/supervisor/__init__.c index a82459ad7..9a3fa855a 100644 --- a/shared-bindings/supervisor/__init__.c +++ b/shared-bindings/supervisor/__init__.c @@ -25,11 +25,15 @@ */ #include "py/obj.h" #include "py/runtime.h" + + #include "lib/utils/interrupt_char.h" #include "supervisor/shared/autoreload.h" + #include "supervisor/shared/rgb_led_status.h" #include "shared-bindings/supervisor/__init__.h" #include "shared-bindings/supervisor/Runtime.h" + //| :mod:`supervisor` --- Supervisor settings //| ================================================= //| @@ -91,12 +95,31 @@ STATIC mp_obj_t supervisor_set_rgb_status_brightness(mp_obj_t lvl){ } MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_set_rgb_status_brightness); +//| .. method:: reload() +//| +//| Reload the microcontroller (equivalent to hitting Ctrl-D at the REPL). +//| +STATIC mp_obj_t supervisor_reload(void) { + reload_next_character = true; + + MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception)); + #if MICROPY_ENABLE_SCHEDULER + if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { + MP_STATE_VM(sched_state) = MP_SCHED_PENDING; + } + #endif + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload); + + STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) }, { MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) }, { MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) }, + { MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) }, }; STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table); |
