builtinimport.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2019 Damien P. George
  7. * Copyright (c) 2014 Paul Sokolovsky
  8. * Copyright (c) 2021 Jim Mussared
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #include "py/compile.h"
  32. #include "py/objmodule.h"
  33. #include "py/persistentcode.h"
  34. #include "py/runtime.h"
  35. #include "py/builtin.h"
  36. #include "py/frozenmod.h"
  37. #if MICROPY_DEBUG_VERBOSE // print debugging info
  38. #define DEBUG_PRINT (1)
  39. #define DEBUG_printf DEBUG_printf
  40. #else // don't print debugging info
  41. #define DEBUG_PRINT (0)
  42. #define DEBUG_printf(...) (void)0
  43. #endif
  44. #if MICROPY_ENABLE_EXTERNAL_IMPORT
  45. // Must be a string of one byte.
  46. #define PATH_SEP_CHAR "/"
  47. // Virtual sys.path entry that maps to the frozen modules.
  48. #define MP_FROZEN_PATH_PREFIX ".frozen/"
  49. // Wrapper for mp_import_stat (which is provided by the port, and typically
  50. // uses mp_vfs_import_stat) to also search frozen modules. Given an exact
  51. // path to a file or directory (e.g. "foo/bar", foo/bar.py" or "foo/bar.mpy"),
  52. // will return whether the path is a file, directory, or doesn't exist.
  53. STATIC mp_import_stat_t stat_path(const char *path) {
  54. #if MICROPY_MODULE_FROZEN
  55. // Only try and load as a frozen module if it starts with .frozen/.
  56. const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
  57. if (strncmp(path, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
  58. // Just stat (which is the return value), don't get the data.
  59. return mp_find_frozen_module(path + frozen_path_prefix_len, NULL, NULL);
  60. }
  61. #endif
  62. return mp_import_stat(path);
  63. }
  64. // Stat a given filesystem path to a .py file. If the file does not exist,
  65. // then attempt to stat the corresponding .mpy file, and update the path
  66. // argument. This is the logic that makes .py files take precedent over .mpy
  67. // files. This uses stat_path above, rather than mp_import_stat directly, so
  68. // that the .frozen path prefix is handled.
  69. STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) {
  70. mp_import_stat_t stat = stat_path(vstr_null_terminated_str(path));
  71. if (stat == MP_IMPORT_STAT_FILE) {
  72. return stat;
  73. }
  74. #if MICROPY_PERSISTENT_CODE_LOAD
  75. // Didn't find .py -- try the .mpy instead by inserting an 'm' into the '.py'.
  76. // Note: There's no point doing this if it's a frozen path, but adding the check
  77. // would be extra code, and no harm letting mp_find_frozen_module fail instead.
  78. vstr_ins_byte(path, path->len - 2, 'm');
  79. stat = stat_path(vstr_null_terminated_str(path));
  80. if (stat == MP_IMPORT_STAT_FILE) {
  81. return stat;
  82. }
  83. #endif
  84. return MP_IMPORT_STAT_NO_EXIST;
  85. }
  86. // Given an import path (e.g. "foo/bar"), try and find "foo/bar" (a directory)
  87. // or "foo/bar.(m)py" in either the filesystem or frozen modules. If the
  88. // result is a file, the path argument will be updated to include the file
  89. // extension.
  90. STATIC mp_import_stat_t stat_module(vstr_t *path) {
  91. mp_import_stat_t stat = stat_path(vstr_null_terminated_str(path));
  92. DEBUG_printf("stat %s: %d\n", vstr_str(path), stat);
  93. if (stat == MP_IMPORT_STAT_DIR) {
  94. return stat;
  95. }
  96. // Not a directory, add .py and try as a file.
  97. vstr_add_str(path, ".py");
  98. return stat_file_py_or_mpy(path);
  99. }
  100. // Given a top-level module name, try and find it in each of the sys.path
  101. // entries. Note: On success, the dest argument will be updated to the matching
  102. // path (i.e. "<entry>/mod_name(.py)").
  103. STATIC mp_import_stat_t stat_top_level(qstr mod_name, vstr_t *dest) {
  104. DEBUG_printf("stat_top_level: '%s'\n", qstr_str(mod_name));
  105. #if MICROPY_PY_SYS
  106. size_t path_num;
  107. mp_obj_t *path_items;
  108. mp_obj_get_array(mp_sys_path, &path_num, &path_items);
  109. // go through each sys.path entry, trying to import "<entry>/<mod_name>".
  110. for (size_t i = 0; i < path_num; i++) {
  111. vstr_reset(dest);
  112. size_t p_len;
  113. const char *p = mp_obj_str_get_data(path_items[i], &p_len);
  114. if (p_len > 0) {
  115. // Add the path separator (unless the entry is "", i.e. cwd).
  116. vstr_add_strn(dest, p, p_len);
  117. vstr_add_char(dest, PATH_SEP_CHAR[0]);
  118. }
  119. vstr_add_str(dest, qstr_str(mod_name));
  120. mp_import_stat_t stat = stat_module(dest);
  121. if (stat != MP_IMPORT_STAT_NO_EXIST) {
  122. return stat;
  123. }
  124. }
  125. // sys.path was empty or no matches, do not search the filesystem or
  126. // frozen code.
  127. return MP_IMPORT_STAT_NO_EXIST;
  128. #else
  129. // mp_sys_path is not enabled, so just stat the given path directly.
  130. vstr_add_str(dest, qstr_str(mod_name));
  131. return stat_module(dest);
  132. #endif
  133. }
  134. #if MICROPY_MODULE_FROZEN_STR || MICROPY_ENABLE_COMPILER
  135. STATIC void do_load_from_lexer(mp_module_context_t *context, mp_lexer_t *lex) {
  136. #if MICROPY_PY___FILE__
  137. qstr source_name = lex->source_name;
  138. mp_store_attr(MP_OBJ_FROM_PTR(&context->module), MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
  139. #endif
  140. // parse, compile and execute the module in its context
  141. mp_obj_dict_t *mod_globals = context->module.globals;
  142. mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
  143. }
  144. #endif
  145. #if (MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD) || MICROPY_MODULE_FROZEN_MPY
  146. STATIC void do_execute_raw_code(const mp_module_context_t *context, const mp_raw_code_t *rc, qstr source_name) {
  147. #if MICROPY_PY___FILE__
  148. mp_store_attr(MP_OBJ_FROM_PTR(&context->module), MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
  149. #else
  150. (void)source_name;
  151. #endif
  152. // execute the module in its context
  153. mp_obj_dict_t *mod_globals = context->module.globals;
  154. // save context
  155. nlr_jump_callback_node_globals_locals_t ctx;
  156. ctx.globals = mp_globals_get();
  157. ctx.locals = mp_locals_get();
  158. // set new context
  159. mp_globals_set(mod_globals);
  160. mp_locals_set(mod_globals);
  161. // set exception handler to restore context if an exception is raised
  162. nlr_push_jump_callback(&ctx.callback, mp_globals_locals_set_from_nlr_jump_callback);
  163. // make and execute the function
  164. mp_obj_t module_fun = mp_make_function_from_raw_code(rc, context, NULL);
  165. mp_call_function_0(module_fun);
  166. // deregister exception handler and restore context
  167. nlr_pop_jump_callback(true);
  168. }
  169. #endif
  170. STATIC void do_load(mp_module_context_t *module_obj, vstr_t *file) {
  171. #if MICROPY_MODULE_FROZEN || MICROPY_ENABLE_COMPILER || (MICROPY_PERSISTENT_CODE_LOAD && MICROPY_HAS_FILE_READER)
  172. const char *file_str = vstr_null_terminated_str(file);
  173. #endif
  174. // If we support frozen modules (either as str or mpy) then try to find the
  175. // requested filename in the list of frozen module filenames.
  176. #if MICROPY_MODULE_FROZEN
  177. void *modref;
  178. int frozen_type;
  179. const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
  180. if (strncmp(file_str, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
  181. mp_find_frozen_module(file_str + frozen_path_prefix_len, &frozen_type, &modref);
  182. // If we support frozen str modules and the compiler is enabled, and we
  183. // found the filename in the list of frozen files, then load and execute it.
  184. #if MICROPY_MODULE_FROZEN_STR
  185. if (frozen_type == MP_FROZEN_STR) {
  186. do_load_from_lexer(module_obj, modref);
  187. return;
  188. }
  189. #endif
  190. // If we support frozen mpy modules and we found a corresponding file (and
  191. // its data) in the list of frozen files, execute it.
  192. #if MICROPY_MODULE_FROZEN_MPY
  193. if (frozen_type == MP_FROZEN_MPY) {
  194. const mp_frozen_module_t *frozen = modref;
  195. module_obj->constants = frozen->constants;
  196. #if MICROPY_PY___FILE__
  197. qstr frozen_file_qstr = qstr_from_str(file_str + frozen_path_prefix_len);
  198. #else
  199. qstr frozen_file_qstr = MP_QSTRnull;
  200. #endif
  201. do_execute_raw_code(module_obj, frozen->rc, frozen_file_qstr);
  202. return;
  203. }
  204. #endif
  205. }
  206. #endif // MICROPY_MODULE_FROZEN
  207. qstr file_qstr = qstr_from_str(file_str);
  208. // If we support loading .mpy files then check if the file extension is of
  209. // the correct format and, if so, load and execute the file.
  210. #if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD
  211. if (file_str[file->len - 3] == 'm') {
  212. mp_compiled_module_t cm;
  213. cm.context = module_obj;
  214. mp_raw_code_load_file(file_qstr, &cm);
  215. do_execute_raw_code(cm.context, cm.rc, file_qstr);
  216. return;
  217. }
  218. #endif
  219. // If we can compile scripts then load the file and compile and execute it.
  220. #if MICROPY_ENABLE_COMPILER
  221. {
  222. mp_lexer_t *lex = mp_lexer_new_from_file(file_qstr);
  223. do_load_from_lexer(module_obj, lex);
  224. return;
  225. }
  226. #else
  227. // If we get here then the file was not frozen and we can't compile scripts.
  228. mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("script compilation not supported"));
  229. #endif
  230. }
  231. // Convert a relative (to the current module) import, going up "level" levels,
  232. // into an absolute import.
  233. STATIC void evaluate_relative_import(mp_int_t level, const char **module_name, size_t *module_name_len) {
  234. // What we want to do here is to take the name of the current module,
  235. // remove <level> trailing components, and concatenate the passed-in
  236. // module name.
  237. // For example, level=3, module_name="foo.bar", __name__="a.b.c.d" --> "a.foo.bar"
  238. // "Relative imports use a module's __name__ attribute to determine that
  239. // module's position in the package hierarchy."
  240. // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
  241. mp_obj_t current_module_name_obj = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
  242. assert(current_module_name_obj != MP_OBJ_NULL);
  243. #if MICROPY_MODULE_OVERRIDE_MAIN_IMPORT && MICROPY_CPYTHON_COMPAT
  244. if (MP_OBJ_QSTR_VALUE(current_module_name_obj) == MP_QSTR___main__) {
  245. // This is a module loaded by -m command-line switch (e.g. unix port),
  246. // and so its __name__ has been set to "__main__". Get its real name
  247. // that we stored during import in the __main__ attribute.
  248. current_module_name_obj = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
  249. }
  250. #endif
  251. // If we have a __path__ in the globals dict, then we're a package.
  252. bool is_pkg = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(MP_QSTR___path__), MP_MAP_LOOKUP);
  253. #if DEBUG_PRINT
  254. DEBUG_printf("Current module/package: ");
  255. mp_obj_print_helper(MICROPY_DEBUG_PRINTER, current_module_name_obj, PRINT_REPR);
  256. DEBUG_printf(", is_package: %d", is_pkg);
  257. DEBUG_printf("\n");
  258. #endif
  259. size_t current_module_name_len;
  260. const char *current_module_name = mp_obj_str_get_data(current_module_name_obj, &current_module_name_len);
  261. const char *p = current_module_name + current_module_name_len;
  262. if (is_pkg) {
  263. // If we're evaluating relative to a package, then take off one fewer
  264. // level (i.e. the relative search starts inside the package, rather
  265. // than as a sibling of the package).
  266. --level;
  267. }
  268. // Walk back 'level' dots (or run out of path).
  269. while (level && p > current_module_name) {
  270. if (*--p == '.') {
  271. --level;
  272. }
  273. }
  274. // We must have some component left over to import from.
  275. if (p == current_module_name) {
  276. mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("can't perform relative import"));
  277. }
  278. // New length is len("<chopped path>.<module_name>"). Note: might be one byte
  279. // more than we need if module_name is empty (for the extra . we will
  280. // append).
  281. uint new_module_name_len = (size_t)(p - current_module_name) + 1 + *module_name_len;
  282. char *new_mod = mp_local_alloc(new_module_name_len);
  283. memcpy(new_mod, current_module_name, p - current_module_name);
  284. // Only append ".<module_name>" if there was one).
  285. if (*module_name_len != 0) {
  286. new_mod[p - current_module_name] = '.';
  287. memcpy(new_mod + (p - current_module_name) + 1, *module_name, *module_name_len);
  288. } else {
  289. --new_module_name_len;
  290. }
  291. // Copy into a QSTR.
  292. qstr new_mod_q = qstr_from_strn(new_mod, new_module_name_len);
  293. mp_local_free(new_mod);
  294. DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q));
  295. *module_name = qstr_str(new_mod_q);
  296. *module_name_len = new_module_name_len;
  297. }
  298. typedef struct _nlr_jump_callback_node_unregister_module_t {
  299. nlr_jump_callback_node_t callback;
  300. qstr name;
  301. } nlr_jump_callback_node_unregister_module_t;
  302. STATIC void unregister_module_from_nlr_jump_callback(void *ctx_in) {
  303. nlr_jump_callback_node_unregister_module_t *ctx = ctx_in;
  304. mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
  305. mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(ctx->name), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
  306. }
  307. // Load a module at the specified absolute path, possibly as a submodule of the given outer module.
  308. // full_mod_name: The full absolute path up to this level (e.g. "foo.bar.baz").
  309. // level_mod_name: The final component of the path (e.g. "baz").
  310. // outer_module_obj: The parent module (we need to store this module as an
  311. // attribute on it) (or MP_OBJ_NULL for top-level).
  312. // override_main: Whether to set the __name__ to "__main__" (and use __main__
  313. // for the actual path).
  314. STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name, mp_obj_t outer_module_obj, bool override_main) {
  315. // Immediately return if the module at this level is already loaded.
  316. mp_map_elem_t *elem;
  317. #if MICROPY_PY_SYS
  318. // If sys.path is empty, the intention is to force using a built-in. This
  319. // means we should also ignore any loaded modules with the same name
  320. // which may have come from the filesystem.
  321. size_t path_num;
  322. mp_obj_t *path_items;
  323. mp_obj_get_array(mp_sys_path, &path_num, &path_items);
  324. if (path_num)
  325. #endif
  326. {
  327. elem = mp_map_lookup(&MP_STATE_VM(mp_loaded_modules_dict).map, MP_OBJ_NEW_QSTR(full_mod_name), MP_MAP_LOOKUP);
  328. if (elem) {
  329. return elem->value;
  330. }
  331. }
  332. VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX);
  333. mp_import_stat_t stat = MP_IMPORT_STAT_NO_EXIST;
  334. mp_obj_t module_obj;
  335. if (outer_module_obj == MP_OBJ_NULL) {
  336. // First module in the dotted-name path.
  337. DEBUG_printf("Searching for top-level module\n");
  338. // An import of a non-extensible built-in will always bypass the
  339. // filesystem. e.g. `import micropython` or `import pyb`. So try and
  340. // match a non-extensible built-ins first.
  341. module_obj = mp_module_get_builtin(level_mod_name, false);
  342. if (module_obj != MP_OBJ_NULL) {
  343. return module_obj;
  344. }
  345. // Next try the filesystem. Search for a directory or file relative to
  346. // all the locations in sys.path.
  347. stat = stat_top_level(level_mod_name, &path);
  348. // If filesystem failed, now try and see if it matches an extensible
  349. // built-in module.
  350. if (stat == MP_IMPORT_STAT_NO_EXIST) {
  351. module_obj = mp_module_get_builtin(level_mod_name, true);
  352. if (module_obj != MP_OBJ_NULL) {
  353. return module_obj;
  354. }
  355. }
  356. } else {
  357. DEBUG_printf("Searching for sub-module\n");
  358. #if MICROPY_MODULE_BUILTIN_SUBPACKAGES
  359. // If the outer module is a built-in (because its map is in ROM), then
  360. // treat it like a package if it contains this submodule in its
  361. // globals dict.
  362. mp_obj_module_t *mod = MP_OBJ_TO_PTR(outer_module_obj);
  363. if (mod->globals->map.is_fixed) {
  364. elem = mp_map_lookup(&mod->globals->map, MP_OBJ_NEW_QSTR(level_mod_name), MP_MAP_LOOKUP);
  365. // Also verify that the entry in the globals dict is in fact a module.
  366. if (elem && mp_obj_is_type(elem->value, &mp_type_module)) {
  367. return elem->value;
  368. }
  369. }
  370. #endif
  371. // If the outer module is a package, it will have __path__ set.
  372. // We can use that as the path to search inside.
  373. mp_obj_t dest[2];
  374. mp_load_method_maybe(outer_module_obj, MP_QSTR___path__, dest);
  375. if (dest[0] != MP_OBJ_NULL) {
  376. // e.g. __path__ will be "<matched search path>/foo/bar"
  377. vstr_add_str(&path, mp_obj_str_get_str(dest[0]));
  378. // Add the level module name to the path to get "<matched search path>/foo/bar/baz".
  379. vstr_add_char(&path, PATH_SEP_CHAR[0]);
  380. vstr_add_str(&path, qstr_str(level_mod_name));
  381. stat = stat_module(&path);
  382. }
  383. }
  384. // Not already loaded, and not a built-in, so look at the stat result from the filesystem/frozen.
  385. if (stat == MP_IMPORT_STAT_NO_EXIST) {
  386. // Not found -- fail.
  387. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  388. mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("module not found"));
  389. #else
  390. mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("no module named '%q'"), full_mod_name);
  391. #endif
  392. }
  393. // Module was found on the filesystem/frozen, try and load it.
  394. DEBUG_printf("Found path to load: %.*s\n", (int)vstr_len(&path), vstr_str(&path));
  395. // Prepare for loading from the filesystem. Create a new shell module
  396. // and register it in sys.modules. Also make sure we remove it if
  397. // there is any problem below.
  398. module_obj = mp_obj_new_module(full_mod_name);
  399. nlr_jump_callback_node_unregister_module_t ctx;
  400. ctx.name = full_mod_name;
  401. nlr_push_jump_callback(&ctx.callback, unregister_module_from_nlr_jump_callback);
  402. #if MICROPY_MODULE_OVERRIDE_MAIN_IMPORT
  403. // If this module is being loaded via -m on unix, then
  404. // override __name__ to "__main__". Do this only for *modules*
  405. // however - packages never have their names replaced, instead
  406. // they're -m'ed using a special __main__ submodule in them. (This all
  407. // apparently is done to not touch the package name itself, which is
  408. // important for future imports).
  409. if (override_main && stat != MP_IMPORT_STAT_DIR) {
  410. mp_obj_module_t *o = MP_OBJ_TO_PTR(module_obj);
  411. mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
  412. #if MICROPY_CPYTHON_COMPAT
  413. // Store module as "__main__" in the dictionary of loaded modules (returned by sys.modules).
  414. mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)), MP_OBJ_NEW_QSTR(MP_QSTR___main__), module_obj);
  415. // Store real name in "__main__" attribute. Need this for
  416. // resolving relative imports later. "__main__ was chosen
  417. // semi-randonly, to reuse existing qstr's.
  418. mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(full_mod_name));
  419. #endif
  420. }
  421. #endif // MICROPY_MODULE_OVERRIDE_MAIN_IMPORT
  422. if (stat == MP_IMPORT_STAT_DIR) {
  423. // Directory (i.e. a package).
  424. DEBUG_printf("%.*s is dir\n", (int)vstr_len(&path), vstr_str(&path));
  425. // Store the __path__ attribute onto this module.
  426. // https://docs.python.org/3/reference/import.html
  427. // "Specifically, any module that contains a __path__ attribute is considered a package."
  428. // This gets used later to locate any subpackages of this module.
  429. mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path)));
  430. size_t orig_path_len = path.len;
  431. vstr_add_str(&path, PATH_SEP_CHAR "__init__.py");
  432. // execute "path/__init__.py" (if available).
  433. if (stat_file_py_or_mpy(&path) == MP_IMPORT_STAT_FILE) {
  434. do_load(MP_OBJ_TO_PTR(module_obj), &path);
  435. } else {
  436. // No-op. Nothing to load.
  437. // mp_warning("%s is imported as namespace package", vstr_str(&path));
  438. }
  439. // Remove /__init__.py suffix from path.
  440. path.len = orig_path_len;
  441. } else { // MP_IMPORT_STAT_FILE
  442. // File -- execute "path.(m)py".
  443. do_load(MP_OBJ_TO_PTR(module_obj), &path);
  444. // Note: This should be the last component in the import path. If
  445. // there are remaining components then in the next call to
  446. // process_import_at_level will detect that it doesn't have
  447. // a __path__ attribute, and not attempt to stat it.
  448. }
  449. if (outer_module_obj != MP_OBJ_NULL) {
  450. // If it's a sub-module then make it available on the parent module.
  451. mp_store_attr(outer_module_obj, level_mod_name, module_obj);
  452. }
  453. nlr_pop_jump_callback(false);
  454. return module_obj;
  455. }
  456. mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) {
  457. #if DEBUG_PRINT
  458. DEBUG_printf("__import__:\n");
  459. for (size_t i = 0; i < n_args; i++) {
  460. DEBUG_printf(" ");
  461. mp_obj_print_helper(MICROPY_DEBUG_PRINTER, args[i], PRINT_REPR);
  462. DEBUG_printf("\n");
  463. }
  464. #endif
  465. // This is the import path, with any leading dots stripped.
  466. // "import foo.bar" --> module_name="foo.bar"
  467. // "from foo.bar import baz" --> module_name="foo.bar"
  468. // "from . import foo" --> module_name=""
  469. // "from ...foo.bar import baz" --> module_name="foo.bar"
  470. mp_obj_t module_name_obj = args[0];
  471. // These are the imported names.
  472. // i.e. "from foo.bar import baz, zap" --> fromtuple=("baz", "zap",)
  473. // Note: There's a special case on the Unix port, where this is set to mp_const_false which means that it's __main__.
  474. mp_obj_t fromtuple = mp_const_none;
  475. // Level is the number of leading dots in a relative import.
  476. // i.e. "from . import foo" --> level=1
  477. // i.e. "from ...foo.bar import baz" --> level=3
  478. mp_int_t level = 0;
  479. if (n_args >= 4) {
  480. fromtuple = args[3];
  481. if (n_args >= 5) {
  482. level = MP_OBJ_SMALL_INT_VALUE(args[4]);
  483. if (level < 0) {
  484. mp_raise_ValueError(NULL);
  485. }
  486. }
  487. }
  488. size_t module_name_len;
  489. const char *module_name = mp_obj_str_get_data(module_name_obj, &module_name_len);
  490. if (level != 0) {
  491. // Turn "foo.bar" with level=3 into "<current module 3 components>.foo.bar".
  492. // Current module name is extracted from globals().__name__.
  493. evaluate_relative_import(level, &module_name, &module_name_len);
  494. // module_name is now an absolute module path.
  495. }
  496. if (module_name_len == 0) {
  497. mp_raise_ValueError(NULL);
  498. }
  499. DEBUG_printf("Starting module search for '%s'\n", module_name);
  500. mp_obj_t top_module_obj = MP_OBJ_NULL;
  501. mp_obj_t outer_module_obj = MP_OBJ_NULL;
  502. // Iterate the absolute path, finding the end of each component of the path.
  503. // foo.bar.baz
  504. // ^ ^ ^
  505. size_t current_component_start = 0;
  506. for (size_t i = 1; i <= module_name_len; i++) {
  507. if (i == module_name_len || module_name[i] == '.') {
  508. // The module name up to this depth (e.g. foo.bar.baz).
  509. qstr full_mod_name = qstr_from_strn(module_name, i);
  510. // The current level name (e.g. baz).
  511. qstr level_mod_name = qstr_from_strn(module_name + current_component_start, i - current_component_start);
  512. DEBUG_printf("Processing module: '%s' at level '%s'\n", qstr_str(full_mod_name), qstr_str(level_mod_name));
  513. #if MICROPY_MODULE_OVERRIDE_MAIN_IMPORT
  514. // On unix, if this is being loaded via -m (indicated by sentinel
  515. // fromtuple=mp_const_false), then handle that if it's the final
  516. // component.
  517. bool override_main = (i == module_name_len && fromtuple == mp_const_false);
  518. #else
  519. bool override_main = false;
  520. #endif
  521. // Import this module.
  522. mp_obj_t module_obj = process_import_at_level(full_mod_name, level_mod_name, outer_module_obj, override_main);
  523. // Set this as the parent module, and remember the top-level module if it's the first.
  524. outer_module_obj = module_obj;
  525. if (top_module_obj == MP_OBJ_NULL) {
  526. top_module_obj = module_obj;
  527. }
  528. current_component_start = i + 1;
  529. }
  530. }
  531. if (fromtuple != mp_const_none) {
  532. // If fromtuple is not empty, return leaf module
  533. return outer_module_obj;
  534. } else {
  535. // Otherwise, we need to return top-level package
  536. return top_module_obj;
  537. }
  538. }
  539. #else // MICROPY_ENABLE_EXTERNAL_IMPORT
  540. mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) {
  541. // Check that it's not a relative import.
  542. if (n_args >= 5 && MP_OBJ_SMALL_INT_VALUE(args[4]) != 0) {
  543. mp_raise_NotImplementedError(MP_ERROR_TEXT("relative import"));
  544. }
  545. // Check if the module is already loaded.
  546. mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_VM(mp_loaded_modules_dict).map, args[0], MP_MAP_LOOKUP);
  547. if (elem) {
  548. return elem->value;
  549. }
  550. // Try the name directly as a non-extensible built-in (e.g. `micropython`).
  551. qstr module_name_qstr = mp_obj_str_get_qstr(args[0]);
  552. mp_obj_t module_obj = mp_module_get_builtin(module_name_qstr, false);
  553. if (module_obj != MP_OBJ_NULL) {
  554. return module_obj;
  555. }
  556. // Now try as an extensible built-in (e.g. `time`).
  557. module_obj = mp_module_get_builtin(module_name_qstr, true);
  558. if (module_obj != MP_OBJ_NULL) {
  559. return module_obj;
  560. }
  561. // Couldn't find the module, so fail
  562. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  563. mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("module not found"));
  564. #else
  565. mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("no module named '%q'"), module_name_qstr);
  566. #endif
  567. }
  568. #endif // MICROPY_ENABLE_EXTERNAL_IMPORT
  569. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);