lib/utils/pyexec: Add pyexec_file_if_exists() helper function.

It will only execute the script if it can be stat'd and is a file.
This commit is contained in:
Damien George 2019-04-26 15:21:09 +10:00
parent 775ffdcc3b
commit 06a532c227
2 changed files with 9 additions and 0 deletions

View File

@ -541,6 +541,14 @@ int pyexec_file(const char *filename) {
return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_FILENAME); return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_FILENAME);
} }
int pyexec_file_if_exists(const char *filename) {
mp_import_stat_t stat = mp_import_stat(filename);
if (stat != MP_IMPORT_STAT_FILE) {
return 1; // success (no file is the same as an empty file executing without fail)
}
return pyexec_file(filename);
}
#if MICROPY_MODULE_FROZEN #if MICROPY_MODULE_FROZEN
int pyexec_frozen_module(const char *name) { int pyexec_frozen_module(const char *name) {
void *frozen_data; void *frozen_data;

View File

@ -46,6 +46,7 @@ extern int pyexec_system_exit;
int pyexec_raw_repl(void); int pyexec_raw_repl(void);
int pyexec_friendly_repl(void); int pyexec_friendly_repl(void);
int pyexec_file(const char *filename); int pyexec_file(const char *filename);
int pyexec_file_if_exists(const char *filename);
int pyexec_frozen_module(const char *name); int pyexec_frozen_module(const char *name);
void pyexec_event_repl_init(void); void pyexec_event_repl_init(void);
int pyexec_event_repl_process_char(int c); int pyexec_event_repl_process_char(int c);