blob: e64ca8237406d1c8b09f64016f35385054c0111b [file] [log] [blame]
Damien George66028ab2014-01-03 14:03:48 +00001#include <stdint.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdarg.h>
5#include <string.h>
6#include <assert.h>
7
8#include "nlr.h"
9#include "misc.h"
10#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000011#include "qstr.h"
Damien George66028ab2014-01-03 14:03:48 +000012#include "lexer.h"
13#include "lexerunix.h"
14#include "parse.h"
Damien George66028ab2014-01-03 14:03:48 +000015#include "obj.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"
20#include "map.h"
21#include "builtin.h"
22
Damien Georgee09ffa12014-02-05 23:57:48 +000023#define PATH_SEP_CHAR '/'
24
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020025mp_obj_t sys_path;
26
Damien Georgee09ffa12014-02-05 23:57:48 +000027mp_import_stat_t stat_dir_or_file(vstr_t *path) {
28 //printf("stat %s\n", vstr_str(path));
29 mp_import_stat_t stat = mp_import_stat(vstr_str(path));
30 if (stat == MP_IMPORT_STAT_DIR) {
31 return stat;
Damien George66028ab2014-01-03 14:03:48 +000032 }
Damien Georgee09ffa12014-02-05 23:57:48 +000033 vstr_add_str(path, ".py");
34 stat = mp_import_stat(vstr_str(path));
35 if (stat == MP_IMPORT_STAT_FILE) {
36 return stat;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020037 }
Damien Georgee09ffa12014-02-05 23:57:48 +000038 return MP_IMPORT_STAT_NO_EXIST;
39}
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020040
Damien Georgee09ffa12014-02-05 23:57:48 +000041mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
42 // extract the list of paths
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020043 uint path_num = 0;
44 mp_obj_t *path_items;
45 if (sys_path != MP_OBJ_NULL) {
46 mp_obj_list_get(sys_path, &path_num, &path_items);
47 }
48
49 if (path_num == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +000050 // sys_path is empty, so just use the given file name
51 vstr_add_strn(dest, file_str, file_len);
52 return stat_dir_or_file(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020053 } else {
Damien Georgee09ffa12014-02-05 23:57:48 +000054 // go through each path looking for a directory or file
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020055 for (int i = 0; i < path_num; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +000056 vstr_reset(dest);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020057 uint p_len;
Damien George698ec212014-02-08 18:17:23 +000058 const char *p = mp_obj_str_get_data(path_items[i], &p_len);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020059 if (p_len > 0) {
Damien George698ec212014-02-08 18:17:23 +000060 vstr_add_strn(dest, p, p_len);
Damien Georgee09ffa12014-02-05 23:57:48 +000061 vstr_add_char(dest, PATH_SEP_CHAR);
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020062 }
Damien Georgee09ffa12014-02-05 23:57:48 +000063 vstr_add_strn(dest, file_str, file_len);
64 mp_import_stat_t stat = stat_dir_or_file(dest);
65 if (stat != MP_IMPORT_STAT_NO_EXIST) {
66 return stat;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020067 }
68 }
Damien Georgee09ffa12014-02-05 23:57:48 +000069
70 // could not find a directory or file
71 return MP_IMPORT_STAT_NO_EXIST;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020072 }
Damien Georgee09ffa12014-02-05 23:57:48 +000073}
74
75void do_load(mp_obj_t module_obj, vstr_t *file) {
76 // create the lexer
77 mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file));
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020078
Damien George66028ab2014-01-03 14:03:48 +000079 if (lex == NULL) {
Damien Georgee09ffa12014-02-05 23:57:48 +000080 // we verified the file exists using stat, but lexer could still fail
Damien Georgec5966122014-02-15 16:10:44 +000081 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "ImportError: No module named '%s'", vstr_str(file)));
Damien George66028ab2014-01-03 14:03:48 +000082 }
83
Damien Georgee09ffa12014-02-05 23:57:48 +000084 qstr source_name = mp_lexer_source_name(lex);
Damien George66028ab2014-01-03 14:03:48 +000085
86 // save the old context
87 mp_map_t *old_locals = rt_locals_get();
88 mp_map_t *old_globals = rt_globals_get();
89
90 // set the new context
91 rt_locals_set(mp_obj_module_get_globals(module_obj));
92 rt_globals_set(mp_obj_module_get_globals(module_obj));
93
94 // parse the imported script
Damien Georgec5966122014-02-15 16:10:44 +000095 mp_parse_error_kind_t parse_error_kind;
96 mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_error_kind);
Damien George66028ab2014-01-03 14:03:48 +000097 mp_lexer_free(lex);
98
99 if (pn == MP_PARSE_NODE_NULL) {
Damien George9528cd62014-01-15 21:23:31 +0000100 // parse error; clean up and raise exception
Damien George66028ab2014-01-03 14:03:48 +0000101 rt_locals_set(old_locals);
102 rt_globals_set(old_globals);
Damien Georgec5966122014-02-15 16:10:44 +0000103 nlr_jump(mp_parse_make_exception(parse_error_kind));
Damien George66028ab2014-01-03 14:03:48 +0000104 }
105
Damien George9528cd62014-01-15 21:23:31 +0000106 // compile the imported script
Damien George08335002014-01-18 23:24:36 +0000107 mp_obj_t module_fun = mp_compile(pn, source_name, false);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000108 mp_parse_node_free(pn);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000109
110 if (module_fun == mp_const_none) {
Damien George66028ab2014-01-03 14:03:48 +0000111 // TODO handle compile error correctly
112 rt_locals_set(old_locals);
113 rt_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000114 return;
Damien George66028ab2014-01-03 14:03:48 +0000115 }
116
117 // complied successfully, execute it
Damien George66028ab2014-01-03 14:03:48 +0000118 nlr_buf_t nlr;
119 if (nlr_push(&nlr) == 0) {
120 rt_call_function_0(module_fun);
121 nlr_pop();
122 } else {
123 // exception; restore context and re-raise same exception
124 rt_locals_set(old_locals);
125 rt_globals_set(old_globals);
126 nlr_jump(nlr.ret_val);
127 }
128 rt_locals_set(old_locals);
129 rt_globals_set(old_globals);
Damien Georgee09ffa12014-02-05 23:57:48 +0000130}
131
132mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
133 /*
134 printf("import:\n");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200135 for (int i = 0; i < n_args; i++) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000136 printf(" ");
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200137 mp_obj_print(args[i], PRINT_REPR);
Damien Georgee09ffa12014-02-05 23:57:48 +0000138 printf("\n");
139 }
140 */
141
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200142 mp_obj_t fromtuple = mp_const_none;
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200143 int level = 0;
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200144 if (n_args >= 4) {
145 fromtuple = args[3];
Paul Sokolovskyfeacaa12014-02-21 01:15:20 +0200146 if (n_args >= 5) {
147 level = MP_OBJ_SMALL_INT_VALUE(args[4]);
148 }
149 }
150
151 if (level != 0) {
152 nlr_jump(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
153 "Relative import is not implemented"));
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200154 }
155
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200156 uint mod_len;
157 const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len);
158
Damien Georgee09ffa12014-02-05 23:57:48 +0000159 // check if module already exists
160 mp_obj_t module_obj = mp_obj_module_get(mp_obj_str_get_qstr(args[0]));
161 if (module_obj != MP_OBJ_NULL) {
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200162 // If it's not a package, return module right away
163 char *p = strchr(mod_str, '.');
164 if (p == NULL) {
165 return module_obj;
166 }
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200167 // If fromlist is not empty, return leaf module
168 if (fromtuple != mp_const_none) {
169 return module_obj;
170 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200171 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200172 qstr pkg_name = qstr_from_strn(mod_str, p - mod_str);
173 return mp_obj_module_get(pkg_name);
Damien Georgee09ffa12014-02-05 23:57:48 +0000174 }
175
Damien Georgee09ffa12014-02-05 23:57:48 +0000176 uint last = 0;
Damien George354d15a2014-02-06 21:11:19 +0000177 VSTR_FIXED(path, MICROPY_PATH_MAX)
Damien Georgee09ffa12014-02-05 23:57:48 +0000178 module_obj = MP_OBJ_NULL;
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200179 mp_obj_t top_module_obj = MP_OBJ_NULL;
180 mp_obj_t outer_module_obj = MP_OBJ_NULL;
Damien Georgee09ffa12014-02-05 23:57:48 +0000181 uint i;
182 for (i = 1; i <= mod_len; i++) {
183 if (i == mod_len || mod_str[i] == '.') {
184 // create a qstr for the module name up to this depth
185 qstr mod_name = qstr_from_strn(mod_str, i);
186
187 // find the file corresponding to the module name
188 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000189 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000190 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000191 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000192 } else {
193 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000194 vstr_add_char(&path, PATH_SEP_CHAR);
195 vstr_add_strn(&path, mod_str + last, i - last);
196 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000197 }
Damien Georgee09ffa12014-02-05 23:57:48 +0000198
199 // fail if we couldn't find the file
200 if (stat == MP_IMPORT_STAT_NO_EXIST) {
Damien Georgec5966122014-02-15 16:10:44 +0000201 nlr_jump(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 +0000202 }
203
204 module_obj = mp_obj_module_get(mod_name);
205 if (module_obj == MP_OBJ_NULL) {
206 // module not already loaded, so load it!
207
208 module_obj = mp_obj_new_module(mod_name);
209
210 if (stat == MP_IMPORT_STAT_DIR) {
Damien George354d15a2014-02-06 21:11:19 +0000211 vstr_add_char(&path, PATH_SEP_CHAR);
212 vstr_add_str(&path, "__init__.py");
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200213 if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
214 vstr_cut_tail(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
215 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
216 "Per PEP-420 a dir without __init__.py (%s) is a namespace package; "
217 "namespace packages are not supported", vstr_str(&path)));
Damien Georgee09ffa12014-02-05 23:57:48 +0000218 }
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200219 do_load(module_obj, &path);
220 vstr_cut_tail(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgee09ffa12014-02-05 23:57:48 +0000221 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000222 do_load(module_obj, &path);
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200223 // TODO: We cannot just break here, at the very least, we must execute
224 // trailer code below. But otherwise if there're remaining components,
225 // that would be (??) object path within module, not modules path within FS.
226 // break;
Damien Georgee09ffa12014-02-05 23:57:48 +0000227 }
228 }
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200229 if (outer_module_obj != MP_OBJ_NULL) {
230 qstr s = qstr_from_strn(mod_str + last, i - last);
231 rt_store_attr(outer_module_obj, s, module_obj);
232 }
233 outer_module_obj = module_obj;
234 if (top_module_obj == MP_OBJ_NULL) {
235 top_module_obj = module_obj;
236 }
237 last = i + 1;
Damien Georgee09ffa12014-02-05 23:57:48 +0000238 }
239 }
240
241 if (i < mod_len) {
242 // we loaded a package, now need to load objects from within that package
243 // TODO
244 assert(0);
245 }
246
Paul Sokolovskyfb7f9432014-02-20 00:29:54 +0200247 // If fromlist is not empty, return leaf module
248 if (fromtuple != mp_const_none) {
249 return module_obj;
250 }
251 // Otherwise, we need to return top-level package
Paul Sokolovsky91ba7a52014-02-16 02:53:44 +0200252 return top_module_obj;
Damien George66028ab2014-01-03 14:03:48 +0000253}
Damien Georgee09ffa12014-02-05 23:57:48 +0000254
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200255MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);