3434#include "py/objmodule.h"
3535#include "py/runtime.h"
3636#include "py/builtin.h"
37+ #include "py/frozenmod.h"
3738
3839#if 0 // print debugging info
3940#define DEBUG_PRINT (1)
@@ -109,17 +110,15 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d
109110#endif
110111}
111112
112- STATIC void do_load (mp_obj_t module_obj , vstr_t * file ) {
113- // create the lexer
114- mp_lexer_t * lex = mp_lexer_new_from_file (vstr_str (file ));
113+ STATIC void do_load_from_lexer (mp_obj_t module_obj , mp_lexer_t * lex , const char * fname ) {
115114
116115 if (lex == NULL ) {
117116 // we verified the file exists using stat, but lexer could still fail
118117 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE ) {
119118 nlr_raise (mp_obj_new_exception_msg (& mp_type_ImportError , "module not found" ));
120119 } else {
121120 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ImportError ,
122- "no module named '%s'" , vstr_str ( file ) ));
121+ "no module named '%s'" , fname ));
123122 }
124123 }
125124
@@ -133,6 +132,12 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
133132 mp_parse_compile_execute (lex , MP_PARSE_FILE_INPUT , mod_globals , mod_globals );
134133}
135134
135+ STATIC void do_load (mp_obj_t module_obj , vstr_t * file ) {
136+ // create the lexer
137+ mp_lexer_t * lex = mp_lexer_new_from_file (vstr_str (file ));
138+ do_load_from_lexer (module_obj , lex , vstr_str (file ));
139+ }
140+
136141mp_obj_t mp_builtin___import__ (mp_uint_t n_args , const mp_obj_t * args ) {
137142#if DEBUG_PRINT
138143 DEBUG_printf ("__import__:\n" );
@@ -237,6 +242,15 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
237242 }
238243 DEBUG_printf ("Module not yet loaded\n" );
239244
245+ #if MICROPY_MODULE_FROZEN
246+ mp_lexer_t * lex = mp_find_frozen_module (mod_str , mod_len );
247+ if (lex != NULL ) {
248+ module_obj = mp_obj_new_module (MP_OBJ_QSTR_VALUE (module_name ));
249+ do_load_from_lexer (module_obj , lex , mod_str );
250+ return module_obj ;
251+ }
252+ #endif
253+
240254 uint last = 0 ;
241255 VSTR_FIXED (path , MICROPY_ALLOC_PATH_MAX )
242256 module_obj = MP_OBJ_NULL ;
0 commit comments