Skip to content

Commit

Permalink
ports: Convert to use pyexec_file_if_exists() to execute boot/main.py.
Browse files Browse the repository at this point in the history
The stm32 and nrf ports already had the behaviour that they would first
check if the script exists before executing it, and this patch makes all
other ports work the same way.  This helps when developing apps because
it's hard to tell (when unconditionally trying to execute the scripts) if
the resulting OSError at boot up comes from missing boot.py or main.py, or
from some other error.  And it's not really an error if these scripts don't
exist.
  • Loading branch information
dpgeorge committed Apr 26, 2019
1 parent 06a532c commit 0646e60
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 32 deletions.
4 changes: 2 additions & 2 deletions ports/cc3200/mptask.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void TASK_MicroPython (void *pvParameters) {

if (!safeboot) {
// run boot.py
int ret = pyexec_file("boot.py");
int ret = pyexec_file_if_exists("boot.py");
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
Expand All @@ -205,7 +205,7 @@ void TASK_MicroPython (void *pvParameters) {
} else {
main_py = mp_obj_str_get_str(MP_STATE_PORT(machine_config_main));
}
int ret = pyexec_file(main_py);
int ret = pyexec_file_if_exists(main_py);
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
Expand Down
4 changes: 2 additions & 2 deletions ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ void mp_task(void *pvParameter) {

// run boot-up scripts
pyexec_frozen_module("_boot.py");
pyexec_file("boot.py");
pyexec_file_if_exists("boot.py");
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
pyexec_file("main.py");
pyexec_file_if_exists("main.py");
}

for (;;) {
Expand Down
4 changes: 2 additions & 2 deletions ports/esp8266/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ STATIC void mp_reset(void) {

#if MICROPY_MODULE_FROZEN
pyexec_frozen_module("_boot.py");
pyexec_file("boot.py");
pyexec_file_if_exists("boot.py");
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
pyexec_file("main.py");
pyexec_file_if_exists("main.py");
}
#endif
}
Expand Down
8 changes: 2 additions & 6 deletions ports/nrf/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,8 @@ pin_init0();

#if MICROPY_VFS || MICROPY_MBFS
// run boot.py and main.py if they exist.
if (mp_import_stat("boot.py") == MP_IMPORT_STAT_FILE) {
pyexec_file("boot.py");
}
if (mp_import_stat("main.py") == MP_IMPORT_STAT_FILE) {
pyexec_file("main.py");
}
pyexec_file_if_exists("boot.py");
pyexec_file_if_exists("main.py");
#endif

for (;;) {
Expand Down
30 changes: 12 additions & 18 deletions ports/stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,15 +678,12 @@ void stm32_main(uint32_t reset_mode) {
// TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
if (reset_mode == 1 || reset_mode == 3) {
const char *boot_py = "boot.py";
mp_import_stat_t stat = mp_import_stat(boot_py);
if (stat == MP_IMPORT_STAT_FILE) {
int ret = pyexec_file(boot_py);
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
if (!ret) {
flash_error(4);
}
int ret = pyexec_file_if_exists(boot_py);
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
if (!ret) {
flash_error(4);
}
}

Expand Down Expand Up @@ -735,15 +732,12 @@ void stm32_main(uint32_t reset_mode) {
} else {
main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main));
}
mp_import_stat_t stat = mp_import_stat(main_py);
if (stat == MP_IMPORT_STAT_FILE) {
int ret = pyexec_file(main_py);
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
if (!ret) {
flash_error(3);
}
int ret = pyexec_file_if_exists(main_py);
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
if (!ret) {
flash_error(3);
}
}

Expand Down
4 changes: 2 additions & 2 deletions ports/teensy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ int main(void) {
#if MICROPY_MODULE_FROZEN
pyexec_frozen_module("boot.py");
#else
if (!pyexec_file("/boot.py")) {
if (!pyexec_file_if_exists("/boot.py")) {
flash_error(4);
}
#endif
Expand All @@ -322,7 +322,7 @@ int main(void) {
} else {
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
}
if (!pyexec_file(vstr_null_terminated_str(vstr))) {
if (!pyexec_file_if_exists(vstr_null_terminated_str(vstr))) {
flash_error(3);
}
vstr_free(vstr);
Expand Down

0 comments on commit 0646e60

Please sign in to comment.