blob: ae03f8c5236f12ee7740eb29d71bffcda8d79f17 [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");
135 for (int i = 0; i < n; i++) {
136 printf(" ");
137 mp_obj_print(args[i]);
138 printf("\n");
139 }
140 */
141
142 // check if module already exists
143 mp_obj_t module_obj = mp_obj_module_get(mp_obj_str_get_qstr(args[0]));
144 if (module_obj != MP_OBJ_NULL) {
145 return module_obj;
146 }
147
148 uint mod_len;
149 const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len);
150
151 uint last = 0;
Damien George354d15a2014-02-06 21:11:19 +0000152 VSTR_FIXED(path, MICROPY_PATH_MAX)
Damien Georgee09ffa12014-02-05 23:57:48 +0000153 module_obj = MP_OBJ_NULL;
154 uint i;
155 for (i = 1; i <= mod_len; i++) {
156 if (i == mod_len || mod_str[i] == '.') {
157 // create a qstr for the module name up to this depth
158 qstr mod_name = qstr_from_strn(mod_str, i);
159
160 // find the file corresponding to the module name
161 mp_import_stat_t stat;
Damien George354d15a2014-02-06 21:11:19 +0000162 if (vstr_len(&path) == 0) {
Damien Georgee09ffa12014-02-05 23:57:48 +0000163 // first module in the dotted-name; search for a directory or file
Damien George354d15a2014-02-06 21:11:19 +0000164 stat = find_file(mod_str, i, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000165 } else {
166 // latter module in the dotted-name; append to path
Damien George354d15a2014-02-06 21:11:19 +0000167 vstr_add_char(&path, PATH_SEP_CHAR);
168 vstr_add_strn(&path, mod_str + last, i - last);
169 stat = stat_dir_or_file(&path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000170 }
171 last = i + 1;
172
173 // fail if we couldn't find the file
174 if (stat == MP_IMPORT_STAT_NO_EXIST) {
Damien Georgec5966122014-02-15 16:10:44 +0000175 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 +0000176 }
177
178 module_obj = mp_obj_module_get(mod_name);
179 if (module_obj == MP_OBJ_NULL) {
180 // module not already loaded, so load it!
181
182 module_obj = mp_obj_new_module(mod_name);
183
184 if (stat == MP_IMPORT_STAT_DIR) {
Damien George354d15a2014-02-06 21:11:19 +0000185 vstr_add_char(&path, PATH_SEP_CHAR);
186 vstr_add_str(&path, "__init__.py");
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200187 if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
188 vstr_cut_tail(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
189 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
190 "Per PEP-420 a dir without __init__.py (%s) is a namespace package; "
191 "namespace packages are not supported", vstr_str(&path)));
Damien Georgee09ffa12014-02-05 23:57:48 +0000192 }
Paul Sokolovskyd3783572014-02-16 01:51:46 +0200193 do_load(module_obj, &path);
194 vstr_cut_tail(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
Damien Georgee09ffa12014-02-05 23:57:48 +0000195 } else { // MP_IMPORT_STAT_FILE
Damien George354d15a2014-02-06 21:11:19 +0000196 do_load(module_obj, &path);
Damien Georgee09ffa12014-02-05 23:57:48 +0000197 break;
198 }
199 }
200 }
201 }
202
203 if (i < mod_len) {
204 // we loaded a package, now need to load objects from within that package
205 // TODO
206 assert(0);
207 }
208
Damien George66028ab2014-01-03 14:03:48 +0000209 return module_obj;
210}
Damien Georgee09ffa12014-02-05 23:57:48 +0000211
Paul Sokolovsky1d938c92014-02-04 00:46:17 +0200212MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);