ports: Convert to use pyexec_file_if_exists() to execute boot/main.py.

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.
pull/1/head
Damien George 2019-04-26 15:22:14 +10:00
parent 06a532c227
commit 0646e607b5
6 changed files with 22 additions and 32 deletions

View File

@ -180,7 +180,7 @@ soft_reset:
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;
}
@ -205,7 +205,7 @@ soft_reset:
} 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;
}

View File

@ -111,9 +111,9 @@ soft_reset:
// 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 (;;) {

View File

@ -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
}

View File

@ -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 (;;) {

View File

@ -678,15 +678,12 @@ soft_reset:
// 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);
}
}
@ -735,15 +732,12 @@ soft_reset:
} 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);
}
}

View File

@ -302,7 +302,7 @@ soft_reset:
#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
@ -322,7 +322,7 @@ soft_reset:
} 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);