From 06a532c227c2f37fc190deb84970fdfeaaf37d6a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 26 Apr 2019 15:21:09 +1000 Subject: 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. --- lib/utils/pyexec.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/utils/pyexec.c') diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index f4e6856e5..946a97a00 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -541,6 +541,14 @@ int pyexec_file(const char *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 int pyexec_frozen_module(const char *name) { void *frozen_data; -- cgit v1.2.3