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 |
Paul Sokolovsky | da9f092 | 2014-05-13 08:44:45 +0300 | [diff] [blame] | 7 | * Copyright (c) 2014 Paul Sokolovsky |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
| 27 | |
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> |
| 31 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/nlr.h" |
| 33 | #include "py/compile.h" |
| 34 | #include "py/objmodule.h" |
| 35 | #include "py/runtime.h" |
| 36 | #include "py/builtin.h" |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 37 | #include "py/frozenmod.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 38 | |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 39 | #if 0 // print debugging info |
| 40 | #define DEBUG_PRINT (1) |
| 41 | #define DEBUG_printf DEBUG_printf |
| 42 | #else // don't print debugging info |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 43 | #define DEBUG_PRINT (0) |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 44 | #define DEBUG_printf(...) (void)0 |
| 45 | #endif |
| 46 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 47 | #define PATH_SEP_CHAR '/' |
| 48 | |
Damien George | 78d702c | 2014-12-09 16:19:48 +0000 | [diff] [blame] | 49 | #if MICROPY_MODULE_WEAK_LINKS |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame^] | 50 | STATIC const mp_rom_map_elem_t mp_builtin_module_weak_links_table[] = { |
Damien George | 78d702c | 2014-12-09 16:19:48 +0000 | [diff] [blame] | 51 | MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS |
| 52 | }; |
| 53 | |
| 54 | STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_weak_links_map, mp_builtin_module_weak_links_table); |
| 55 | #endif |
| 56 | |
Paul Sokolovsky | e5a3759 | 2014-10-25 21:04:13 +0300 | [diff] [blame] | 57 | bool mp_obj_is_package(mp_obj_t module) { |
| 58 | mp_obj_t dest[2]; |
| 59 | mp_load_method_maybe(module, MP_QSTR___path__, dest); |
| 60 | return dest[0] != MP_OBJ_NULL; |
| 61 | } |
| 62 | |
Damien George | 8a0801a | 2014-06-11 19:55:46 +0100 | [diff] [blame] | 63 | STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) { |
Damien George | 827b0f7 | 2015-01-29 13:57:23 +0000 | [diff] [blame] | 64 | mp_import_stat_t stat = mp_import_stat(vstr_null_terminated_str(path)); |
Paul Sokolovsky | 078172d | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 65 | DEBUG_printf("stat %s: %d\n", vstr_str(path), stat); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 66 | if (stat == MP_IMPORT_STAT_DIR) { |
| 67 | return stat; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 68 | } |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 69 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 70 | vstr_add_str(path, ".py"); |
Damien George | 827b0f7 | 2015-01-29 13:57:23 +0000 | [diff] [blame] | 71 | stat = mp_import_stat(vstr_null_terminated_str(path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 72 | if (stat == MP_IMPORT_STAT_FILE) { |
| 73 | return stat; |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 74 | } |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 75 | |
| 76 | #if MICROPY_PERSISTENT_CODE_LOAD |
| 77 | vstr_ins_byte(path, path->len - 2, 'm'); |
| 78 | stat = mp_import_stat(vstr_null_terminated_str(path)); |
| 79 | if (stat == MP_IMPORT_STAT_FILE) { |
| 80 | return stat; |
| 81 | } |
| 82 | #endif |
| 83 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 84 | return MP_IMPORT_STAT_NO_EXIST; |
| 85 | } |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 86 | |
Damien George | 8a0801a | 2014-06-11 19:55:46 +0100 | [diff] [blame] | 87 | STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) { |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 88 | #if MICROPY_PY_SYS |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 89 | // extract the list of paths |
| 90 | mp_uint_t path_num; |
| 91 | mp_obj_t *path_items; |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 92 | mp_obj_list_get(mp_sys_path, &path_num, &path_items); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 93 | |
| 94 | if (path_num == 0) { |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 95 | #endif |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 96 | // mp_sys_path is empty, so just use the given file name |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 97 | vstr_add_strn(dest, file_str, file_len); |
| 98 | return stat_dir_or_file(dest); |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 99 | #if MICROPY_PY_SYS |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 100 | } else { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 101 | // go through each path looking for a directory or file |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 102 | for (mp_uint_t i = 0; i < path_num; i++) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 103 | vstr_reset(dest); |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 104 | mp_uint_t p_len; |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 105 | 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] | 106 | if (p_len > 0) { |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 107 | vstr_add_strn(dest, p, p_len); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 108 | vstr_add_char(dest, PATH_SEP_CHAR); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 109 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 110 | vstr_add_strn(dest, file_str, file_len); |
| 111 | mp_import_stat_t stat = stat_dir_or_file(dest); |
| 112 | if (stat != MP_IMPORT_STAT_NO_EXIST) { |
| 113 | return stat; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 114 | } |
| 115 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 116 | |
| 117 | // could not find a directory or file |
| 118 | return MP_IMPORT_STAT_NO_EXIST; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 119 | } |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 120 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 123 | STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char *fname) { |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 124 | |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 125 | if (lex == NULL) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 126 | // we verified the file exists using stat, but lexer could still fail |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 127 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 128 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found")); |
| 129 | } else { |
| 130 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 131 | "no module named '%s'", fname)); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 132 | } |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Paul Sokolovsky | d0f5e61 | 2014-07-25 11:00:15 +0300 | [diff] [blame] | 135 | #if MICROPY_PY___FILE__ |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 136 | qstr source_name = lex->source_name; |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 137 | mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); |
Paul Sokolovsky | d0f5e61 | 2014-07-25 11:00:15 +0300 | [diff] [blame] | 138 | #endif |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 139 | |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 140 | // parse, compile and execute the module in its context |
| 141 | mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj); |
| 142 | mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 145 | #if MICROPY_PERSISTENT_CODE_LOAD |
| 146 | STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) { |
| 147 | #if MICROPY_PY___FILE__ |
| 148 | // TODO |
| 149 | //qstr source_name = lex->source_name; |
| 150 | //mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); |
| 151 | #endif |
| 152 | |
| 153 | // execute the module in its context |
| 154 | mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj); |
| 155 | |
| 156 | // save context |
| 157 | mp_obj_dict_t *volatile old_globals = mp_globals_get(); |
| 158 | mp_obj_dict_t *volatile old_locals = mp_locals_get(); |
| 159 | |
| 160 | // set new context |
| 161 | mp_globals_set(mod_globals); |
| 162 | mp_locals_set(mod_globals); |
| 163 | |
| 164 | nlr_buf_t nlr; |
| 165 | if (nlr_push(&nlr) == 0) { |
| 166 | mp_obj_t module_fun = mp_make_function_from_raw_code(raw_code, MP_OBJ_NULL, MP_OBJ_NULL); |
| 167 | mp_call_function_0(module_fun); |
| 168 | |
| 169 | // finish nlr block, restore context |
| 170 | nlr_pop(); |
| 171 | mp_globals_set(old_globals); |
| 172 | mp_locals_set(old_locals); |
| 173 | } else { |
| 174 | // exception; restore context and re-raise same exception |
| 175 | mp_globals_set(old_globals); |
| 176 | mp_locals_set(old_locals); |
| 177 | nlr_raise(nlr.ret_val); |
| 178 | } |
| 179 | } |
| 180 | #endif |
| 181 | |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 182 | STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { |
| 183 | // create the lexer |
Damien George | 827b0f7 | 2015-01-29 13:57:23 +0000 | [diff] [blame] | 184 | char *file_str = vstr_null_terminated_str(file); |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 185 | #if MICROPY_PERSISTENT_CODE_LOAD |
| 186 | if (file_str[file->len - 3] == 'm') { |
| 187 | mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str); |
| 188 | do_execute_raw_code(module_obj, raw_code); |
| 189 | } else |
| 190 | #endif |
| 191 | { |
| 192 | mp_lexer_t *lex = mp_lexer_new_from_file(file_str); |
| 193 | do_load_from_lexer(module_obj, lex, file_str); |
| 194 | } |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 197 | STATIC void chop_component(const char *start, const char **end) { |
| 198 | const char *p = *end; |
| 199 | while (p > start) { |
| 200 | if (*--p == '.') { |
| 201 | *end = p; |
| 202 | return; |
| 203 | } |
| 204 | } |
| 205 | *end = p; |
| 206 | } |
| 207 | |
Paul Sokolovsky | 8becca7 | 2014-10-25 21:03:20 +0300 | [diff] [blame] | 208 | mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 209 | #if DEBUG_PRINT |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 210 | DEBUG_printf("__import__:\n"); |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 211 | for (mp_uint_t i = 0; i < n_args; i++) { |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 212 | DEBUG_printf(" "); |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 213 | mp_obj_print(args[i], PRINT_REPR); |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 214 | DEBUG_printf("\n"); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 215 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 216 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 217 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 218 | mp_obj_t module_name = args[0]; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 219 | mp_obj_t fromtuple = mp_const_none; |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 220 | mp_int_t level = 0; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 221 | if (n_args >= 4) { |
| 222 | fromtuple = args[3]; |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 223 | if (n_args >= 5) { |
| 224 | level = MP_OBJ_SMALL_INT_VALUE(args[4]); |
| 225 | } |
| 226 | } |
| 227 | |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 228 | mp_uint_t mod_len; |
| 229 | const char *mod_str = mp_obj_str_get_data(module_name, &mod_len); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 230 | |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 231 | if (level != 0) { |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 232 | // What we want to do here is to take name of current module, |
| 233 | // chop <level> trailing components, and concatenate with passed-in |
| 234 | // module name, thus resolving relative import name into absolue. |
| 235 | // This even appears to be correct per |
| 236 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 237 | // "Relative imports use a module's __name__ attribute to determine that |
| 238 | // module's position in the package hierarchy." |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 239 | level--; |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 240 | mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__)); |
| 241 | assert(this_name_q != MP_OBJ_NULL); |
Paul Sokolovsky | 9780e55 | 2015-06-29 00:21:36 +0300 | [diff] [blame] | 242 | #if MICROPY_CPYTHON_COMPAT |
| 243 | if (MP_OBJ_QSTR_VALUE(this_name_q) == MP_QSTR___main__) { |
| 244 | // This is a module run by -m command-line switch, get its real name from backup attribute |
| 245 | this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___main__)); |
| 246 | } |
| 247 | #endif |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 248 | mp_map_t *globals_map = mp_obj_dict_get_map(mp_globals_get()); |
| 249 | mp_map_elem_t *elem = mp_map_lookup(globals_map, MP_OBJ_NEW_QSTR(MP_QSTR___path__), MP_MAP_LOOKUP); |
| 250 | bool is_pkg = (elem != NULL); |
| 251 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 252 | #if DEBUG_PRINT |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 253 | DEBUG_printf("Current module/package: "); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 254 | mp_obj_print(this_name_q, PRINT_REPR); |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 255 | DEBUG_printf(", is_package: %d", is_pkg); |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 256 | DEBUG_printf("\n"); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 257 | #endif |
| 258 | |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 259 | mp_uint_t this_name_l; |
| 260 | const char *this_name = mp_obj_str_get_data(this_name_q, &this_name_l); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 261 | |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 262 | const char *p = this_name + this_name_l; |
| 263 | if (!is_pkg) { |
| 264 | // We have module, but relative imports are anchored at package, so |
| 265 | // go there. |
| 266 | chop_component(this_name, &p); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 267 | } |
| 268 | |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 269 | |
| 270 | uint dots_seen = 0; |
| 271 | while (level--) { |
| 272 | chop_component(this_name, &p); |
| 273 | dots_seen++; |
| 274 | } |
| 275 | |
| 276 | if (dots_seen == 0 && level >= 1) { |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 277 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 278 | // "If the module's name does not contain any package information |
| 279 | // (e.g. it is set to '__main__') then relative imports are |
| 280 | // resolved as if the module were a top level module, regardless |
| 281 | // of where the module is actually located on the file system." |
| 282 | // Supposedly this if catches this condition and resolve it properly |
| 283 | // TODO: But nobody knows for sure. This condition happens when |
| 284 | // package's __init__.py does something like "import .submod". So, |
| 285 | // maybe we should check for package here? But quote above doesn't |
| 286 | // talk about packages, it talks about dot-less module names. |
Paul Sokolovsky | 078172d | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 287 | DEBUG_printf("Warning: no dots in current module name and level>0\n"); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 288 | p = this_name + this_name_l; |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 289 | } else if (level != -1) { |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 290 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import")); |
| 291 | } |
| 292 | |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 293 | uint new_mod_l = (mod_len == 0 ? (size_t)(p - this_name) : (size_t)(p - this_name) + 1 + mod_len); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 294 | char *new_mod = alloca(new_mod_l); |
| 295 | memcpy(new_mod, this_name, p - this_name); |
| 296 | if (mod_len != 0) { |
| 297 | new_mod[p - this_name] = '.'; |
| 298 | memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len); |
| 299 | } |
| 300 | |
| 301 | qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l); |
Paul Sokolovsky | c4045f5 | 2015-06-27 00:34:53 +0300 | [diff] [blame] | 302 | DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q)); |
| 303 | if (new_mod_q == MP_QSTR_) { |
| 304 | // CPython raises SystemError |
| 305 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "cannot perform relative import")); |
| 306 | } |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 307 | module_name = MP_OBJ_NEW_QSTR(new_mod_q); |
| 308 | mod_str = new_mod; |
| 309 | mod_len = new_mod_l; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 312 | // check if module already exists |
Paul Sokolovsky | 8064892 | 2015-01-21 23:14:46 +0200 | [diff] [blame] | 313 | qstr module_name_qstr = mp_obj_str_get_qstr(module_name); |
| 314 | mp_obj_t module_obj = mp_module_get(module_name_qstr); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 315 | if (module_obj != MP_OBJ_NULL) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 316 | DEBUG_printf("Module already loaded\n"); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 317 | // If it's not a package, return module right away |
| 318 | char *p = strchr(mod_str, '.'); |
| 319 | if (p == NULL) { |
| 320 | return module_obj; |
| 321 | } |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 322 | // If fromlist is not empty, return leaf module |
| 323 | if (fromtuple != mp_const_none) { |
| 324 | return module_obj; |
| 325 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 326 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 327 | qstr pkg_name = qstr_from_strn(mod_str, p - mod_str); |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 328 | return mp_module_get(pkg_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 329 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 330 | DEBUG_printf("Module not yet loaded\n"); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 331 | |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 332 | #if MICROPY_MODULE_FROZEN |
| 333 | mp_lexer_t *lex = mp_find_frozen_module(mod_str, mod_len); |
| 334 | if (lex != NULL) { |
Paul Sokolovsky | 8064892 | 2015-01-21 23:14:46 +0200 | [diff] [blame] | 335 | module_obj = mp_obj_new_module(module_name_qstr); |
Paul Sokolovsky | 07408cb | 2015-06-06 00:10:40 +0300 | [diff] [blame] | 336 | // if args[3] (fromtuple) has magic value False, set up |
| 337 | // this module for command-line "-m" option (set module's |
| 338 | // name to __main__ instead of real name). |
| 339 | // TODO: Duplicated below too. |
| 340 | if (fromtuple == mp_const_false) { |
| 341 | mp_obj_module_t *o = module_obj; |
| 342 | mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__)); |
| 343 | } |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 344 | do_load_from_lexer(module_obj, lex, mod_str); |
| 345 | return module_obj; |
| 346 | } |
| 347 | #endif |
| 348 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 349 | uint last = 0; |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 350 | VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX) |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 351 | module_obj = MP_OBJ_NULL; |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 352 | mp_obj_t top_module_obj = MP_OBJ_NULL; |
| 353 | mp_obj_t outer_module_obj = MP_OBJ_NULL; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 354 | uint i; |
| 355 | for (i = 1; i <= mod_len; i++) { |
| 356 | if (i == mod_len || mod_str[i] == '.') { |
| 357 | // create a qstr for the module name up to this depth |
| 358 | qstr mod_name = qstr_from_strn(mod_str, i); |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 359 | DEBUG_printf("Processing module: %s\n", qstr_str(mod_name)); |
Paul Sokolovsky | 078172d | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 360 | DEBUG_printf("Previous path: =%.*s=\n", vstr_len(&path), vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 361 | |
| 362 | // find the file corresponding to the module name |
| 363 | mp_import_stat_t stat; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 364 | if (vstr_len(&path) == 0) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 365 | // first module in the dotted-name; search for a directory or file |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 366 | stat = find_file(mod_str, i, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 367 | } else { |
| 368 | // latter module in the dotted-name; append to path |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 369 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 370 | vstr_add_strn(&path, mod_str + last, i - last); |
| 371 | stat = stat_dir_or_file(&path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 372 | } |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 373 | DEBUG_printf("Current path: %.*s\n", vstr_len(&path), vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 374 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 375 | if (stat == MP_IMPORT_STAT_NO_EXIST) { |
Damien George | c14a816 | 2014-10-12 11:46:04 +0100 | [diff] [blame] | 376 | #if MICROPY_MODULE_WEAK_LINKS |
| 377 | // check if there is a weak link to this module |
| 378 | if (i == mod_len) { |
Damien George | 78d702c | 2014-12-09 16:19:48 +0000 | [diff] [blame] | 379 | mp_map_elem_t *el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_map, MP_OBJ_NEW_QSTR(mod_name), MP_MAP_LOOKUP); |
Damien George | c14a816 | 2014-10-12 11:46:04 +0100 | [diff] [blame] | 380 | if (el == NULL) { |
| 381 | goto no_exist; |
| 382 | } |
| 383 | // found weak linked module |
| 384 | module_obj = el->value; |
| 385 | } else { |
| 386 | no_exist: |
| 387 | #else |
| 388 | { |
| 389 | #endif |
| 390 | // couldn't find the file, so fail |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 391 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 392 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found")); |
| 393 | } else { |
| 394 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 395 | "no module named '%q'", mod_name)); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 396 | } |
Damien George | c14a816 | 2014-10-12 11:46:04 +0100 | [diff] [blame] | 397 | } |
| 398 | } else { |
| 399 | // found the file, so get the module |
| 400 | module_obj = mp_module_get(mod_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 403 | if (module_obj == MP_OBJ_NULL) { |
| 404 | // module not already loaded, so load it! |
| 405 | |
| 406 | module_obj = mp_obj_new_module(mod_name); |
| 407 | |
Paul Sokolovsky | e503512 | 2014-10-25 21:16:24 +0300 | [diff] [blame] | 408 | // if args[3] (fromtuple) has magic value False, set up |
| 409 | // this module for command-line "-m" option (set module's |
| 410 | // name to __main__ instead of real name). |
| 411 | if (i == mod_len && fromtuple == mp_const_false) { |
| 412 | mp_obj_module_t *o = module_obj; |
| 413 | mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__)); |
Paul Sokolovsky | 9780e55 | 2015-06-29 00:21:36 +0300 | [diff] [blame] | 414 | #if MICROPY_CPYTHON_COMPAT |
| 415 | // Store real name in "__main__" attribute. Choosen semi-randonly, to reuse existing qstr's. |
| 416 | mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(mod_name)); |
| 417 | #endif |
Paul Sokolovsky | e503512 | 2014-10-25 21:16:24 +0300 | [diff] [blame] | 418 | } |
| 419 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 420 | if (stat == MP_IMPORT_STAT_DIR) { |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 421 | DEBUG_printf("%.*s is dir\n", vstr_len(&path), vstr_str(&path)); |
Chris Angelico | daf973a | 2014-06-06 03:51:03 +1000 | [diff] [blame] | 422 | // https://docs.python.org/3/reference/import.html |
Paul Sokolovsky | f9589d2 | 2014-05-10 18:46:02 +0300 | [diff] [blame] | 423 | // "Specifically, any module that contains a __path__ attribute is considered a package." |
Damien George | 2617eeb | 2014-05-25 22:27:57 +0100 | [diff] [blame] | 424 | mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false)); |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 425 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 426 | vstr_add_str(&path, "__init__.py"); |
Damien George | 827b0f7 | 2015-01-29 13:57:23 +0000 | [diff] [blame] | 427 | if (mp_import_stat(vstr_null_terminated_str(&path)) != MP_IMPORT_STAT_FILE) { |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 428 | vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py |
Paul Sokolovsky | 8a8c1fc | 2015-01-01 09:29:28 +0200 | [diff] [blame] | 429 | mp_warning("%s is imported as namespace package", vstr_str(&path)); |
Paul Sokolovsky | a5854d2 | 2014-04-15 01:23:40 +0300 | [diff] [blame] | 430 | } else { |
| 431 | do_load(module_obj, &path); |
Paul Sokolovsky | ad6178b | 2014-05-10 19:00:03 +0300 | [diff] [blame] | 432 | 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] | 433 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 434 | } else { // MP_IMPORT_STAT_FILE |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 435 | do_load(module_obj, &path); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 436 | // TODO: We cannot just break here, at the very least, we must execute |
| 437 | // trailer code below. But otherwise if there're remaining components, |
| 438 | // that would be (??) object path within module, not modules path within FS. |
| 439 | // break; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 442 | if (outer_module_obj != MP_OBJ_NULL) { |
| 443 | qstr s = qstr_from_strn(mod_str + last, i - last); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 444 | mp_store_attr(outer_module_obj, s, module_obj); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 445 | } |
| 446 | outer_module_obj = module_obj; |
| 447 | if (top_module_obj == MP_OBJ_NULL) { |
| 448 | top_module_obj = module_obj; |
| 449 | } |
| 450 | last = i + 1; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | if (i < mod_len) { |
| 455 | // we loaded a package, now need to load objects from within that package |
| 456 | // TODO |
| 457 | assert(0); |
| 458 | } |
| 459 | |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 460 | // If fromlist is not empty, return leaf module |
| 461 | if (fromtuple != mp_const_none) { |
| 462 | return module_obj; |
| 463 | } |
| 464 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 465 | return top_module_obj; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 466 | } |
Paul Sokolovsky | 1d938c9 | 2014-02-04 00:46:17 +0200 | [diff] [blame] | 467 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__); |