Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 1 | #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 George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 11 | #include "qstr.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 12 | #include "lexer.h" |
| 13 | #include "lexerunix.h" |
| 14 | #include "parse.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 15 | #include "obj.h" |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 16 | #include "parsehelper.h" |
Damien George | 1fb0317 | 2014-01-03 14:22:03 +0000 | [diff] [blame] | 17 | #include "compile.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 18 | #include "runtime0.h" |
| 19 | #include "runtime.h" |
| 20 | #include "map.h" |
| 21 | #include "builtin.h" |
| 22 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 23 | #define PATH_SEP_CHAR '/' |
| 24 | |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 25 | mp_obj_t sys_path; |
| 26 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 27 | mp_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 George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 32 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 33 | vstr_add_str(path, ".py"); |
| 34 | stat = mp_import_stat(vstr_str(path)); |
| 35 | if (stat == MP_IMPORT_STAT_FILE) { |
| 36 | return stat; |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 37 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 38 | return MP_IMPORT_STAT_NO_EXIST; |
| 39 | } |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 40 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 41 | mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) { |
| 42 | // extract the list of paths |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 43 | 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 50 | // 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 Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 53 | } else { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 54 | // go through each path looking for a directory or file |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 55 | for (int i = 0; i < path_num; i++) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 56 | vstr_reset(dest); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 57 | uint p_len; |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 58 | const char *p = mp_obj_str_get_data(path_items[i], &p_len); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 59 | if (p_len > 0) { |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 60 | vstr_add_strn(dest, p, p_len); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 61 | vstr_add_char(dest, PATH_SEP_CHAR); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 62 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 63 | 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 Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 67 | } |
| 68 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 69 | |
| 70 | // could not find a directory or file |
| 71 | return MP_IMPORT_STAT_NO_EXIST; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 72 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void 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 Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 78 | |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 79 | if (lex == NULL) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 80 | // we verified the file exists using stat, but lexer could still fail |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 81 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "ImportError: No module named '%s'", vstr_str(file))); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 84 | qstr source_name = mp_lexer_source_name(lex); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 85 | |
| 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 George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 95 | 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 George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 97 | mp_lexer_free(lex); |
| 98 | |
| 99 | if (pn == MP_PARSE_NODE_NULL) { |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 100 | // parse error; clean up and raise exception |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 101 | rt_locals_set(old_locals); |
| 102 | rt_globals_set(old_globals); |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 103 | nlr_jump(mp_parse_make_exception(parse_error_kind)); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 106 | // compile the imported script |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 107 | mp_obj_t module_fun = mp_compile(pn, source_name, false); |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame] | 108 | mp_parse_node_free(pn); |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 109 | |
| 110 | if (module_fun == mp_const_none) { |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 111 | // TODO handle compile error correctly |
| 112 | rt_locals_set(old_locals); |
| 113 | rt_globals_set(old_globals); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 114 | return; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // complied successfully, execute it |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 118 | 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { |
| 133 | /* |
| 134 | printf("import:\n"); |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 135 | for (int i = 0; i < n_args; i++) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 136 | printf(" "); |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 137 | mp_obj_print(args[i], PRINT_REPR); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 138 | printf("\n"); |
| 139 | } |
| 140 | */ |
| 141 | |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 142 | mp_obj_t fromtuple = mp_const_none; |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame^] | 143 | int level = 0; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 144 | if (n_args >= 4) { |
| 145 | fromtuple = args[3]; |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame^] | 146 | 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 Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 156 | uint mod_len; |
| 157 | const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len); |
| 158 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 159 | // 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 Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 162 | // 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 Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 167 | // If fromlist is not empty, return leaf module |
| 168 | if (fromtuple != mp_const_none) { |
| 169 | return module_obj; |
| 170 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 171 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 172 | qstr pkg_name = qstr_from_strn(mod_str, p - mod_str); |
| 173 | return mp_obj_module_get(pkg_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 176 | uint last = 0; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 177 | VSTR_FIXED(path, MICROPY_PATH_MAX) |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 178 | module_obj = MP_OBJ_NULL; |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 179 | mp_obj_t top_module_obj = MP_OBJ_NULL; |
| 180 | mp_obj_t outer_module_obj = MP_OBJ_NULL; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 181 | 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 George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 189 | if (vstr_len(&path) == 0) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 190 | // first module in the dotted-name; search for a directory or file |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 191 | stat = find_file(mod_str, i, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 192 | } else { |
| 193 | // latter module in the dotted-name; append to path |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 194 | 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 197 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 198 | |
| 199 | // fail if we couldn't find the file |
| 200 | if (stat == MP_IMPORT_STAT_NO_EXIST) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 201 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "ImportError: No module named '%s'", qstr_str(mod_name))); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 202 | } |
| 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 George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 211 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 212 | vstr_add_str(&path, "__init__.py"); |
Paul Sokolovsky | d378357 | 2014-02-16 01:51:46 +0200 | [diff] [blame] | 213 | 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 218 | } |
Paul Sokolovsky | d378357 | 2014-02-16 01:51:46 +0200 | [diff] [blame] | 219 | do_load(module_obj, &path); |
| 220 | vstr_cut_tail(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 221 | } else { // MP_IMPORT_STAT_FILE |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 222 | do_load(module_obj, &path); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 223 | // 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 229 | 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 238 | } |
| 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 Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 247 | // 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 Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 252 | return top_module_obj; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 253 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 254 | |
Paul Sokolovsky | 1d938c9 | 2014-02-04 00:46:17 +0200 | [diff] [blame] | 255 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__); |