blob: a3ab3c9ed7fa8295d09bf7eb040509c269a40b73 [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
Damien Georgee09ffa12014-02-05 23:57:48 +000022#define PATH_SEP_CHAR '/'
23
Damien Georged17926d2014-03-30 13:35:08 +010024mp_obj_t mp_sys_path;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020025
Damien Georgee09ffa12014-02-05 23:57:48 +000026mp_import_stat_t stat_dir_or_file(vstr_t *path) {
27 //printf("stat %s\n", vstr_str(path));
28 mp_import_stat_t stat = mp_import_stat(vstr_str(path));
29 if (stat == MP_IMPORT_STAT_DIR) {
30 return stat;
Damien George66028ab2014-01-03 14:03:48 +000031 }
Damien Georgee09ffa12014-02-05 23:57:48 +000032 vstr_add_str(path, ".py");
33 stat = mp_import_stat(vstr_str(path));
34 if (stat == MP_IMPORT_STAT_FILE) {
35 return stat;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020036 }
Damien Georgee09ffa12014-02-05 23:57:48 +000037 return MP_IMPORT_STAT_NO_EXIST;
38}
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020039
Damien Georgee09ffa12014-02-05 23:57:48 +000040mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
41 // extract the list of paths
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020042 uint path_num = 0;
43 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +010044 if (mp_sys_path != MP_OBJ_NULL) {
45 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020046 }
47
48 if (path_num == 0) {
Damien Georged17926d2014-03-30 13:35:08 +010049 // mp_sys_path is empty, so just use the given file name
Damien Georgee09ffa12014-02-05 23:57:48 +000050 vstr_add_strn(dest, file_str, file_len);
51 return stat_dir_or_file(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020052 } else {
Damien Georgee09ffa12014-02-05 23:57:48 +000053 // go through each path looking for a directory or file
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020054 for (int i = 0; i < path_num; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +000055 vstr_reset(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020056 uint p_len;
Damien George698ec212014-02-08 18:17:23 +000057 const char *p = mp_obj_str_get_data(path_items[i], &p_len);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020058 if (p_len > 0) {
Damien George698ec212014-02-08 18:17:23 +000059 vstr_add_strn(dest, p, p_len);
Damien Georgee09ffa12014-02-05 23:57:48 +000060 vstr_add_char(dest, PATH_SEP_CHAR);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020061 }
Damien Georgee09ffa12014-02-05 23:57:48 +000062 vstr_add_strn(dest, file_str, file_len);
63 mp_import_stat_t stat = stat_dir_or_file(dest);
64 if (stat != MP_IMPORT_STAT_NO_EXIST) {
65 return stat;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020066 }
67 }
Damien Georgee09ffa12014-02-05 23:57:48 +000068
69 // could not find a directory or file
70 return MP_IMPORT_STAT_NO_EXIST;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020071 }
Damien Georgee09ffa12014-02-05 23:57:48 +000072}
73
74void do_load(mp_obj_t module_obj, vstr_t *file) {
75 // create the lexer
76 mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file));
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020077
Damien George66028ab2014-01-03 14:03:48 +000078 if (lex == NULL) {
Damien Georgee09ffa12014-02-05 23:57:48 +000079 // we verified the file exists using stat, but lexer could still fail
Damien Georgeea13f402014-04-05 18:32:08 +010080 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "ImportError: No module named '%s'", vstr_str(file)));
Damien George66028ab2014-01-03 14:03:48 +000081 }
82
Damien Georgee09ffa12014-02-05 23:57:48 +000083 qstr source_name = mp_lexer_source_name(lex);
Damien George66028ab2014-01-03 14:03:48 +000084
85 // save the old context
Damien George7efc5b32014-04-05 22:36:42 +010086 mp_obj_dict_t *old_locals = mp_locals_get();
87 mp_obj_dict_t *old_globals = mp_globals_get();
Damien George66028ab2014-01-03 14:03:48 +000088
89 // set the new context
Damien George7efc5b32014-04-05 22:36:42 +010090 mp_locals_set(mp_obj_module_get_globals(module_obj));
91 mp_globals_set(mp_obj_module_get_globals(module_obj));
Damien George66028ab2014-01-03 14:03:48 +000092
93 // parse the imported script
Damien Georgec5966122014-02-15 16:10:44 +000094 mp_parse_error_kind_t parse_error_kind;
95 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_error_kind);
Damien George66028ab2014-01-03 14:03:48 +000096 mp_lexer_free(lex);
97
98 if (pn == MP_PARSE_NODE_NULL) {
Damien George9528cd62014-01-15 21:23:31 +000099 // parse error; clean up and raise exception
Damien Georged17926d2014-03-30 13:35:08 +0100100 mp_locals_set(old_locals);
101 mp_globals_set(old_globals);
Damien Georgeea13f402014-04-05 18:32:08 +0100102 nlr_raise(mp_parse_make_exception(parse_error_kind));
Damien George66028ab2014-01-03 14:03:48 +0000103 }
104
Damien George9528cd62014-01-15 21:23:31 +0000105 // compile the imported script
Damien George65cad122014-04-06 11:48:15 +0100106 mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000107 mp_parse_node_free(pn);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000108
109 if (module_fun == mp_const_none) {
Damien George66028ab2014-01-03 14:03:48 +0000110 // TODO handle compile error correctly
Damien Georged17926d2014-03-30 13:35:08 +0100111 mp_locals_set(old_locals);
112 mp_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000113 return;
Damien George66028ab2014-01-03 14:03:48 +0000114 }
115
116 // complied successfully, execute it
Damien George66028ab2014-01-03 14:03:48 +0000117 nlr_buf_t nlr;
118 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100119 mp_call_function_0(module_fun);
Damien George66028ab2014-01-03 14:03:48 +0000120 nlr_pop();
121 } else {
122 // exception; restore context and re-raise same exception
Damien Georged17926d2014-03-30 13:35:08 +0100123 mp_locals_set(old_locals);
124 mp_globals_set(old_globals);
Damien Georgeea13f402014-04-05 18:32:08 +0100125 nlr_raise(nlr.ret_val);
Damien George66028ab2014-01-03 14:03:48 +0000126 }
Damien Georged17926d2014-03-30 13:35:08 +0100127 mp_locals_set(old_locals);
128 mp_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000129}
130
Damien George24ff0632014-03-24 10:47:13 +0000131mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000132 /*
133 printf("import:\n");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200134 for (int i = 0; i < n_args; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000135 printf(" ");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200136 mp_obj_print(args[i], PRINT_REPR);
Damien Georgee09ffa12014-02-05 23:57:48 +0000137 printf("\n");
138 }
139 */
140
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200141 mp_obj_t fromtuple = mp_const_none;
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200142 int level = 0;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200143 if (n_args >= 4) {
144 fromtuple = args[3];
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200145 if (n_args >= 5) {
146 level = MP_OBJ_SMALL_INT_VALUE(args[4]);
147 }
148 }
149
150 if (level != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100151 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200152 "Relative import is not implemented"));
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200153 }
154
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200155 uint mod_len;
156 const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len);
157
Damien Georgee09ffa12014-02-05 23:57:48 +0000158 // check if module already exists
Damien Georgecaac5422014-03-25 14:18:18 +0000159 mp_obj_t module_obj = mp_module_get(mp_obj_str_get_qstr(args[0]));
Damien Georgee09ffa12014-02-05 23:57:48 +0000160 if (module_obj != MP_OBJ_NULL) {
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200161 // If it's not a package, return module right away
162 char *p = strchr(mod_str, '.');
163 if (p == NULL) {
164 return module_obj;
165 }
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200166 // If fromlist is not empty, return leaf module
167 if (fromtuple != mp_const_none) {
168 return module_obj;
169 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200170 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200171 qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
Damien Georgecaac5422014-03-25 14:18:18 +0000172 return mp_module_get(pkg_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000173 }
174
Damien Georgee09ffa12014-02-05 23:57:48 +0000175 uint last = 0;
Damien George354d15a2014-02-06 21:11:19 +0000176 VSTR_FIXED(path, MICROPY_PATH_MAX)
Damien Georgee09ffa12014-02-05 23:57:48 +0000177 module_obj = MP_OBJ_NULL;
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200178 mp_obj_t top_module_obj = MP_OBJ_NULL;
179 mp_obj_t outer_module_obj = MP_OBJ_NULL;
Damien Georgee09ffa12014-02-05 23:57:48 +0000180 uint i;
181 for (i = 1; i <= mod_len; i++) {
182 if (i == mod_len || mod_str[i] == '.') {
183 // create a qstr for the module name up to this depth
184 qstr mod_name = qstr_from_strn(mod_str, i);
185
186 // find the file corresponding to the module name
187 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000188 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000189 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000190 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000191 } else {
192 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000193 vstr_add_char(&path, PATH_SEP_CHAR);
194 vstr_add_strn(&path, mod_str + last, i - last);
195 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000196 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000197
198 // fail if we couldn't find the file
199 if (stat == MP_IMPORT_STAT_NO_EXIST) {
Damien Georgeea13f402014-04-05 18:32:08 +0100200 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "ImportError: No module named '%s'", qstr_str(mod_name)));
Damien Georgee09ffa12014-02-05 23:57:48 +0000201 }
202
Damien Georgecaac5422014-03-25 14:18:18 +0000203 module_obj = mp_module_get(mod_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000204 if (module_obj == MP_OBJ_NULL) {
205 // module not already loaded, so load it!
206
207 module_obj = mp_obj_new_module(mod_name);
208
209 if (stat == MP_IMPORT_STAT_DIR) {
Damien George354d15a2014-02-06 21:11:19 +0000210 vstr_add_char(&path, PATH_SEP_CHAR);
211 vstr_add_str(&path, "__init__.py");
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200212 if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
Damien George280e7202014-03-15 14:33:09 +0000213 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgeea13f402014-04-05 18:32:08 +0100214 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200215 "Per PEP-420 a dir without __init__.py (%s) is a namespace package; "
216 "namespace packages are not supported", vstr_str(&path)));
Damien Georgee09ffa12014-02-05 23:57:48 +0000217 }
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200218 do_load(module_obj, &path);
Damien George280e7202014-03-15 14:33:09 +0000219 vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgee09ffa12014-02-05 23:57:48 +0000220 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000221 do_load(module_obj, &path);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200222 // TODO: We cannot just break here, at the very least, we must execute
223 // trailer code below. But otherwise if there're remaining components,
224 // that would be (??) object path within module, not modules path within FS.
225 // break;
Damien Georgee09ffa12014-02-05 23:57:48 +0000226 }
227 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200228 if (outer_module_obj != MP_OBJ_NULL) {
229 qstr s = qstr_from_strn(mod_str + last, i - last);
Damien Georged17926d2014-03-30 13:35:08 +0100230 mp_store_attr(outer_module_obj, s, module_obj);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200231 }
232 outer_module_obj = module_obj;
233 if (top_module_obj == MP_OBJ_NULL) {
234 top_module_obj = module_obj;
235 }
236 last = i + 1;
Damien Georgee09ffa12014-02-05 23:57:48 +0000237 }
238 }
239
240 if (i < mod_len) {
241 // we loaded a package, now need to load objects from within that package
242 // TODO
243 assert(0);
244 }
245
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200246 // If fromlist is not empty, return leaf module
247 if (fromtuple != mp_const_none) {
248 return module_obj;
249 }
250 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200251 return top_module_obj;
Damien George66028ab2014-01-03 14:03:48 +0000252}
Damien Georgee09ffa12014-02-05 23:57:48 +0000253
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200254MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);