blob: f1288744a8caafd46c7be379c8fd6734235147d7 [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 <stdint.h>
Damien George66028ab2014-01-03 14:03:48 +000029#include <stdio.h>
Damien George66028ab2014-01-03 14:03:48 +000030#include <string.h>
31#include <assert.h>
32
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030033#include "mpconfig.h"
Damien George66028ab2014-01-03 14:03:48 +000034#include "nlr.h"
35#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damien George66028ab2014-01-03 14:03:48 +000037#include "lexer.h"
38#include "lexerunix.h"
39#include "parse.h"
Damien George66028ab2014-01-03 14:03:48 +000040#include "obj.h"
Damien Georgecaac5422014-03-25 14:18:18 +000041#include "objmodule.h"
Damien Georgec5966122014-02-15 16:10:44 +000042#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000043#include "compile.h"
Damien George66028ab2014-01-03 14:03:48 +000044#include "runtime0.h"
45#include "runtime.h"
Damien George66028ab2014-01-03 14:03:48 +000046#include "builtin.h"
Damien Georgec14a8162014-10-12 11:46:04 +010047#include "builtintables.h"
Damien George66028ab2014-01-03 14:03:48 +000048
Paul Sokolovskye0813292014-04-11 23:08:29 +030049#if 0 // print debugging info
50#define DEBUG_PRINT (1)
51#define DEBUG_printf DEBUG_printf
52#else // don't print debugging info
Damien George7860c2a2014-11-05 21:16:41 +000053#define DEBUG_PRINT (0)
Paul Sokolovskye0813292014-04-11 23:08:29 +030054#define DEBUG_printf(...) (void)0
55#endif
56
Damien Georgee09ffa12014-02-05 23:57:48 +000057#define PATH_SEP_CHAR '/'
58
Paul Sokolovskye5a37592014-10-25 21:04:13 +030059bool mp_obj_is_package(mp_obj_t module) {
60 mp_obj_t dest[2];
61 mp_load_method_maybe(module, MP_QSTR___path__, dest);
62 return dest[0] != MP_OBJ_NULL;
63}
64
Damien George8a0801a2014-06-11 19:55:46 +010065STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
Damien Georgee09ffa12014-02-05 23:57:48 +000066 //printf("stat %s\n", vstr_str(path));
67 mp_import_stat_t stat = mp_import_stat(vstr_str(path));
68 if (stat == MP_IMPORT_STAT_DIR) {
69 return stat;
Damien George66028ab2014-01-03 14:03:48 +000070 }
Damien Georgee09ffa12014-02-05 23:57:48 +000071 vstr_add_str(path, ".py");
72 stat = mp_import_stat(vstr_str(path));
73 if (stat == MP_IMPORT_STAT_FILE) {
74 return stat;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020075 }
Damien Georgee09ffa12014-02-05 23:57:48 +000076 return MP_IMPORT_STAT_NO_EXIST;
77}
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020078
Damien George8a0801a2014-06-11 19:55:46 +010079STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
Damien Georgee09ffa12014-02-05 23:57:48 +000080 // extract the list of paths
Damien George9c4cbe22014-08-30 14:04:14 +010081 mp_uint_t path_num = 0;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020082 mp_obj_t *path_items;
Damien Georgeee3fd462014-05-24 23:03:12 +010083#if MICROPY_PY_SYS
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030084 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Damien George49f20b82014-04-13 13:05:16 +010085#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020086
87 if (path_num == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010088 // mp_sys_path is empty, so just use the given file name
Damien Georgee09ffa12014-02-05 23:57:48 +000089 vstr_add_strn(dest, file_str, file_len);
90 return stat_dir_or_file(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020091 } else {
Damien Georgee09ffa12014-02-05 23:57:48 +000092 // go through each path looking for a directory or file
Damien George42f3de92014-10-03 17:44:14 +000093 for (mp_uint_t i = 0; i < path_num; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +000094 vstr_reset(dest);
Damien Georged182b982014-08-30 14:19:41 +010095 mp_uint_t p_len;
Damien George698ec212014-02-08 18:17:23 +000096 const char *p = mp_obj_str_get_data(path_items[i], &p_len);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020097 if (p_len > 0) {
Damien George698ec212014-02-08 18:17:23 +000098 vstr_add_strn(dest, p, p_len);
Damien Georgee09ffa12014-02-05 23:57:48 +000099 vstr_add_char(dest, PATH_SEP_CHAR);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200100 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000101 vstr_add_strn(dest, file_str, file_len);
102 mp_import_stat_t stat = stat_dir_or_file(dest);
103 if (stat != MP_IMPORT_STAT_NO_EXIST) {
104 return stat;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200105 }
106 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000107
108 // could not find a directory or file
109 return MP_IMPORT_STAT_NO_EXIST;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200110 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000111}
112
Damien George8a0801a2014-06-11 19:55:46 +0100113STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000114 // create the lexer
115 mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file));
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200116
Damien George66028ab2014-01-03 14:03:48 +0000117 if (lex == NULL) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000118 // we verified the file exists using stat, but lexer could still fail
Andrew Schellerf78cfaf2014-04-09 19:56:38 +0100119 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "No module named '%s'", vstr_str(file)));
Damien George66028ab2014-01-03 14:03:48 +0000120 }
121
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300122 #if MICROPY_PY___FILE__
Damien Georgec4d08682014-10-05 20:13:34 +0100123 qstr source_name = mp_lexer_source_name(lex);
124 mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300125 #endif
Damien George66028ab2014-01-03 14:03:48 +0000126
Damien Georgec4d08682014-10-05 20:13:34 +0100127 // parse, compile and execute the module in its context
128 mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
129 mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000130}
131
Paul Sokolovsky8becca72014-10-25 21:03:20 +0300132mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300133#if DEBUG_PRINT
Damien Georgeeaaebf32014-09-23 10:59:05 +0100134 DEBUG_printf("__import__:\n");
Damien George42f3de92014-10-03 17:44:14 +0000135 for (mp_uint_t i = 0; i < n_args; i++) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100136 DEBUG_printf(" ");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200137 mp_obj_print(args[i], PRINT_REPR);
Damien Georgeeaaebf32014-09-23 10:59:05 +0100138 DEBUG_printf("\n");
Damien Georgee09ffa12014-02-05 23:57:48 +0000139 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300140#endif
Damien Georgee09ffa12014-02-05 23:57:48 +0000141
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300142 mp_obj_t module_name = args[0];
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200143 mp_obj_t fromtuple = mp_const_none;
Damien George42f3de92014-10-03 17:44:14 +0000144 mp_int_t level = 0;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200145 if (n_args >= 4) {
146 fromtuple = args[3];
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200147 if (n_args >= 5) {
148 level = MP_OBJ_SMALL_INT_VALUE(args[4]);
149 }
150 }
151
Damien Georged182b982014-08-30 14:19:41 +0100152 mp_uint_t mod_len;
153 const char *mod_str = mp_obj_str_get_data(module_name, &mod_len);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300154
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200155 if (level != 0) {
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300156 // What we want to do here is to take name of current module,
157 // chop <level> trailing components, and concatenate with passed-in
158 // module name, thus resolving relative import name into absolue.
159 // This even appears to be correct per
160 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
161 // "Relative imports use a module's __name__ attribute to determine that
162 // module's position in the package hierarchy."
163 mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
164 assert(this_name_q != MP_OBJ_NULL);
165#if DEBUG_PRINT
Damien Georgeeaaebf32014-09-23 10:59:05 +0100166 DEBUG_printf("Current module: ");
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300167 mp_obj_print(this_name_q, PRINT_REPR);
Damien Georgeeaaebf32014-09-23 10:59:05 +0100168 DEBUG_printf("\n");
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300169#endif
170
Damien Georged182b982014-08-30 14:19:41 +0100171 mp_uint_t this_name_l;
172 const char *this_name = mp_obj_str_get_data(this_name_q, &this_name_l);
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300173
174 uint dots_seen = 0;
175 const char *p = this_name + this_name_l - 1;
176 while (p > this_name) {
177 if (*p == '.') {
178 dots_seen++;
179 if (--level == 0) {
180 break;
181 }
182 }
183 p--;
184 }
185
186 if (dots_seen == 0 && level == 1) {
187 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
188 // "If the module's name does not contain any package information
189 // (e.g. it is set to '__main__') then relative imports are
190 // resolved as if the module were a top level module, regardless
191 // of where the module is actually located on the file system."
192 // Supposedly this if catches this condition and resolve it properly
193 // TODO: But nobody knows for sure. This condition happens when
194 // package's __init__.py does something like "import .submod". So,
195 // maybe we should check for package here? But quote above doesn't
196 // talk about packages, it talks about dot-less module names.
197 p = this_name + this_name_l;
198 } else if (level != 0) {
199 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import"));
200 }
201
202 uint new_mod_l = (mod_len == 0 ? p - this_name : p - this_name + 1 + mod_len);
203 char *new_mod = alloca(new_mod_l);
204 memcpy(new_mod, this_name, p - this_name);
205 if (mod_len != 0) {
206 new_mod[p - this_name] = '.';
207 memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len);
208 }
209
210 qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l);
211 DEBUG_printf("Resolved relative name: %s\n", qstr_str(new_mod_q));
212 module_name = MP_OBJ_NEW_QSTR(new_mod_q);
213 mod_str = new_mod;
214 mod_len = new_mod_l;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200215 }
216
Damien Georgee09ffa12014-02-05 23:57:48 +0000217 // check if module already exists
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300218 mp_obj_t module_obj = mp_module_get(mp_obj_str_get_qstr(module_name));
Damien Georgee09ffa12014-02-05 23:57:48 +0000219 if (module_obj != MP_OBJ_NULL) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300220 DEBUG_printf("Module already loaded\n");
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200221 // If it's not a package, return module right away
222 char *p = strchr(mod_str, '.');
223 if (p == NULL) {
224 return module_obj;
225 }
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200226 // If fromlist is not empty, return leaf module
227 if (fromtuple != mp_const_none) {
228 return module_obj;
229 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200230 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200231 qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
Damien Georgecaac5422014-03-25 14:18:18 +0000232 return mp_module_get(pkg_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000233 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300234 DEBUG_printf("Module not yet loaded\n");
Damien Georgee09ffa12014-02-05 23:57:48 +0000235
Damien Georgee09ffa12014-02-05 23:57:48 +0000236 uint last = 0;
Damien George58ebde42014-05-21 20:32:59 +0100237 VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX)
Damien Georgee09ffa12014-02-05 23:57:48 +0000238 module_obj = MP_OBJ_NULL;
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200239 mp_obj_t top_module_obj = MP_OBJ_NULL;
240 mp_obj_t outer_module_obj = MP_OBJ_NULL;
Damien Georgee09ffa12014-02-05 23:57:48 +0000241 uint i;
242 for (i = 1; i <= mod_len; i++) {
243 if (i == mod_len || mod_str[i] == '.') {
244 // create a qstr for the module name up to this depth
245 qstr mod_name = qstr_from_strn(mod_str, i);
Paul Sokolovskye0813292014-04-11 23:08:29 +0300246 DEBUG_printf("Processing module: %s\n", qstr_str(mod_name));
Paul Sokolovskyad6178b2014-05-10 19:00:03 +0300247 DEBUG_printf("Previous path: %s\n", vstr_str(&path));
Damien Georgee09ffa12014-02-05 23:57:48 +0000248
249 // find the file corresponding to the module name
250 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000251 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000252 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000253 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000254 } else {
255 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000256 vstr_add_char(&path, PATH_SEP_CHAR);
257 vstr_add_strn(&path, mod_str + last, i - last);
258 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000259 }
Paul Sokolovskyad6178b2014-05-10 19:00:03 +0300260 DEBUG_printf("Current path: %s\n", vstr_str(&path));
Damien Georgee09ffa12014-02-05 23:57:48 +0000261
Damien Georgee09ffa12014-02-05 23:57:48 +0000262 if (stat == MP_IMPORT_STAT_NO_EXIST) {
Damien Georgec14a8162014-10-12 11:46:04 +0100263 #if MICROPY_MODULE_WEAK_LINKS
264 // check if there is a weak link to this module
265 if (i == mod_len) {
266 mp_map_elem_t *el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_dict_obj.map, MP_OBJ_NEW_QSTR(mod_name), MP_MAP_LOOKUP);
267 if (el == NULL) {
268 goto no_exist;
269 }
270 // found weak linked module
271 module_obj = el->value;
272 } else {
273 no_exist:
274 #else
275 {
276 #endif
277 // couldn't find the file, so fail
278 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "No module named '%s'", qstr_str(mod_name)));
279 }
280 } else {
281 // found the file, so get the module
282 module_obj = mp_module_get(mod_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000283 }
284
Damien Georgee09ffa12014-02-05 23:57:48 +0000285 if (module_obj == MP_OBJ_NULL) {
286 // module not already loaded, so load it!
287
288 module_obj = mp_obj_new_module(mod_name);
289
Paul Sokolovskye5035122014-10-25 21:16:24 +0300290 // if args[3] (fromtuple) has magic value False, set up
291 // this module for command-line "-m" option (set module's
292 // name to __main__ instead of real name).
293 if (i == mod_len && fromtuple == mp_const_false) {
294 mp_obj_module_t *o = module_obj;
295 mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
296 }
297
Damien Georgee09ffa12014-02-05 23:57:48 +0000298 if (stat == MP_IMPORT_STAT_DIR) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300299 DEBUG_printf("%s is dir\n", vstr_str(&path));
Chris Angelicodaf973a2014-06-06 03:51:03 +1000300 // https://docs.python.org/3/reference/import.html
Paul Sokolovskyf9589d22014-05-10 18:46:02 +0300301 // "Specifically, any module that contains a __path__ attribute is considered a package."
Damien George2617eeb2014-05-25 22:27:57 +0100302 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 +0000303 vstr_add_char(&path, PATH_SEP_CHAR);
304 vstr_add_str(&path, "__init__.py");
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200305 if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
Damien George280e7202014-03-15 14:33:09 +0000306 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Paul Sokolovskya5854d22014-04-15 01:23:40 +0300307 printf("Notice: %s is imported as namespace package\n", vstr_str(&path));
308 } else {
309 do_load(module_obj, &path);
Paul Sokolovskyad6178b2014-05-10 19:00:03 +0300310 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgee09ffa12014-02-05 23:57:48 +0000311 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000312 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000313 do_load(module_obj, &path);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200314 // TODO: We cannot just break here, at the very least, we must execute
315 // trailer code below. But otherwise if there're remaining components,
316 // that would be (??) object path within module, not modules path within FS.
317 // break;
Damien Georgee09ffa12014-02-05 23:57:48 +0000318 }
319 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200320 if (outer_module_obj != MP_OBJ_NULL) {
321 qstr s = qstr_from_strn(mod_str + last, i - last);
Damien Georged17926d2014-03-30 13:35:08 +0100322 mp_store_attr(outer_module_obj, s, module_obj);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200323 }
324 outer_module_obj = module_obj;
325 if (top_module_obj == MP_OBJ_NULL) {
326 top_module_obj = module_obj;
327 }
328 last = i + 1;
Damien Georgee09ffa12014-02-05 23:57:48 +0000329 }
330 }
331
332 if (i < mod_len) {
333 // we loaded a package, now need to load objects from within that package
334 // TODO
335 assert(0);
336 }
337
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200338 // If fromlist is not empty, return leaf module
339 if (fromtuple != mp_const_none) {
340 return module_obj;
341 }
342 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200343 return top_module_obj;
Damien George66028ab2014-01-03 14:03:48 +0000344}
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200345MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);