@@ -110,23 +110,70 @@ STATIC mp_obj_t supervisor_set_next_stack_limit(mp_obj_t size_obj) {
110
110
}
111
111
MP_DEFINE_CONST_FUN_OBJ_1 (supervisor_set_next_stack_limit_obj , supervisor_set_next_stack_limit );
112
112
113
+ //| def set_next_code_file(filename: Optional[str], /, *, reload_on_success : bool = False, reload_on_error: bool = False, sticky_on_success: bool = False, sticky_on_error: bool = False, sticky_on_reload: bool = False) -> None:
114
+ //| """Set what file to run on the next vm run.
115
+ //|
116
+ //| When not None, the given filename is inserted at the front of the usual ['code.py',
117
+ //| 'main.py'] search sequence.
118
+ //|
119
+ //| The optional keyword arguments specify what happens after the specified file has run:
120
+ //|
121
+ //| sticky_on_… determine whether the newly set filename and options stay in effect: If True,
122
+ //| further runs will continue to run that file (unless it says otherwise by calling
123
+ //| set_next_code_filename() itself). If False, the settings will only affect one run and revert
124
+ //| to the standard code.py/main.py afterwards.
125
+ //|
126
+ //| reload_on_… determine how to continue: If False, wait in the usual "Code done running.
127
+ //| Waiting for reload. / Press any key to enter the REPL. Use CTRL-D to reload." state. If
128
+ //| True, reload immediately as if CTRL-D was pressed.
129
+ //|
130
+ //| …_on_success take effect when the program runs to completion or calls sys.exit().
131
+ //|
132
+ //| …_on_error take effect when the program exits with an exception, including the
133
+ //| KeyboardInterrupt caused by CTRL-C.
134
+ //|
135
+ //| …_on_reload take effect when the program is interrupted by files being written to the USB
136
+ //| drive (auto-reload) or when it calls supervisor.reload().
137
+ //|
138
+ //| These settings are stored in RAM, not in persistent memory, and will therefore only affect
139
+ //| soft reloads. Powering off or resetting the device will always revert to standard settings.
140
+ //|
141
+ //| When called multiple times in the same run, only the last call takes effect, replacing any
142
+ //| settings made by previous ones. This is the main use of passing None as a filename: to
143
+ //| reset to the standard search sequence."""
144
+ //| ...
145
+ //|
113
146
STATIC mp_obj_t supervisor_set_next_code_file (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
114
147
static const mp_arg_t allowed_args [] = {
115
148
{ MP_QSTR_filename , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
116
149
{ MP_QSTR_reload_on_success , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
150
+ { MP_QSTR_reload_on_error , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
151
+ { MP_QSTR_sticky_on_success , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
152
+ { MP_QSTR_sticky_on_error , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
153
+ { MP_QSTR_sticky_on_reload , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
117
154
};
118
155
struct {
119
156
mp_arg_val_t filename ;
120
157
mp_arg_val_t reload_on_success ;
158
+ mp_arg_val_t reload_on_error ;
159
+ mp_arg_val_t sticky_on_success ;
160
+ mp_arg_val_t sticky_on_error ;
161
+ mp_arg_val_t sticky_on_reload ;
121
162
} args ;
122
163
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
123
164
if (!MP_OBJ_IS_STR_OR_BYTES (args .filename .u_obj ) && args .filename .u_obj != mp_const_none ) {
124
165
mp_raise_TypeError (translate ("argument has wrong type" ));
125
166
}
126
167
if (args .filename .u_obj == mp_const_none ) args .filename .u_obj = mp_const_empty_bytes ;
168
+ uint8_t options = 0 ;
169
+ if (args .reload_on_success .u_bool ) options |= SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS ;
170
+ if (args .reload_on_error .u_bool ) options |= SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_ERROR ;
171
+ if (args .sticky_on_success .u_bool ) options |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS ;
172
+ if (args .sticky_on_error .u_bool ) options |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_ERROR ;
173
+ if (args .sticky_on_reload .u_bool ) options |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD ;
127
174
mp_obj_t items [] = {
128
175
args .filename .u_obj ,
129
- args . reload_on_success . u_bool ? MP_OBJ_NEW_SMALL_INT (SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS ) : MP_OBJ_NEW_SMALL_INT ( 0 )
176
+ MP_OBJ_NEW_SMALL_INT (options )
130
177
};
131
178
MP_STATE_VM (supervisor_next_code ) = mp_obj_new_tuple (MP_ARRAY_SIZE (items ), items );
132
179
return mp_const_none ;
0 commit comments