py: Remove calls to file reader functions when these are disabled.

If MICROPY_PERSISTENT_CODE_LOAD or MICROPY_ENABLE_COMPILER are enabled then
code gets enabled that calls file reading functions which may be disabled
if no readers have been implemented.

To fix this, introduce a MICROPY_HAS_FILE_READER variable, which is
automatically set if MICROPY_READER_POSIX or MICROPY_READER_VFS is set but
can also be manually set if a custom reader is being implemented.  Then
disable the file reading calls if this is not set.
This commit is contained in:
Sean Burton 2018-12-13 12:10:35 +00:00 committed by Damien George
parent 35687a87ec
commit e33bc59712
3 changed files with 12 additions and 4 deletions

View File

@ -131,7 +131,7 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d
#endif #endif
} }
#if MICROPY_ENABLE_COMPILER #if MICROPY_MODULE_FROZEN_STR || MICROPY_ENABLE_COMPILER
STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) { STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {
#if MICROPY_PY___FILE__ #if MICROPY_PY___FILE__
qstr source_name = lex->source_name; qstr source_name = lex->source_name;
@ -182,7 +182,7 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
#endif #endif
STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
#if MICROPY_MODULE_FROZEN || MICROPY_PERSISTENT_CODE_LOAD || MICROPY_ENABLE_COMPILER #if MICROPY_MODULE_FROZEN || MICROPY_ENABLE_COMPILER || (MICROPY_PERSISTENT_CODE_LOAD && MICROPY_HAS_FILE_READER)
char *file_str = vstr_null_terminated_str(file); char *file_str = vstr_null_terminated_str(file);
#endif #endif
@ -213,7 +213,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// If we support loading .mpy files then check if the file extension is of // If we support loading .mpy files then check if the file extension is of
// the correct format and, if so, load and execute the file. // the correct format and, if so, load and execute the file.
#if MICROPY_PERSISTENT_CODE_LOAD #if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD
if (file_str[file->len - 3] == 'm') { if (file_str[file->len - 3] == 'm') {
mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str); mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
do_execute_raw_code(module_obj, raw_code); do_execute_raw_code(module_obj, raw_code);
@ -229,7 +229,6 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
return; return;
} }
#else #else
// If we get here then the file was not frozen and we can't compile scripts. // If we get here then the file was not frozen and we can't compile scripts.
mp_raise_msg(&mp_type_ImportError, "script compilation not supported"); mp_raise_msg(&mp_type_ImportError, "script compilation not supported");
#endif #endif

View File

@ -464,6 +464,11 @@
#define MICROPY_READER_VFS (0) #define MICROPY_READER_VFS (0)
#endif #endif
// Whether any readers have been defined
#ifndef MICROPY_HAS_FILE_READER
#define MICROPY_HAS_FILE_READER (MICROPY_READER_POSIX || MICROPY_READER_VFS)
#endif
// Hook for the VM at the start of the opcode loop (can contain variable // Hook for the VM at the start of the opcode loop (can contain variable
// definitions usable by the other hook functions) // definitions usable by the other hook functions)
#ifndef MICROPY_VM_HOOK_INIT #ifndef MICROPY_VM_HOOK_INIT

View File

@ -232,12 +232,16 @@ mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) {
return mp_raw_code_load(&reader); return mp_raw_code_load(&reader);
} }
#if MICROPY_HAS_FILE_READER
mp_raw_code_t *mp_raw_code_load_file(const char *filename) { mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
mp_reader_t reader; mp_reader_t reader;
mp_reader_new_file(&reader, filename); mp_reader_new_file(&reader, filename);
return mp_raw_code_load(&reader); return mp_raw_code_load(&reader);
} }
#endif // MICROPY_HAS_FILE_READER
#endif // MICROPY_PERSISTENT_CODE_LOAD #endif // MICROPY_PERSISTENT_CODE_LOAD
#if MICROPY_PERSISTENT_CODE_SAVE #if MICROPY_PERSISTENT_CODE_SAVE