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"); |
| 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 George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 152 | VSTR_FIXED(path, MICROPY_PATH_MAX) |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 153 | 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 George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 162 | if (vstr_len(&path) == 0) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 163 | // first module in the dotted-name; search for a directory or file |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 164 | stat = find_file(mod_str, i, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 165 | } else { |
| 166 | // latter module in the dotted-name; append to path |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 167 | 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 170 | } |
| 171 | last = i + 1; |
| 172 | |
| 173 | // fail if we couldn't find the file |
| 174 | if (stat == MP_IMPORT_STAT_NO_EXIST) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 175 | 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] | 176 | } |
| 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 George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 185 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 186 | vstr_add_str(&path, "__init__.py"); |
Paul Sokolovsky | d378357 | 2014-02-16 01:51:46 +0200 | [diff] [blame^] | 187 | 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 George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 192 | } |
Paul Sokolovsky | d378357 | 2014-02-16 01:51:46 +0200 | [diff] [blame^] | 193 | do_load(module_obj, &path); |
| 194 | vstr_cut_tail(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 195 | } else { // MP_IMPORT_STAT_FILE |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 196 | do_load(module_obj, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 197 | 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 George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 209 | return module_obj; |
| 210 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 211 | |
Paul Sokolovsky | 1d938c9 | 2014-02-04 00:46:17 +0200 | [diff] [blame] | 212 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__); |