Skip to content

Commit 567f3e3

Browse files
committed
Initial implementation of supervisor.reload()
1 parent a0954b9 commit 567f3e3

7 files changed

Lines changed: 39 additions & 0 deletions

File tree

docs/library/builtins.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ Exceptions
188188

189189
.. exception:: RuntimeError
190190

191+
.. exception:: ReloadException
192+
193+
`ReloadException` is used internally to deal with soft restarts.
194+
191195
.. exception:: StopIteration
192196

193197
.. exception:: SyntaxError

py/modbuiltins.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
700700
{ MP_ROM_QSTR(MP_QSTR_IndentationError), MP_ROM_PTR(&mp_type_IndentationError) },
701701
{ MP_ROM_QSTR(MP_QSTR_IndexError), MP_ROM_PTR(&mp_type_IndexError) },
702702
{ MP_ROM_QSTR(MP_QSTR_KeyboardInterrupt), MP_ROM_PTR(&mp_type_KeyboardInterrupt) },
703+
{ MP_ROM_QSTR(MP_QSTR_ReloadException), MP_ROM_PTR(&mp_type_ReloadException) },
703704
{ MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) },
704705
{ MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) },
705706
{ MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) },

py/mpstate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ typedef struct _mp_state_vm_t {
136136
mp_obj_exception_t mp_kbd_exception;
137137
#endif
138138

139+
// exception object of type ReloadException
140+
mp_obj_exception_t mp_reload_exception;
141+
139142
// dictionary with loaded modules (may be exposed as sys.modules)
140143
mp_obj_dict_t mp_loaded_modules_dict;
141144

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ extern const mp_obj_type_t mp_type_ImportError;
590590
extern const mp_obj_type_t mp_type_IndentationError;
591591
extern const mp_obj_type_t mp_type_IndexError;
592592
extern const mp_obj_type_t mp_type_KeyboardInterrupt;
593+
extern const mp_obj_type_t mp_type_ReloadException;
593594
extern const mp_obj_type_t mp_type_KeyError;
594595
extern const mp_obj_type_t mp_type_LookupError;
595596
extern const mp_obj_type_t mp_type_MemoryError;

py/objexcept.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ const mp_obj_type_t mp_type_ ## exc_name = { \
249249
// http://docs.python.org/3/library/exceptions.html
250250
MP_DEFINE_EXCEPTION(SystemExit, BaseException)
251251
MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
252+
MP_DEFINE_EXCEPTION(ReloadException, BaseException)
252253
MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
253254
MP_DEFINE_EXCEPTION(Exception, BaseException)
254255
#if MICROPY_PY_ASYNC_AWAIT

py/runtime.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ void mp_init(void) {
8484
MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj;
8585
#endif
8686

87+
MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException;
88+
MP_STATE_VM(mp_reload_exception).traceback_alloc = 0;
89+
MP_STATE_VM(mp_reload_exception).traceback_len = 0;
90+
MP_STATE_VM(mp_reload_exception).traceback_data = NULL;
91+
MP_STATE_VM(mp_reload_exception).args = (mp_obj_tuple_t*)&mp_const_empty_tuple_obj;
92+
8793
// call port specific initialization if any
8894
#ifdef MICROPY_PORT_INIT_FUNC
8995
MICROPY_PORT_INIT_FUNC;

shared-bindings/supervisor/__init__.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
*/
2626
#include "py/obj.h"
2727
#include "py/runtime.h"
28+
29+
#include "lib/utils/interrupt_char.h"
2830
#include "supervisor/shared/autoreload.h"
31+
2932
#include "supervisor/shared/rgb_led_status.h"
3033

3134
#include "shared-bindings/supervisor/__init__.h"
3235
#include "shared-bindings/supervisor/Runtime.h"
36+
3337
//| :mod:`supervisor` --- Supervisor settings
3438
//| =================================================
3539
//|
@@ -91,12 +95,31 @@ STATIC mp_obj_t supervisor_set_rgb_status_brightness(mp_obj_t lvl){
9195
}
9296
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_set_rgb_status_brightness);
9397

98+
//| .. method:: reload()
99+
//|
100+
//| Reload the microcontroller (equivalent to hitting Ctrl-D at the REPL).
101+
//|
102+
STATIC mp_obj_t supervisor_reload(void) {
103+
reload_next_character = true;
104+
105+
MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception));
106+
#if MICROPY_ENABLE_SCHEDULER
107+
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
108+
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
109+
}
110+
#endif
111+
return mp_const_none;
112+
}
113+
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload);
114+
115+
94116
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
95117
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
96118
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) },
97119
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable_autoreload), MP_ROM_PTR(&supervisor_disable_autoreload_obj) },
98120
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },
99121
{ MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) },
122+
{ MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) },
100123
};
101124

102125
STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table);

0 commit comments

Comments
 (0)