@@ -281,6 +281,19 @@ bool cleanup_after_vm(supervisor_allocation* heap) {
281
281
return false;
282
282
}
283
283
284
+ void maybe_clear_next_code_info (bool new_next_code ) {
285
+ // - If a new next code file was set, honor that in any case.
286
+ // - Otherwise: If a reload was requested by a USB file write, we want to run the same file
287
+ // again, preserve any next-code info. Currently that also covers a reload requested by
288
+ // supervisor.reload(), or do we want to treat that differently by turning
289
+ // reload_requested into an enum?
290
+ // - Otherwise: Clear any leftover next-code info and restart at code.py/main.py.
291
+ if (!new_next_code && !reload_requested ) {
292
+ free_memory (next_code_allocation );
293
+ next_code_allocation = NULL ;
294
+ }
295
+ }
296
+
284
297
bool run_code_py (safe_mode_t safe_mode ) {
285
298
bool serial_connected_at_start = serial_connected ();
286
299
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
@@ -304,6 +317,7 @@ bool run_code_py(safe_mode_t safe_mode) {
304
317
result .exception_line = 0 ;
305
318
306
319
bool found_main = false;
320
+ bool new_next_code = false;
307
321
308
322
if (safe_mode != NO_SAFE_MODE ) {
309
323
serial_write_compressed (translate ("Running in safe mode! " ));
@@ -342,22 +356,14 @@ bool run_code_py(safe_mode_t safe_mode) {
342
356
}
343
357
#endif
344
358
}
345
- bool new_next_code = cleanup_after_vm (heap );
346
- // - If a new next code file was set, honor that in any case.
347
- // - Otherwise: If a reload was requested by a USB file write, we want to run the same file
348
- // again, preserve any next-code info. Currently that also covers a reload requested by
349
- // supervisor.reload(), or do we want to treat that differently by turning
350
- // reload_requested into an enum?
351
- // - Otherwise: Clear any leftover next-code info and restart at code.py/main.py.
352
- if (!new_next_code && !reload_requested ) {
353
- free_memory (next_code_allocation );
354
- next_code_allocation = NULL ;
355
- }
359
+ new_next_code = cleanup_after_vm (heap );
356
360
357
361
if (result .return_code == 0 && (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS )) {
362
+ maybe_clear_next_code_info (new_next_code );
358
363
return true;
359
364
}
360
365
if (result .return_code & PYEXEC_FORCED_EXIT ) {
366
+ maybe_clear_next_code_info (new_next_code );
361
367
return reload_requested ;
362
368
}
363
369
}
@@ -376,11 +382,13 @@ bool run_code_py(safe_mode_t safe_mode) {
376
382
while (true) {
377
383
RUN_BACKGROUND_TASKS ;
378
384
if (reload_requested ) {
385
+ // no need to call maybe_clear_next_code_info(), it would be a no-op
379
386
reload_requested = false;
380
387
return true;
381
388
}
382
389
383
390
if (serial_connected () && serial_bytes_available ()) {
391
+ maybe_clear_next_code_info (new_next_code );
384
392
// Skip REPL if reload was requested.
385
393
return (serial_read () == CHAR_CTRL_D );
386
394
}
0 commit comments