Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 27 | #include <stdint.h> |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <assert.h> |
stijn | 98e2ee0 | 2014-05-03 10:14:16 +0200 | [diff] [blame] | 31 | #include <alloca.h> |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 32 | |
Paul Sokolovsky | f54bcbf | 2014-05-02 17:47:01 +0300 | [diff] [blame] | 33 | #include "mpconfig.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 34 | #include "nlr.h" |
| 35 | #include "misc.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 36 | #include "qstr.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 37 | #include "lexer.h" |
| 38 | #include "lexerunix.h" |
| 39 | #include "parse.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 40 | #include "obj.h" |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 41 | #include "objmodule.h" |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 42 | #include "parsehelper.h" |
Damien George | 1fb0317 | 2014-01-03 14:22:03 +0000 | [diff] [blame] | 43 | #include "compile.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 44 | #include "runtime0.h" |
| 45 | #include "runtime.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 46 | #include "builtin.h" |
| 47 | |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 48 | #if 0 // print debugging info |
| 49 | #define DEBUG_PRINT (1) |
| 50 | #define DEBUG_printf DEBUG_printf |
| 51 | #else // don't print debugging info |
| 52 | #define DEBUG_printf(...) (void)0 |
| 53 | #endif |
| 54 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 55 | #define PATH_SEP_CHAR '/' |
| 56 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 57 | mp_import_stat_t stat_dir_or_file(vstr_t *path) { |
| 58 | //printf("stat %s\n", vstr_str(path)); |
| 59 | mp_import_stat_t stat = mp_import_stat(vstr_str(path)); |
| 60 | if (stat == MP_IMPORT_STAT_DIR) { |
| 61 | return stat; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 62 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 63 | vstr_add_str(path, ".py"); |
| 64 | stat = mp_import_stat(vstr_str(path)); |
| 65 | if (stat == MP_IMPORT_STAT_FILE) { |
| 66 | return stat; |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 67 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 68 | return MP_IMPORT_STAT_NO_EXIST; |
| 69 | } |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 70 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 71 | mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) { |
| 72 | // extract the list of paths |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 73 | uint path_num = 0; |
| 74 | mp_obj_t *path_items; |
Damien George | 49f20b8 | 2014-04-13 13:05:16 +0100 | [diff] [blame] | 75 | #if MICROPY_ENABLE_MOD_SYS |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 76 | mp_obj_list_get(mp_sys_path, &path_num, &path_items); |
Damien George | 49f20b8 | 2014-04-13 13:05:16 +0100 | [diff] [blame] | 77 | #endif |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 78 | |
| 79 | if (path_num == 0) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 80 | // mp_sys_path is empty, so just use the given file name |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 81 | vstr_add_strn(dest, file_str, file_len); |
| 82 | return stat_dir_or_file(dest); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 83 | } else { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 84 | // go through each path looking for a directory or file |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 85 | for (int i = 0; i < path_num; i++) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 86 | vstr_reset(dest); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 87 | uint p_len; |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 88 | 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] | 89 | if (p_len > 0) { |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 90 | vstr_add_strn(dest, p, p_len); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 91 | vstr_add_char(dest, PATH_SEP_CHAR); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 92 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 93 | vstr_add_strn(dest, file_str, file_len); |
| 94 | mp_import_stat_t stat = stat_dir_or_file(dest); |
| 95 | if (stat != MP_IMPORT_STAT_NO_EXIST) { |
| 96 | return stat; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 97 | } |
| 98 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 99 | |
| 100 | // could not find a directory or file |
| 101 | return MP_IMPORT_STAT_NO_EXIST; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 102 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void do_load(mp_obj_t module_obj, vstr_t *file) { |
| 106 | // create the lexer |
| 107 | mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file)); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 108 | |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 109 | if (lex == NULL) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 110 | // we verified the file exists using stat, but lexer could still fail |
Andrew Scheller | f78cfaf | 2014-04-09 19:56:38 +0100 | [diff] [blame] | 111 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "No module named '%s'", vstr_str(file))); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 114 | qstr source_name = mp_lexer_source_name(lex); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 115 | |
| 116 | // save the old context |
Damien George | 7efc5b3 | 2014-04-05 22:36:42 +0100 | [diff] [blame] | 117 | mp_obj_dict_t *old_locals = mp_locals_get(); |
| 118 | mp_obj_dict_t *old_globals = mp_globals_get(); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 119 | |
| 120 | // set the new context |
Damien George | 7efc5b3 | 2014-04-05 22:36:42 +0100 | [diff] [blame] | 121 | mp_locals_set(mp_obj_module_get_globals(module_obj)); |
| 122 | mp_globals_set(mp_obj_module_get_globals(module_obj)); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 123 | |
| 124 | // parse the imported script |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 125 | mp_parse_error_kind_t parse_error_kind; |
| 126 | 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] | 127 | |
| 128 | if (pn == MP_PARSE_NODE_NULL) { |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 129 | // parse error; clean up and raise exception |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 130 | mp_obj_t exc = mp_parse_make_exception(lex, parse_error_kind); |
| 131 | mp_lexer_free(lex); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 132 | mp_locals_set(old_locals); |
| 133 | mp_globals_set(old_globals); |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 134 | nlr_raise(exc); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 137 | mp_lexer_free(lex); |
| 138 | |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 139 | // compile the imported script |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 140 | mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false); |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame] | 141 | mp_parse_node_free(pn); |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 142 | |
| 143 | if (module_fun == mp_const_none) { |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 144 | // TODO handle compile error correctly |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 145 | mp_locals_set(old_locals); |
| 146 | mp_globals_set(old_globals); |
Paul Sokolovsky | 20e9ed3 | 2014-04-22 02:53:20 +0300 | [diff] [blame] | 147 | nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "Syntax error in imported module")); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // complied successfully, execute it |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 151 | nlr_buf_t nlr; |
| 152 | if (nlr_push(&nlr) == 0) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 153 | mp_call_function_0(module_fun); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 154 | nlr_pop(); |
| 155 | } else { |
| 156 | // exception; restore context and re-raise same exception |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 157 | mp_locals_set(old_locals); |
| 158 | mp_globals_set(old_globals); |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 159 | nlr_raise(nlr.ret_val); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 160 | } |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 161 | mp_locals_set(old_locals); |
| 162 | mp_globals_set(old_globals); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 165 | // TODO: Move to objdict? |
| 166 | STATIC inline mp_obj_t mp_obj_dict_get(mp_obj_t dict_in, mp_obj_t key) { |
| 167 | mp_obj_dict_t *dict = dict_in; |
| 168 | mp_map_elem_t *elem = mp_map_lookup(&dict->map, key, MP_MAP_LOOKUP); |
| 169 | if (elem == NULL) { |
| 170 | return elem; |
| 171 | } |
| 172 | return elem->value; |
| 173 | } |
| 174 | |
Damien George | 24ff063 | 2014-03-24 10:47:13 +0000 | [diff] [blame] | 175 | mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 176 | #if DEBUG_PRINT |
| 177 | printf("__import__:\n"); |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 178 | for (int i = 0; i < n_args; i++) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 179 | printf(" "); |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 180 | mp_obj_print(args[i], PRINT_REPR); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 181 | printf("\n"); |
| 182 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 183 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 184 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 185 | mp_obj_t module_name = args[0]; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 186 | mp_obj_t fromtuple = mp_const_none; |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 187 | int level = 0; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 188 | if (n_args >= 4) { |
| 189 | fromtuple = args[3]; |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 190 | if (n_args >= 5) { |
| 191 | level = MP_OBJ_SMALL_INT_VALUE(args[4]); |
| 192 | } |
| 193 | } |
| 194 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 195 | uint mod_len; |
| 196 | const char *mod_str = (const char*)mp_obj_str_get_data(module_name, &mod_len); |
| 197 | |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 198 | if (level != 0) { |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 199 | // What we want to do here is to take name of current module, |
| 200 | // chop <level> trailing components, and concatenate with passed-in |
| 201 | // module name, thus resolving relative import name into absolue. |
| 202 | // This even appears to be correct per |
| 203 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 204 | // "Relative imports use a module's __name__ attribute to determine that |
| 205 | // module's position in the package hierarchy." |
| 206 | mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__)); |
| 207 | assert(this_name_q != MP_OBJ_NULL); |
| 208 | #if DEBUG_PRINT |
| 209 | printf("Current module: "); |
| 210 | mp_obj_print(this_name_q, PRINT_REPR); |
| 211 | printf("\n"); |
| 212 | #endif |
| 213 | |
| 214 | uint this_name_l; |
| 215 | const char *this_name = (const char*)mp_obj_str_get_data(this_name_q, &this_name_l); |
| 216 | |
| 217 | uint dots_seen = 0; |
| 218 | const char *p = this_name + this_name_l - 1; |
| 219 | while (p > this_name) { |
| 220 | if (*p == '.') { |
| 221 | dots_seen++; |
| 222 | if (--level == 0) { |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | p--; |
| 227 | } |
| 228 | |
| 229 | if (dots_seen == 0 && level == 1) { |
| 230 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 231 | // "If the module's name does not contain any package information |
| 232 | // (e.g. it is set to '__main__') then relative imports are |
| 233 | // resolved as if the module were a top level module, regardless |
| 234 | // of where the module is actually located on the file system." |
| 235 | // Supposedly this if catches this condition and resolve it properly |
| 236 | // TODO: But nobody knows for sure. This condition happens when |
| 237 | // package's __init__.py does something like "import .submod". So, |
| 238 | // maybe we should check for package here? But quote above doesn't |
| 239 | // talk about packages, it talks about dot-less module names. |
| 240 | p = this_name + this_name_l; |
| 241 | } else if (level != 0) { |
| 242 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import")); |
| 243 | } |
| 244 | |
| 245 | uint new_mod_l = (mod_len == 0 ? p - this_name : p - this_name + 1 + mod_len); |
| 246 | char *new_mod = alloca(new_mod_l); |
| 247 | memcpy(new_mod, this_name, p - this_name); |
| 248 | if (mod_len != 0) { |
| 249 | new_mod[p - this_name] = '.'; |
| 250 | memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len); |
| 251 | } |
| 252 | |
| 253 | qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l); |
| 254 | DEBUG_printf("Resolved relative name: %s\n", qstr_str(new_mod_q)); |
| 255 | module_name = MP_OBJ_NEW_QSTR(new_mod_q); |
| 256 | mod_str = new_mod; |
| 257 | mod_len = new_mod_l; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 260 | // check if module already exists |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 261 | mp_obj_t module_obj = mp_module_get(mp_obj_str_get_qstr(module_name)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 262 | if (module_obj != MP_OBJ_NULL) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 263 | DEBUG_printf("Module already loaded\n"); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 264 | // If it's not a package, return module right away |
| 265 | char *p = strchr(mod_str, '.'); |
| 266 | if (p == NULL) { |
| 267 | return module_obj; |
| 268 | } |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 269 | // If fromlist is not empty, return leaf module |
| 270 | if (fromtuple != mp_const_none) { |
| 271 | return module_obj; |
| 272 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 273 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 274 | qstr pkg_name = qstr_from_strn(mod_str, p - mod_str); |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 275 | return mp_module_get(pkg_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 276 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 277 | DEBUG_printf("Module not yet loaded\n"); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 278 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 279 | uint last = 0; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 280 | VSTR_FIXED(path, MICROPY_PATH_MAX) |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 281 | module_obj = MP_OBJ_NULL; |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 282 | mp_obj_t top_module_obj = MP_OBJ_NULL; |
| 283 | mp_obj_t outer_module_obj = MP_OBJ_NULL; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 284 | uint i; |
| 285 | for (i = 1; i <= mod_len; i++) { |
| 286 | if (i == mod_len || mod_str[i] == '.') { |
| 287 | // create a qstr for the module name up to this depth |
| 288 | qstr mod_name = qstr_from_strn(mod_str, i); |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 289 | DEBUG_printf("Processing module: %s\n", qstr_str(mod_name)); |
Paul Sokolovsky | ad6178b | 2014-05-10 19:00:03 +0300 | [diff] [blame^] | 290 | DEBUG_printf("Previous path: %s\n", vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 291 | |
| 292 | // find the file corresponding to the module name |
| 293 | mp_import_stat_t stat; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 294 | if (vstr_len(&path) == 0) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 295 | // first module in the dotted-name; search for a directory or file |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 296 | stat = find_file(mod_str, i, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 297 | } else { |
| 298 | // latter module in the dotted-name; append to path |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 299 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 300 | vstr_add_strn(&path, mod_str + last, i - last); |
| 301 | stat = stat_dir_or_file(&path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 302 | } |
Paul Sokolovsky | ad6178b | 2014-05-10 19:00:03 +0300 | [diff] [blame^] | 303 | DEBUG_printf("Current path: %s\n", vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 304 | |
| 305 | // fail if we couldn't find the file |
| 306 | if (stat == MP_IMPORT_STAT_NO_EXIST) { |
Andrew Scheller | f78cfaf | 2014-04-09 19:56:38 +0100 | [diff] [blame] | 307 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "No module named '%s'", qstr_str(mod_name))); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 310 | module_obj = mp_module_get(mod_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 311 | if (module_obj == MP_OBJ_NULL) { |
| 312 | // module not already loaded, so load it! |
| 313 | |
| 314 | module_obj = mp_obj_new_module(mod_name); |
| 315 | |
| 316 | if (stat == MP_IMPORT_STAT_DIR) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 317 | DEBUG_printf("%s is dir\n", vstr_str(&path)); |
Paul Sokolovsky | f9589d2 | 2014-05-10 18:46:02 +0300 | [diff] [blame] | 318 | // https://docs.python.org/3.3/reference/import.html |
| 319 | // "Specifically, any module that contains a __path__ attribute is considered a package." |
Paul Sokolovsky | 2ff3d9d | 2014-04-12 02:44:47 +0300 | [diff] [blame] | 320 | mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str((byte*)vstr_str(&path), vstr_len(&path), false)); |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 321 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 322 | vstr_add_str(&path, "__init__.py"); |
Paul Sokolovsky | d378357 | 2014-02-16 01:51:46 +0200 | [diff] [blame] | 323 | if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) { |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 324 | vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py |
Paul Sokolovsky | a5854d2 | 2014-04-15 01:23:40 +0300 | [diff] [blame] | 325 | printf("Notice: %s is imported as namespace package\n", vstr_str(&path)); |
| 326 | } else { |
| 327 | do_load(module_obj, &path); |
Paul Sokolovsky | ad6178b | 2014-05-10 19:00:03 +0300 | [diff] [blame^] | 328 | vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 329 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 330 | } else { // MP_IMPORT_STAT_FILE |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 331 | do_load(module_obj, &path); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 332 | // TODO: We cannot just break here, at the very least, we must execute |
| 333 | // trailer code below. But otherwise if there're remaining components, |
| 334 | // that would be (??) object path within module, not modules path within FS. |
| 335 | // break; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 338 | if (outer_module_obj != MP_OBJ_NULL) { |
| 339 | qstr s = qstr_from_strn(mod_str + last, i - last); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 340 | mp_store_attr(outer_module_obj, s, module_obj); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 341 | } |
| 342 | outer_module_obj = module_obj; |
| 343 | if (top_module_obj == MP_OBJ_NULL) { |
| 344 | top_module_obj = module_obj; |
| 345 | } |
| 346 | last = i + 1; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | if (i < mod_len) { |
| 351 | // we loaded a package, now need to load objects from within that package |
| 352 | // TODO |
| 353 | assert(0); |
| 354 | } |
| 355 | |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 356 | // If fromlist is not empty, return leaf module |
| 357 | if (fromtuple != mp_const_none) { |
| 358 | return module_obj; |
| 359 | } |
| 360 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 361 | return top_module_obj; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 362 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 363 | |
Paul Sokolovsky | 1d938c9 | 2014-02-04 00:46:17 +0200 | [diff] [blame] | 364 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__); |