blob: 3fb658c27255734b2385287a3874693a1615e17f [file] [log] [blame]
Damien George66028ab2014-01-03 14:03:48 +00001#include <stdint.h>
2#include <stdlib.h>
3#include <stdio.h>
Damien George66028ab2014-01-03 14:03:48 +00004#include <string.h>
5#include <assert.h>
6
7#include "nlr.h"
8#include "misc.h"
9#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien George66028ab2014-01-03 14:03:48 +000011#include "lexer.h"
12#include "lexerunix.h"
13#include "parse.h"
Damien George66028ab2014-01-03 14:03:48 +000014#include "obj.h"
Damien Georgecaac5422014-03-25 14:18:18 +000015#include "objmodule.h"
Damien Georgec5966122014-02-15 16:10:44 +000016#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "compile.h"
Damien George66028ab2014-01-03 14:03:48 +000018#include "runtime0.h"
19#include "runtime.h"
Damien George66028ab2014-01-03 14:03:48 +000020#include "builtin.h"
21
Paul Sokolovskye0813292014-04-11 23:08:29 +030022#if 0 // print debugging info
23#define DEBUG_PRINT (1)
24#define DEBUG_printf DEBUG_printf
25#else // don't print debugging info
26#define DEBUG_printf(...) (void)0
27#endif
28
Damien Georgee09ffa12014-02-05 23:57:48 +000029#define PATH_SEP_CHAR '/'
30
Damien Georged17926d2014-03-30 13:35:08 +010031mp_obj_t mp_sys_path;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020032
Damien Georgee09ffa12014-02-05 23:57:48 +000033mp_import_stat_t stat_dir_or_file(vstr_t *path) {
34 //printf("stat %s\n", vstr_str(path));
35 mp_import_stat_t stat = mp_import_stat(vstr_str(path));
36 if (stat == MP_IMPORT_STAT_DIR) {
37 return stat;
Damien George66028ab2014-01-03 14:03:48 +000038 }
Damien Georgee09ffa12014-02-05 23:57:48 +000039 vstr_add_str(path, ".py");
40 stat = mp_import_stat(vstr_str(path));
41 if (stat == MP_IMPORT_STAT_FILE) {
42 return stat;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020043 }
Damien Georgee09ffa12014-02-05 23:57:48 +000044 return MP_IMPORT_STAT_NO_EXIST;
45}
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020046
Damien Georgee09ffa12014-02-05 23:57:48 +000047mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
48 // extract the list of paths
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020049 uint path_num = 0;
50 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +010051 if (mp_sys_path != MP_OBJ_NULL) {
52 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020053 }
54
55 if (path_num == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010056 // mp_sys_path is empty, so just use the given file name
Damien Georgee09ffa12014-02-05 23:57:48 +000057 vstr_add_strn(dest, file_str, file_len);
58 return stat_dir_or_file(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020059 } else {
Damien Georgee09ffa12014-02-05 23:57:48 +000060 // go through each path looking for a directory or file
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020061 for (int i = 0; i < path_num; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +000062 vstr_reset(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020063 uint p_len;
Damien George698ec212014-02-08 18:17:23 +000064 const char *p = mp_obj_str_get_data(path_items[i], &p_len);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020065 if (p_len > 0) {
Damien George698ec212014-02-08 18:17:23 +000066 vstr_add_strn(dest, p, p_len);
Damien Georgee09ffa12014-02-05 23:57:48 +000067 vstr_add_char(dest, PATH_SEP_CHAR);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020068 }
Damien Georgee09ffa12014-02-05 23:57:48 +000069 vstr_add_strn(dest, file_str, file_len);
70 mp_import_stat_t stat = stat_dir_or_file(dest);
71 if (stat != MP_IMPORT_STAT_NO_EXIST) {
72 return stat;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020073 }
74 }
Damien Georgee09ffa12014-02-05 23:57:48 +000075
76 // could not find a directory or file
77 return MP_IMPORT_STAT_NO_EXIST;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020078 }
Damien Georgee09ffa12014-02-05 23:57:48 +000079}
80
81void do_load(mp_obj_t module_obj, vstr_t *file) {
82 // create the lexer
83 mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file));
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020084
Damien George66028ab2014-01-03 14:03:48 +000085 if (lex == NULL) {
Damien Georgee09ffa12014-02-05 23:57:48 +000086 // we verified the file exists using stat, but lexer could still fail
Andrew Schellerf78cfaf2014-04-09 19:56:38 +010087 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 +000088 }
89
Damien Georgee09ffa12014-02-05 23:57:48 +000090 qstr source_name = mp_lexer_source_name(lex);
Damien George66028ab2014-01-03 14:03:48 +000091
92 // save the old context
Damien George7efc5b32014-04-05 22:36:42 +010093 mp_obj_dict_t *old_locals = mp_locals_get();
94 mp_obj_dict_t *old_globals = mp_globals_get();
Damien George66028ab2014-01-03 14:03:48 +000095
96 // set the new context
Damien George7efc5b32014-04-05 22:36:42 +010097 mp_locals_set(mp_obj_module_get_globals(module_obj));
98 mp_globals_set(mp_obj_module_get_globals(module_obj));
Damien George66028ab2014-01-03 14:03:48 +000099
100 // parse the imported script
Damien Georgec5966122014-02-15 16:10:44 +0000101 mp_parse_error_kind_t parse_error_kind;
102 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_error_kind);
Damien George66028ab2014-01-03 14:03:48 +0000103
104 if (pn == MP_PARSE_NODE_NULL) {
Damien George9528cd62014-01-15 21:23:31 +0000105 // parse error; clean up and raise exception
Damien George4b01de42014-04-13 11:56:02 +0100106 mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind);
107 mp_lexer_free(lex);
Damien Georged17926d2014-03-30 13:35:08 +0100108 mp_locals_set(old_locals);
109 mp_globals_set(old_globals);
Damien George4b01de42014-04-13 11:56:02 +0100110 nlr_raise(exc);
Damien George66028ab2014-01-03 14:03:48 +0000111 }
112
Damien George4b01de42014-04-13 11:56:02 +0100113 mp_lexer_free(lex);
114
Damien George9528cd62014-01-15 21:23:31 +0000115 // compile the imported script
Damien George65cad122014-04-06 11:48:15 +0100116 mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000117 mp_parse_node_free(pn);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000118
119 if (module_fun == mp_const_none) {
Damien George66028ab2014-01-03 14:03:48 +0000120 // TODO handle compile error correctly
Damien Georged17926d2014-03-30 13:35:08 +0100121 mp_locals_set(old_locals);
122 mp_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000123 return;
Damien George66028ab2014-01-03 14:03:48 +0000124 }
125
126 // complied successfully, execute it
Damien George66028ab2014-01-03 14:03:48 +0000127 nlr_buf_t nlr;
128 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100129 mp_call_function_0(module_fun);
Damien George66028ab2014-01-03 14:03:48 +0000130 nlr_pop();
131 } else {
132 // exception; restore context and re-raise same exception
Damien Georged17926d2014-03-30 13:35:08 +0100133 mp_locals_set(old_locals);
134 mp_globals_set(old_globals);
Damien Georgeea13f402014-04-05 18:32:08 +0100135 nlr_raise(nlr.ret_val);
Damien George66028ab2014-01-03 14:03:48 +0000136 }
Damien Georged17926d2014-03-30 13:35:08 +0100137 mp_locals_set(old_locals);
138 mp_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000139}
140
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300141// TODO: Move to objdict?
142STATIC inline mp_obj_t mp_obj_dict_get(mp_obj_t dict_in, mp_obj_t key) {
143 mp_obj_dict_t *dict = dict_in;
144 mp_map_elem_t *elem = mp_map_lookup(&dict->map, key, MP_MAP_LOOKUP);
145 if (elem == NULL) {
146 return elem;
147 }
148 return elem->value;
149}
150
Damien George24ff0632014-03-24 10:47:13 +0000151mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300152#if DEBUG_PRINT
153 printf("__import__:\n");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200154 for (int i = 0; i < n_args; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000155 printf(" ");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200156 mp_obj_print(args[i], PRINT_REPR);
Damien Georgee09ffa12014-02-05 23:57:48 +0000157 printf("\n");
158 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300159#endif
Damien Georgee09ffa12014-02-05 23:57:48 +0000160
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300161 mp_obj_t module_name = args[0];
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200162 mp_obj_t fromtuple = mp_const_none;
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200163 int level = 0;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200164 if (n_args >= 4) {
165 fromtuple = args[3];
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200166 if (n_args >= 5) {
167 level = MP_OBJ_SMALL_INT_VALUE(args[4]);
168 }
169 }
170
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300171 uint mod_len;
172 const char *mod_str = (const char*)mp_obj_str_get_data(module_name, &mod_len);
173
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200174 if (level != 0) {
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300175 // What we want to do here is to take name of current module,
176 // chop <level> trailing components, and concatenate with passed-in
177 // module name, thus resolving relative import name into absolue.
178 // This even appears to be correct per
179 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
180 // "Relative imports use a module's __name__ attribute to determine that
181 // module's position in the package hierarchy."
182 mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
183 assert(this_name_q != MP_OBJ_NULL);
184#if DEBUG_PRINT
185 printf("Current module: ");
186 mp_obj_print(this_name_q, PRINT_REPR);
187 printf("\n");
188#endif
189
190 uint this_name_l;
191 const char *this_name = (const char*)mp_obj_str_get_data(this_name_q, &this_name_l);
192
193 uint dots_seen = 0;
194 const char *p = this_name + this_name_l - 1;
195 while (p > this_name) {
196 if (*p == '.') {
197 dots_seen++;
198 if (--level == 0) {
199 break;
200 }
201 }
202 p--;
203 }
204
205 if (dots_seen == 0 && level == 1) {
206 // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
207 // "If the module's name does not contain any package information
208 // (e.g. it is set to '__main__') then relative imports are
209 // resolved as if the module were a top level module, regardless
210 // of where the module is actually located on the file system."
211 // Supposedly this if catches this condition and resolve it properly
212 // TODO: But nobody knows for sure. This condition happens when
213 // package's __init__.py does something like "import .submod". So,
214 // maybe we should check for package here? But quote above doesn't
215 // talk about packages, it talks about dot-less module names.
216 p = this_name + this_name_l;
217 } else if (level != 0) {
218 nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import"));
219 }
220
221 uint new_mod_l = (mod_len == 0 ? p - this_name : p - this_name + 1 + mod_len);
222 char *new_mod = alloca(new_mod_l);
223 memcpy(new_mod, this_name, p - this_name);
224 if (mod_len != 0) {
225 new_mod[p - this_name] = '.';
226 memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len);
227 }
228
229 qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l);
230 DEBUG_printf("Resolved relative name: %s\n", qstr_str(new_mod_q));
231 module_name = MP_OBJ_NEW_QSTR(new_mod_q);
232 mod_str = new_mod;
233 mod_len = new_mod_l;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200234 }
235
Damien Georgee09ffa12014-02-05 23:57:48 +0000236 // check if module already exists
Paul Sokolovskya5afc902014-04-12 17:46:54 +0300237 mp_obj_t module_obj = mp_module_get(mp_obj_str_get_qstr(module_name));
Damien Georgee09ffa12014-02-05 23:57:48 +0000238 if (module_obj != MP_OBJ_NULL) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300239 DEBUG_printf("Module already loaded\n");
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200240 // If it's not a package, return module right away
241 char *p = strchr(mod_str, '.');
242 if (p == NULL) {
243 return module_obj;
244 }
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200245 // If fromlist is not empty, return leaf module
246 if (fromtuple != mp_const_none) {
247 return module_obj;
248 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200249 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200250 qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
Damien Georgecaac5422014-03-25 14:18:18 +0000251 return mp_module_get(pkg_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000252 }
Paul Sokolovskye0813292014-04-11 23:08:29 +0300253 DEBUG_printf("Module not yet loaded\n");
Damien Georgee09ffa12014-02-05 23:57:48 +0000254
Damien Georgee09ffa12014-02-05 23:57:48 +0000255 uint last = 0;
Damien George354d15a2014-02-06 21:11:19 +0000256 VSTR_FIXED(path, MICROPY_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));
Damien Georgee09ffa12014-02-05 23:57:48 +0000266
267 // find the file corresponding to the module name
268 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000269 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000270 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000271 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000272 } else {
273 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000274 vstr_add_char(&path, PATH_SEP_CHAR);
275 vstr_add_strn(&path, mod_str + last, i - last);
276 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000277 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000278
279 // fail if we couldn't find the file
280 if (stat == MP_IMPORT_STAT_NO_EXIST) {
Andrew Schellerf78cfaf2014-04-09 19:56:38 +0100281 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "No module named '%s'", qstr_str(mod_name)));
Damien Georgee09ffa12014-02-05 23:57:48 +0000282 }
283
Damien Georgecaac5422014-03-25 14:18:18 +0000284 module_obj = mp_module_get(mod_name);
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
290 if (stat == MP_IMPORT_STAT_DIR) {
Paul Sokolovskye0813292014-04-11 23:08:29 +0300291 DEBUG_printf("%s is dir\n", vstr_str(&path));
Paul Sokolovsky2ff3d9d2014-04-12 02:44:47 +0300292 mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str((byte*)vstr_str(&path), vstr_len(&path), false));
Damien George354d15a2014-02-06 21:11:19 +0000293 vstr_add_char(&path, PATH_SEP_CHAR);
294 vstr_add_str(&path, "__init__.py");
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200295 if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
Damien George280e7202014-03-15 14:33:09 +0000296 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgeea13f402014-04-05 18:32:08 +0100297 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200298 "Per PEP-420 a dir without __init__.py (%s) is a namespace package; "
299 "namespace packages are not supported", vstr_str(&path)));
Damien Georgee09ffa12014-02-05 23:57:48 +0000300 }
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200301 do_load(module_obj, &path);
Damien George280e7202014-03-15 14:33:09 +0000302 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Paul Sokolovsky13d52df2014-04-11 23:25:35 +0300303 // https://docs.python.org/3.3/reference/import.html
304 // "Specifically, any module that contains a __path__ attribute is considered a package."
Damien Georgee09ffa12014-02-05 23:57:48 +0000305 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000306 do_load(module_obj, &path);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200307 // TODO: We cannot just break here, at the very least, we must execute
308 // trailer code below. But otherwise if there're remaining components,
309 // that would be (??) object path within module, not modules path within FS.
310 // break;
Damien Georgee09ffa12014-02-05 23:57:48 +0000311 }
312 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200313 if (outer_module_obj != MP_OBJ_NULL) {
314 qstr s = qstr_from_strn(mod_str + last, i - last);
Damien Georged17926d2014-03-30 13:35:08 +0100315 mp_store_attr(outer_module_obj, s, module_obj);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200316 }
317 outer_module_obj = module_obj;
318 if (top_module_obj == MP_OBJ_NULL) {
319 top_module_obj = module_obj;
320 }
321 last = i + 1;
Damien Georgee09ffa12014-02-05 23:57:48 +0000322 }
323 }
324
325 if (i < mod_len) {
326 // we loaded a package, now need to load objects from within that package
327 // TODO
328 assert(0);
329 }
330
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200331 // If fromlist is not empty, return leaf module
332 if (fromtuple != mp_const_none) {
333 return module_obj;
334 }
335 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200336 return top_module_obj;
Damien George66028ab2014-01-03 14:03:48 +0000337}
Damien Georgee09ffa12014-02-05 23:57:48 +0000338
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200339MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);