blob: dc9bf75f07f57d8433ba73ff65fa51e23876be1e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Damien George66028ab2014-01-03 14:03:48 +000028#include <stdio.h>
Damien George66028ab2014-01-03 14:03:48 +000029#include <string.h>
30#include <assert.h>
31
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/nlr.h"
33#include "py/compile.h"
34#include "py/objmodule.h"
35#include "py/runtime.h"
36#include "py/builtin.h"
Paul Sokolovsky640e0b22015-01-20 11:52:12 +020037#include "py/frozenmod.h"
Damien George66028ab2014-01-03 14:03:48 +000038
Paul Sokolovskye0813292014-04-11 23:08:29 +030039#if 0 // print debugging info
40#define DEBUG_PRINT (1)
41#define DEBUG_printf DEBUG_printf
42#else // don't print debugging info
Damien George7860c2a2014-11-05 21:16:41 +000043#define DEBUG_PRINT (0)
Paul Sokolovskye0813292014-04-11 23:08:29 +030044#define DEBUG_printf(...) (void)0
45#endif
46
Damien Georgee09ffa12014-02-05 23:57:48 +000047#define PATH_SEP_CHAR '/'
48
Damien George78d702c2014-12-09 16:19:48 +000049#if MICROPY_MODULE_WEAK_LINKS
Damien Georgecbf76742015-11-27 13:38:15 +000050STATIC const mp_rom_map_elem_t mp_builtin_module_weak_links_table[] = {
Damien George78d702c2014-12-09 16:19:48 +000051 MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS
52};
53
54STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_weak_links_map, mp_builtin_module_weak_links_table);
55#endif
56
Paul Sokolovskye5a37592014-10-25 21:04:13 +030057bool mp_obj_is_package(mp_obj_t module) {
58 mp_obj_t dest[2];
59 mp_load_method_maybe(module, MP_QSTR___path__, dest);
60 return dest[0] != MP_OBJ_NULL;
61}
62
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +030063// Stat either frozen or normal module by a given path
64// (whatever is available, if at all).
65STATIC mp_import_stat_t mp_import_stat_any(const char *path) {
Paul Sokolovsky8a2970e2016-05-21 22:23:08 +030066 #if MICROPY_MODULE_FROZEN_STR
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +030067 mp_import_stat_t st = mp_frozen_stat(path);
68 if (st != MP_IMPORT_STAT_NO_EXIST) {
69 return st;
70 }
Paul Sokolovsky8a2970e2016-05-21 22:23:08 +030071 #endif
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +030072 return mp_import_stat(path);
73}
74
Damien George8a0801a2014-06-11 19:55:46 +010075STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +030076 mp_import_stat_t stat = mp_import_stat_any(vstr_null_terminated_str(path));
Paul Sokolovsky078172d2015-02-16 12:10:13 +020077 DEBUG_printf("stat %s: %d\n", vstr_str(path), stat);
Damien Georgee09ffa12014-02-05 23:57:48 +000078 if (stat == MP_IMPORT_STAT_DIR) {
79 return stat;
Damien George66028ab2014-01-03 14:03:48 +000080 }
Damien George432e8272015-11-02 21:57:42 +000081
Damien Georgee09ffa12014-02-05 23:57:48 +000082 vstr_add_str(path, ".py");
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +030083 stat = mp_import_stat_any(vstr_null_terminated_str(path));
Damien Georgee09ffa12014-02-05 23:57:48 +000084 if (stat == MP_IMPORT_STAT_FILE) {
85 return stat;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020086 }
Damien George432e8272015-11-02 21:57:42 +000087
88 #if MICROPY_PERSISTENT_CODE_LOAD
89 vstr_ins_byte(path, path->len - 2, 'm');
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +030090 stat = mp_import_stat_any(vstr_null_terminated_str(path));
Damien George432e8272015-11-02 21:57:42 +000091 if (stat == MP_IMPORT_STAT_FILE) {
92 return stat;
93 }
94 #endif
95
Damien Georgee09ffa12014-02-05 23:57:48 +000096 return MP_IMPORT_STAT_NO_EXIST;
97}
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020098
Damien George8a0801a2014-06-11 19:55:46 +010099STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
Damien Georgeee3fd462014-05-24 23:03:12 +0100100#if MICROPY_PY_SYS
Sven Wegener238ab502014-11-05 21:02:33 +0100101 // extract the list of paths
102 mp_uint_t path_num;
103 mp_obj_t *path_items;
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300104 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200105
106 if (path_num == 0) {
Sven Wegener238ab502014-11-05 21:02:33 +0100107#endif
Damien Georged17926d2014-03-30 13:35:08 +0100108 // mp_sys_path is empty, so just use the given file name
Damien Georgee09ffa12014-02-05 23:57:48 +0000109 vstr_add_strn(dest, file_str, file_len);
110 return stat_dir_or_file(dest);
Sven Wegener238ab502014-11-05 21:02:33 +0100111#if MICROPY_PY_SYS
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200112 } else {
Damien Georgee09ffa12014-02-05 23:57:48 +0000113 // go through each path looking for a directory or file
Damien George42f3de92014-10-03 17:44:14 +0000114 for (mp_uint_t i = 0; i < path_num; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000115 vstr_reset(dest);
Damien Georged182b982014-08-30 14:19:41 +0100116 mp_uint_t p_len;
Damien George698ec212014-02-08 18:17:23 +0000117 const char *p = mp_obj_str_get_data(path_items[i], &p_len);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200118 if (p_len > 0) {
Damien George698ec212014-02-08 18:17:23 +0000119 vstr_add_strn(dest, p, p_len);
Damien Georgee09ffa12014-02-05 23:57:48 +0000120 vstr_add_char(dest, PATH_SEP_CHAR);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200121 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000122 vstr_add_strn(dest, file_str, file_len);
123 mp_import_stat_t stat = stat_dir_or_file(dest);
124 if (stat != MP_IMPORT_STAT_NO_EXIST) {
125 return stat;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200126 }
127 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000128
129 // could not find a directory or file
130 return MP_IMPORT_STAT_NO_EXIST;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200131 }
Sven Wegener238ab502014-11-05 21:02:33 +0100132#endif
Damien Georgee09ffa12014-02-05 23:57:48 +0000133}
134
Damien Georgedd5353a2015-12-18 12:35:44 +0000135#if MICROPY_ENABLE_COMPILER
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200136STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char *fname) {
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200137
Damien George66028ab2014-01-03 14:03:48 +0000138 if (lex == NULL) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000139 // we verified the file exists using stat, but lexer could still fail
Damien George1e9a92f2014-11-06 17:36:16 +0000140 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
141 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found"));
142 } else {
143 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200144 "no module named '%s'", fname));
Damien George1e9a92f2014-11-06 17:36:16 +0000145 }
Damien George66028ab2014-01-03 14:03:48 +0000146 }
147
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300148 #if MICROPY_PY___FILE__
Damien Georgea4c52c52014-12-05 19:35:18 +0000149 qstr source_name = lex->source_name;
Damien Georgec4d08682014-10-05 20:13:34 +0100150 mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300151 #endif
Damien George66028ab2014-01-03 14:03:48 +0000152
Damien Georgec4d08682014-10-05 20:13:34 +0100153 // parse, compile and execute the module in its context
154 mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
155 mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000156}
Damien Georgedd5353a2015-12-18 12:35:44 +0000157#endif
Damien Georgee09ffa12014-02-05 23:57:48 +0000158
Damien George0a2e9652016-01-31 22:24:16 +0000159#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY
Damien George432e8272015-11-02 21:57:42 +0000160STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
161 #if MICROPY_PY___FILE__
162 // TODO
163 //qstr source_name = lex->source_name;
164 //mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
165 #endif
166
167 // execute the module in its context
168 mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
169
170 // save context
171 mp_obj_dict_t *volatile old_globals = mp_globals_get();
172 mp_obj_dict_t *volatile old_locals = mp_locals_get();
173
174 // set new context
175 mp_globals_set(mod_globals);
176 mp_locals_set(mod_globals);
177
178 nlr_buf_t nlr;
179 if (nlr_push(&nlr) == 0) {
180 mp_obj_t module_fun = mp_make_function_from_raw_code(raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
181 mp_call_function_0(module_fun);
182
183 // finish nlr block, restore context
184 nlr_pop();
185 mp_globals_set(old_globals);
186 mp_locals_set(old_locals);
187 } else {
188 // exception; restore context and re-raise same exception
189 mp_globals_set(old_globals);
190 mp_locals_set(old_locals);
Damien George999cedb2015-11-27 17:01:44 +0000191 nlr_jump(nlr.ret_val);
Damien George432e8272015-11-02 21:57:42 +0000192 }
193}
194#endif
195
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200196STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
Damien George0a2e9652016-01-31 22:24:16 +0000197 #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_ENABLE_COMPILER
Damien George827b0f72015-01-29 13:57:23 +0000198 char *file_str = vstr_null_terminated_str(file);
Damien George0a2e9652016-01-31 22:24:16 +0000199 #endif
Damien Georgedd5353a2015-12-18 12:35:44 +0000200
Damien George432e8272015-11-02 21:57:42 +0000201 #if MICROPY_PERSISTENT_CODE_LOAD
202 if (file_str[file->len - 3] == 'm') {
203 mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
204 do_execute_raw_code(module_obj, raw_code);
Damien Georgedd5353a2015-12-18 12:35:44 +0000205 return;
206 }
Damien George432e8272015-11-02 21:57:42 +0000207 #endif
Damien Georgedd5353a2015-12-18 12:35:44 +0000208
209 #if MICROPY_ENABLE_COMPILER
Damien George432e8272015-11-02 21:57:42 +0000210 {
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +0300211 void *modref;
Paul Sokolovsky2c573f02016-05-21 22:37:58 +0300212 #if MICROPY_MODULE_FROZEN
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +0300213 int frozen_type = mp_find_frozen_module(file_str, file->len, &modref);
Paul Sokolovsky2c573f02016-05-21 22:37:58 +0300214 #else
215 int frozen_type = MP_FROZEN_NONE;
216 #endif
Paul Sokolovsky8a2970e2016-05-21 22:23:08 +0300217 #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +0300218 if (frozen_type == MP_FROZEN_MPY) {
219 do_execute_raw_code(module_obj, modref);
220 return;
221 }
222 #endif
223 if (frozen_type == MP_FROZEN_NONE) {
224 modref = mp_lexer_new_from_file(file_str);
225 }
226 do_load_from_lexer(module_obj, modref, file_str);
Damien George432e8272015-11-02 21:57:42 +0000227 }
Damien Georgedd5353a2015-12-18 12:35:44 +0000228 #else
229 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
230 "script compilation not supported"));
231 #endif
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200232}
233
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200234STATIC void chop_component(const char *start, const char **end) {
235 const char *p = *end;
236 while (p > start) {
237 if (*--p == '.') {
238 *end = p;
239 return;
240 }
241 }
242 *end = p;
243}
244
Damien George4b72b3a2016-01-03 14:21:40 +0000245mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300246#if DEBUG_PRINT
Damien Georgeeaaebf32014-09-23 10:59:05 +0100247 DEBUG_printf("__import__:\n");
Damien George42f3de92014-10-03 17:44:14 +0000248 for (mp_uint_t i = 0; i < n_args; i++) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100249 DEBUG_printf(" ");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200250 mp_obj_print(args[i], PRINT_REPR);
Damien Georgeeaaebf32014-09-23 10:59:05 +0100251 DEBUG_printf("\n");
Damien Georgee09ffa12014-02-05 23:57:48 +0000252 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300253#endif
Damien Georgee09ffa12014-02-05 23:57:48 +0000254
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300255 mp_obj_t module_name = args[0];
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200256 mp_obj_t fromtuple = mp_const_none;
Damien George42f3de92014-10-03 17:44:14 +0000257 mp_int_t level = 0;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200258 if (n_args >= 4) {
259 fromtuple = args[3];
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200260 if (n_args >= 5) {
261 level = MP_OBJ_SMALL_INT_VALUE(args[4]);
262 }
263 }
264
Damien Georged182b982014-08-30 14:19:41 +0100265 mp_uint_t mod_len;
266 const char *mod_str = mp_obj_str_get_data(module_name, &mod_len);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300267
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200268 if (level != 0) {
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300269 // What we want to do here is to take name of current module,
270 // chop <level> trailing components, and concatenate with passed-in
271 // module name, thus resolving relative import name into absolue.
272 // This even appears to be correct per
273 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
274 // "Relative imports use a module's __name__ attribute to determine that
275 // module's position in the package hierarchy."
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200276 level--;
Damien George999cedb2015-11-27 17:01:44 +0000277 mp_obj_t this_name_q = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300278 assert(this_name_q != MP_OBJ_NULL);
Paul Sokolovsky9780e552015-06-29 00:21:36 +0300279 #if MICROPY_CPYTHON_COMPAT
280 if (MP_OBJ_QSTR_VALUE(this_name_q) == MP_QSTR___main__) {
281 // This is a module run by -m command-line switch, get its real name from backup attribute
Damien George999cedb2015-11-27 17:01:44 +0000282 this_name_q = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Paul Sokolovsky9780e552015-06-29 00:21:36 +0300283 }
284 #endif
Damien George999cedb2015-11-27 17:01:44 +0000285 mp_map_t *globals_map = &mp_globals_get()->map;
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200286 mp_map_elem_t *elem = mp_map_lookup(globals_map, MP_OBJ_NEW_QSTR(MP_QSTR___path__), MP_MAP_LOOKUP);
287 bool is_pkg = (elem != NULL);
288
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300289#if DEBUG_PRINT
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200290 DEBUG_printf("Current module/package: ");
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300291 mp_obj_print(this_name_q, PRINT_REPR);
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200292 DEBUG_printf(", is_package: %d", is_pkg);
Damien Georgeeaaebf32014-09-23 10:59:05 +0100293 DEBUG_printf("\n");
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300294#endif
295
Damien Georged182b982014-08-30 14:19:41 +0100296 mp_uint_t this_name_l;
297 const char *this_name = mp_obj_str_get_data(this_name_q, &this_name_l);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300298
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200299 const char *p = this_name + this_name_l;
300 if (!is_pkg) {
301 // We have module, but relative imports are anchored at package, so
302 // go there.
303 chop_component(this_name, &p);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300304 }
305
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200306
307 uint dots_seen = 0;
308 while (level--) {
309 chop_component(this_name, &p);
310 dots_seen++;
311 }
312
313 if (dots_seen == 0 && level >= 1) {
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300314 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
315 // "If the module's name does not contain any package information
316 // (e.g. it is set to '__main__') then relative imports are
317 // resolved as if the module were a top level module, regardless
318 // of where the module is actually located on the file system."
319 // Supposedly this if catches this condition and resolve it properly
320 // TODO: But nobody knows for sure. This condition happens when
321 // package's __init__.py does something like "import .submod". So,
322 // maybe we should check for package here? But quote above doesn't
323 // talk about packages, it talks about dot-less module names.
Paul Sokolovsky078172d2015-02-16 12:10:13 +0200324 DEBUG_printf("Warning: no dots in current module name and level>0\n");
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300325 p = this_name + this_name_l;
Paul Sokolovsky9e6c8292015-02-16 12:10:13 +0200326 } else if (level != -1) {
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300327 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import"));
328 }
329
Damien George963a5a32015-01-16 17:47:07 +0000330 uint new_mod_l = (mod_len == 0 ? (size_t)(p - this_name) : (size_t)(p - this_name) + 1 + mod_len);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300331 char *new_mod = alloca(new_mod_l);
332 memcpy(new_mod, this_name, p - this_name);
333 if (mod_len != 0) {
334 new_mod[p - this_name] = '.';
335 memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len);
336 }
337
338 qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l);
Paul Sokolovskyc4045f52015-06-27 00:34:53 +0300339 DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q));
340 if (new_mod_q == MP_QSTR_) {
341 // CPython raises SystemError
342 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "cannot perform relative import"));
343 }
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300344 module_name = MP_OBJ_NEW_QSTR(new_mod_q);
345 mod_str = new_mod;
346 mod_len = new_mod_l;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200347 }
348
Damien Georgee09ffa12014-02-05 23:57:48 +0000349 // check if module already exists
Paul Sokolovsky80648922015-01-21 23:14:46 +0200350 qstr module_name_qstr = mp_obj_str_get_qstr(module_name);
351 mp_obj_t module_obj = mp_module_get(module_name_qstr);
Damien Georgee09ffa12014-02-05 23:57:48 +0000352 if (module_obj != MP_OBJ_NULL) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300353 DEBUG_printf("Module already loaded\n");
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200354 // If it's not a package, return module right away
355 char *p = strchr(mod_str, '.');
356 if (p == NULL) {
357 return module_obj;
358 }
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200359 // If fromlist is not empty, return leaf module
360 if (fromtuple != mp_const_none) {
361 return module_obj;
362 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200363 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200364 qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
Damien Georgecaac5422014-03-25 14:18:18 +0000365 return mp_module_get(pkg_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000366 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300367 DEBUG_printf("Module not yet loaded\n");
Damien Georgee09ffa12014-02-05 23:57:48 +0000368
Damien Georgee09ffa12014-02-05 23:57:48 +0000369 uint last = 0;
Damien George58ebde42014-05-21 20:32:59 +0100370 VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX)
Damien Georgee09ffa12014-02-05 23:57:48 +0000371 module_obj = MP_OBJ_NULL;
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200372 mp_obj_t top_module_obj = MP_OBJ_NULL;
373 mp_obj_t outer_module_obj = MP_OBJ_NULL;
Damien Georgee09ffa12014-02-05 23:57:48 +0000374 uint i;
375 for (i = 1; i <= mod_len; i++) {
376 if (i == mod_len || mod_str[i] == '.') {
377 // create a qstr for the module name up to this depth
378 qstr mod_name = qstr_from_strn(mod_str, i);
Paul Sokolovskye0813292014-04-11 23:08:29 +0300379 DEBUG_printf("Processing module: %s\n", qstr_str(mod_name));
Paul Sokolovsky078172d2015-02-16 12:10:13 +0200380 DEBUG_printf("Previous path: =%.*s=\n", vstr_len(&path), vstr_str(&path));
Damien Georgee09ffa12014-02-05 23:57:48 +0000381
382 // find the file corresponding to the module name
383 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000384 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000385 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000386 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000387 } else {
388 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000389 vstr_add_char(&path, PATH_SEP_CHAR);
390 vstr_add_strn(&path, mod_str + last, i - last);
391 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000392 }
Damien George0d3cb672015-01-28 23:43:01 +0000393 DEBUG_printf("Current path: %.*s\n", vstr_len(&path), vstr_str(&path));
Damien Georgee09ffa12014-02-05 23:57:48 +0000394
Damien Georgee09ffa12014-02-05 23:57:48 +0000395 if (stat == MP_IMPORT_STAT_NO_EXIST) {
Damien Georgec14a8162014-10-12 11:46:04 +0100396 #if MICROPY_MODULE_WEAK_LINKS
397 // check if there is a weak link to this module
398 if (i == mod_len) {
Damien George78d702c2014-12-09 16:19:48 +0000399 mp_map_elem_t *el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_map, MP_OBJ_NEW_QSTR(mod_name), MP_MAP_LOOKUP);
Damien Georgec14a8162014-10-12 11:46:04 +0100400 if (el == NULL) {
401 goto no_exist;
402 }
403 // found weak linked module
404 module_obj = el->value;
405 } else {
406 no_exist:
407 #else
408 {
409 #endif
410 // couldn't find the file, so fail
Damien George1e9a92f2014-11-06 17:36:16 +0000411 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
412 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found"));
413 } else {
414 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
Damien George044c4732015-04-11 13:03:37 +0100415 "no module named '%q'", mod_name));
Damien George1e9a92f2014-11-06 17:36:16 +0000416 }
Damien Georgec14a8162014-10-12 11:46:04 +0100417 }
418 } else {
419 // found the file, so get the module
420 module_obj = mp_module_get(mod_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000421 }
422
Damien Georgee09ffa12014-02-05 23:57:48 +0000423 if (module_obj == MP_OBJ_NULL) {
424 // module not already loaded, so load it!
425
426 module_obj = mp_obj_new_module(mod_name);
427
Paul Sokolovskye5035122014-10-25 21:16:24 +0300428 // if args[3] (fromtuple) has magic value False, set up
429 // this module for command-line "-m" option (set module's
430 // name to __main__ instead of real name).
431 if (i == mod_len && fromtuple == mp_const_false) {
Damien George999cedb2015-11-27 17:01:44 +0000432 mp_obj_module_t *o = MP_OBJ_TO_PTR(module_obj);
433 mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Paul Sokolovsky9780e552015-06-29 00:21:36 +0300434 #if MICROPY_CPYTHON_COMPAT
435 // Store real name in "__main__" attribute. Choosen semi-randonly, to reuse existing qstr's.
Damien George999cedb2015-11-27 17:01:44 +0000436 mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(mod_name));
Paul Sokolovsky9780e552015-06-29 00:21:36 +0300437 #endif
Paul Sokolovskye5035122014-10-25 21:16:24 +0300438 }
439
Damien Georgee09ffa12014-02-05 23:57:48 +0000440 if (stat == MP_IMPORT_STAT_DIR) {
Damien George0d3cb672015-01-28 23:43:01 +0000441 DEBUG_printf("%.*s is dir\n", vstr_len(&path), vstr_str(&path));
Chris Angelicodaf973a2014-06-06 03:51:03 +1000442 // https://docs.python.org/3/reference/import.html
Paul Sokolovskyf9589d22014-05-10 18:46:02 +0300443 // "Specifically, any module that contains a __path__ attribute is considered a package."
Damien George2617eeb2014-05-25 22:27:57 +0100444 mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false));
Damien George354d15a2014-02-06 21:11:19 +0000445 vstr_add_char(&path, PATH_SEP_CHAR);
446 vstr_add_str(&path, "__init__.py");
Paul Sokolovskyfb742cd2016-05-21 21:33:42 +0300447 if (mp_import_stat_any(vstr_null_terminated_str(&path)) != MP_IMPORT_STAT_FILE) {
Damien George280e7202014-03-15 14:33:09 +0000448 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Paul Sokolovsky8a8c1fc2015-01-01 09:29:28 +0200449 mp_warning("%s is imported as namespace package", vstr_str(&path));
Paul Sokolovskya5854d22014-04-15 01:23:40 +0300450 } else {
451 do_load(module_obj, &path);
Paul Sokolovskyad6178b2014-05-10 19:00:03 +0300452 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgee09ffa12014-02-05 23:57:48 +0000453 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000454 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000455 do_load(module_obj, &path);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200456 // TODO: We cannot just break here, at the very least, we must execute
457 // trailer code below. But otherwise if there're remaining components,
458 // that would be (??) object path within module, not modules path within FS.
459 // break;
Damien Georgee09ffa12014-02-05 23:57:48 +0000460 }
461 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200462 if (outer_module_obj != MP_OBJ_NULL) {
463 qstr s = qstr_from_strn(mod_str + last, i - last);
Damien Georged17926d2014-03-30 13:35:08 +0100464 mp_store_attr(outer_module_obj, s, module_obj);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200465 }
466 outer_module_obj = module_obj;
467 if (top_module_obj == MP_OBJ_NULL) {
468 top_module_obj = module_obj;
469 }
470 last = i + 1;
Damien Georgee09ffa12014-02-05 23:57:48 +0000471 }
472 }
473
474 if (i < mod_len) {
475 // we loaded a package, now need to load objects from within that package
476 // TODO
477 assert(0);
478 }
479
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200480 // If fromlist is not empty, return leaf module
481 if (fromtuple != mp_const_none) {
482 return module_obj;
483 }
484 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200485 return top_module_obj;
Damien George66028ab2014-01-03 14:03:48 +0000486}
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200487MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);