blob: 73bf0b4722a14689dab8b40d96a0441b289268af [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
50STATIC const mp_map_elem_t mp_builtin_module_weak_links_table[] = {
51 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
Damien George8a0801a2014-06-11 19:55:46 +010063STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
Damien Georgee09ffa12014-02-05 23:57:48 +000064 //printf("stat %s\n", vstr_str(path));
65 mp_import_stat_t stat = mp_import_stat(vstr_str(path));
66 if (stat == MP_IMPORT_STAT_DIR) {
67 return stat;
Damien George66028ab2014-01-03 14:03:48 +000068 }
Damien Georgee09ffa12014-02-05 23:57:48 +000069 vstr_add_str(path, ".py");
70 stat = mp_import_stat(vstr_str(path));
71 if (stat == MP_IMPORT_STAT_FILE) {
72 return stat;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020073 }
Damien Georgee09ffa12014-02-05 23:57:48 +000074 return MP_IMPORT_STAT_NO_EXIST;
75}
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020076
Damien George8a0801a2014-06-11 19:55:46 +010077STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
Damien Georgeee3fd462014-05-24 23:03:12 +010078#if MICROPY_PY_SYS
Sven Wegener238ab502014-11-05 21:02:33 +010079 // extract the list of paths
80 mp_uint_t path_num;
81 mp_obj_t *path_items;
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030082 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020083
84 if (path_num == 0) {
Sven Wegener238ab502014-11-05 21:02:33 +010085#endif
Damien Georged17926d2014-03-30 13:35:08 +010086 // mp_sys_path is empty, so just use the given file name
Damien Georgee09ffa12014-02-05 23:57:48 +000087 vstr_add_strn(dest, file_str, file_len);
88 return stat_dir_or_file(dest);
Sven Wegener238ab502014-11-05 21:02:33 +010089#if MICROPY_PY_SYS
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020090 } else {
Damien Georgee09ffa12014-02-05 23:57:48 +000091 // go through each path looking for a directory or file
Damien George42f3de92014-10-03 17:44:14 +000092 for (mp_uint_t i = 0; i < path_num; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +000093 vstr_reset(dest);
Damien Georged182b982014-08-30 14:19:41 +010094 mp_uint_t p_len;
Damien George698ec212014-02-08 18:17:23 +000095 const char *p = mp_obj_str_get_data(path_items[i], &p_len);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020096 if (p_len > 0) {
Damien George698ec212014-02-08 18:17:23 +000097 vstr_add_strn(dest, p, p_len);
Damien Georgee09ffa12014-02-05 23:57:48 +000098 vstr_add_char(dest, PATH_SEP_CHAR);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020099 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000100 vstr_add_strn(dest, file_str, file_len);
101 mp_import_stat_t stat = stat_dir_or_file(dest);
102 if (stat != MP_IMPORT_STAT_NO_EXIST) {
103 return stat;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200104 }
105 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000106
107 // could not find a directory or file
108 return MP_IMPORT_STAT_NO_EXIST;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200109 }
Sven Wegener238ab502014-11-05 21:02:33 +0100110#endif
Damien Georgee09ffa12014-02-05 23:57:48 +0000111}
112
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200113STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char *fname) {
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200114
Damien George66028ab2014-01-03 14:03:48 +0000115 if (lex == NULL) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000116 // we verified the file exists using stat, but lexer could still fail
Damien George1e9a92f2014-11-06 17:36:16 +0000117 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
118 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found"));
119 } else {
120 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200121 "no module named '%s'", fname));
Damien George1e9a92f2014-11-06 17:36:16 +0000122 }
Damien George66028ab2014-01-03 14:03:48 +0000123 }
124
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300125 #if MICROPY_PY___FILE__
Damien Georgea4c52c52014-12-05 19:35:18 +0000126 qstr source_name = lex->source_name;
Damien Georgec4d08682014-10-05 20:13:34 +0100127 mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300128 #endif
Damien George66028ab2014-01-03 14:03:48 +0000129
Damien Georgec4d08682014-10-05 20:13:34 +0100130 // parse, compile and execute the module in its context
131 mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
132 mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000133}
134
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200135STATIC 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
Paul Sokolovsky8becca72014-10-25 21:03:20 +0300141mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300142#if DEBUG_PRINT
Damien Georgeeaaebf32014-09-23 10:59:05 +0100143 DEBUG_printf("__import__:\n");
Damien George42f3de92014-10-03 17:44:14 +0000144 for (mp_uint_t i = 0; i < n_args; i++) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100145 DEBUG_printf(" ");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200146 mp_obj_print(args[i], PRINT_REPR);
Damien Georgeeaaebf32014-09-23 10:59:05 +0100147 DEBUG_printf("\n");
Damien Georgee09ffa12014-02-05 23:57:48 +0000148 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300149#endif
Damien Georgee09ffa12014-02-05 23:57:48 +0000150
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300151 mp_obj_t module_name = args[0];
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200152 mp_obj_t fromtuple = mp_const_none;
Damien George42f3de92014-10-03 17:44:14 +0000153 mp_int_t level = 0;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200154 if (n_args >= 4) {
155 fromtuple = args[3];
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200156 if (n_args >= 5) {
157 level = MP_OBJ_SMALL_INT_VALUE(args[4]);
158 }
159 }
160
Damien Georged182b982014-08-30 14:19:41 +0100161 mp_uint_t mod_len;
162 const char *mod_str = mp_obj_str_get_data(module_name, &mod_len);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300163
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200164 if (level != 0) {
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300165 // What we want to do here is to take name of current module,
166 // chop <level> trailing components, and concatenate with passed-in
167 // module name, thus resolving relative import name into absolue.
168 // This even appears to be correct per
169 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
170 // "Relative imports use a module's __name__ attribute to determine that
171 // module's position in the package hierarchy."
172 mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
173 assert(this_name_q != MP_OBJ_NULL);
174#if DEBUG_PRINT
Damien Georgeeaaebf32014-09-23 10:59:05 +0100175 DEBUG_printf("Current module: ");
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300176 mp_obj_print(this_name_q, PRINT_REPR);
Damien Georgeeaaebf32014-09-23 10:59:05 +0100177 DEBUG_printf("\n");
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300178#endif
179
Damien Georged182b982014-08-30 14:19:41 +0100180 mp_uint_t this_name_l;
181 const char *this_name = mp_obj_str_get_data(this_name_q, &this_name_l);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300182
183 uint dots_seen = 0;
184 const char *p = this_name + this_name_l - 1;
185 while (p > this_name) {
186 if (*p == '.') {
187 dots_seen++;
188 if (--level == 0) {
189 break;
190 }
191 }
192 p--;
193 }
194
195 if (dots_seen == 0 && level == 1) {
196 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
197 // "If the module's name does not contain any package information
198 // (e.g. it is set to '__main__') then relative imports are
199 // resolved as if the module were a top level module, regardless
200 // of where the module is actually located on the file system."
201 // Supposedly this if catches this condition and resolve it properly
202 // TODO: But nobody knows for sure. This condition happens when
203 // package's __init__.py does something like "import .submod". So,
204 // maybe we should check for package here? But quote above doesn't
205 // talk about packages, it talks about dot-less module names.
206 p = this_name + this_name_l;
207 } else if (level != 0) {
208 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import"));
209 }
210
Damien George963a5a32015-01-16 17:47:07 +0000211 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 +0300212 char *new_mod = alloca(new_mod_l);
213 memcpy(new_mod, this_name, p - this_name);
214 if (mod_len != 0) {
215 new_mod[p - this_name] = '.';
216 memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len);
217 }
218
219 qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l);
220 DEBUG_printf("Resolved relative name: %s\n", qstr_str(new_mod_q));
221 module_name = MP_OBJ_NEW_QSTR(new_mod_q);
222 mod_str = new_mod;
223 mod_len = new_mod_l;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200224 }
225
Damien Georgee09ffa12014-02-05 23:57:48 +0000226 // check if module already exists
Paul Sokolovsky80648922015-01-21 23:14:46 +0200227 qstr module_name_qstr = mp_obj_str_get_qstr(module_name);
228 mp_obj_t module_obj = mp_module_get(module_name_qstr);
Damien Georgee09ffa12014-02-05 23:57:48 +0000229 if (module_obj != MP_OBJ_NULL) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300230 DEBUG_printf("Module already loaded\n");
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200231 // If it's not a package, return module right away
232 char *p = strchr(mod_str, '.');
233 if (p == NULL) {
234 return module_obj;
235 }
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200236 // If fromlist is not empty, return leaf module
237 if (fromtuple != mp_const_none) {
238 return module_obj;
239 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200240 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200241 qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
Damien Georgecaac5422014-03-25 14:18:18 +0000242 return mp_module_get(pkg_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000243 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300244 DEBUG_printf("Module not yet loaded\n");
Damien Georgee09ffa12014-02-05 23:57:48 +0000245
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200246 #if MICROPY_MODULE_FROZEN
247 mp_lexer_t *lex = mp_find_frozen_module(mod_str, mod_len);
248 if (lex != NULL) {
Paul Sokolovsky80648922015-01-21 23:14:46 +0200249 module_obj = mp_obj_new_module(module_name_qstr);
Paul Sokolovsky640e0b22015-01-20 11:52:12 +0200250 do_load_from_lexer(module_obj, lex, mod_str);
251 return module_obj;
252 }
253 #endif
254
Damien Georgee09ffa12014-02-05 23:57:48 +0000255 uint last = 0;
Damien George58ebde42014-05-21 20:32:59 +0100256 VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX)
Damien Georgee09ffa12014-02-05 23:57:48 +0000257 module_obj = MP_OBJ_NULL;
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200258 mp_obj_t top_module_obj = MP_OBJ_NULL;
259 mp_obj_t outer_module_obj = MP_OBJ_NULL;
Damien Georgee09ffa12014-02-05 23:57:48 +0000260 uint i;
261 for (i = 1; i <= mod_len; i++) {
262 if (i == mod_len || mod_str[i] == '.') {
263 // create a qstr for the module name up to this depth
264 qstr mod_name = qstr_from_strn(mod_str, i);
Paul Sokolovskye0813292014-04-11 23:08:29 +0300265 DEBUG_printf("Processing module: %s\n", qstr_str(mod_name));
Paul Sokolovskyad6178b2014-05-10 19:00:03 +0300266 DEBUG_printf("Previous path: %s\n", vstr_str(&path));
Damien Georgee09ffa12014-02-05 23:57:48 +0000267
268 // find the file corresponding to the module name
269 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000270 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000271 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000272 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000273 } else {
274 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000275 vstr_add_char(&path, PATH_SEP_CHAR);
276 vstr_add_strn(&path, mod_str + last, i - last);
277 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000278 }
Paul Sokolovskyad6178b2014-05-10 19:00:03 +0300279 DEBUG_printf("Current path: %s\n", vstr_str(&path));
Damien Georgee09ffa12014-02-05 23:57:48 +0000280
Damien Georgee09ffa12014-02-05 23:57:48 +0000281 if (stat == MP_IMPORT_STAT_NO_EXIST) {
Damien Georgec14a8162014-10-12 11:46:04 +0100282 #if MICROPY_MODULE_WEAK_LINKS
283 // check if there is a weak link to this module
284 if (i == mod_len) {
Damien George78d702c2014-12-09 16:19:48 +0000285 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 +0100286 if (el == NULL) {
287 goto no_exist;
288 }
289 // found weak linked module
290 module_obj = el->value;
291 } else {
292 no_exist:
293 #else
294 {
295 #endif
296 // couldn't find the file, so fail
Damien George1e9a92f2014-11-06 17:36:16 +0000297 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
298 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found"));
299 } else {
300 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
301 "no module named '%s'", qstr_str(mod_name)));
302 }
Damien Georgec14a8162014-10-12 11:46:04 +0100303 }
304 } else {
305 // found the file, so get the module
306 module_obj = mp_module_get(mod_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000307 }
308
Damien Georgee09ffa12014-02-05 23:57:48 +0000309 if (module_obj == MP_OBJ_NULL) {
310 // module not already loaded, so load it!
311
312 module_obj = mp_obj_new_module(mod_name);
313
Paul Sokolovskye5035122014-10-25 21:16:24 +0300314 // if args[3] (fromtuple) has magic value False, set up
315 // this module for command-line "-m" option (set module's
316 // name to __main__ instead of real name).
317 if (i == mod_len && fromtuple == mp_const_false) {
318 mp_obj_module_t *o = module_obj;
319 mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
320 }
321
Damien Georgee09ffa12014-02-05 23:57:48 +0000322 if (stat == MP_IMPORT_STAT_DIR) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300323 DEBUG_printf("%s is dir\n", vstr_str(&path));
Chris Angelicodaf973a2014-06-06 03:51:03 +1000324 // https://docs.python.org/3/reference/import.html
Paul Sokolovskyf9589d22014-05-10 18:46:02 +0300325 // "Specifically, any module that contains a __path__ attribute is considered a package."
Damien George2617eeb2014-05-25 22:27:57 +0100326 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 +0000327 vstr_add_char(&path, PATH_SEP_CHAR);
328 vstr_add_str(&path, "__init__.py");
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200329 if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
Damien George280e7202014-03-15 14:33:09 +0000330 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Paul Sokolovsky8a8c1fc2015-01-01 09:29:28 +0200331 mp_warning("%s is imported as namespace package", vstr_str(&path));
Paul Sokolovskya5854d22014-04-15 01:23:40 +0300332 } else {
333 do_load(module_obj, &path);
Paul Sokolovskyad6178b2014-05-10 19:00:03 +0300334 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgee09ffa12014-02-05 23:57:48 +0000335 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000336 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000337 do_load(module_obj, &path);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200338 // TODO: We cannot just break here, at the very least, we must execute
339 // trailer code below. But otherwise if there're remaining components,
340 // that would be (??) object path within module, not modules path within FS.
341 // break;
Damien Georgee09ffa12014-02-05 23:57:48 +0000342 }
343 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200344 if (outer_module_obj != MP_OBJ_NULL) {
345 qstr s = qstr_from_strn(mod_str + last, i - last);
Damien Georged17926d2014-03-30 13:35:08 +0100346 mp_store_attr(outer_module_obj, s, module_obj);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200347 }
348 outer_module_obj = module_obj;
349 if (top_module_obj == MP_OBJ_NULL) {
350 top_module_obj = module_obj;
351 }
352 last = i + 1;
Damien Georgee09ffa12014-02-05 23:57:48 +0000353 }
354 }
355
356 if (i < mod_len) {
357 // we loaded a package, now need to load objects from within that package
358 // TODO
359 assert(0);
360 }
361
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200362 // If fromlist is not empty, return leaf module
363 if (fromtuple != mp_const_none) {
364 return module_obj;
365 }
366 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200367 return top_module_obj;
Damien George66028ab2014-01-03 14:03:48 +0000368}
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200369MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);