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" |
Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 35 | #include "py/persistentcode.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 36 | #include "py/runtime.h" |
| 37 | #include "py/builtin.h" |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 38 | #include "py/frozenmod.h" |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 39 | |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 40 | #if 0 // print debugging info |
| 41 | #define DEBUG_PRINT (1) |
| 42 | #define DEBUG_printf DEBUG_printf |
| 43 | #else // don't print debugging info |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 44 | #define DEBUG_PRINT (0) |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 45 | #define DEBUG_printf(...) (void)0 |
| 46 | #endif |
| 47 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 48 | #define PATH_SEP_CHAR '/' |
| 49 | |
Paul Sokolovsky | e5a3759 | 2014-10-25 21:04:13 +0300 | [diff] [blame] | 50 | bool mp_obj_is_package(mp_obj_t module) { |
| 51 | mp_obj_t dest[2]; |
| 52 | mp_load_method_maybe(module, MP_QSTR___path__, dest); |
| 53 | return dest[0] != MP_OBJ_NULL; |
| 54 | } |
| 55 | |
Paul Sokolovsky | fb742cd | 2016-05-21 21:33:42 +0300 | [diff] [blame] | 56 | // Stat either frozen or normal module by a given path |
| 57 | // (whatever is available, if at all). |
| 58 | STATIC mp_import_stat_t mp_import_stat_any(const char *path) { |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 59 | #if MICROPY_MODULE_FROZEN |
Paul Sokolovsky | fb742cd | 2016-05-21 21:33:42 +0300 | [diff] [blame] | 60 | mp_import_stat_t st = mp_frozen_stat(path); |
| 61 | if (st != MP_IMPORT_STAT_NO_EXIST) { |
| 62 | return st; |
| 63 | } |
Paul Sokolovsky | 8a2970e | 2016-05-21 22:23:08 +0300 | [diff] [blame] | 64 | #endif |
Paul Sokolovsky | fb742cd | 2016-05-21 21:33:42 +0300 | [diff] [blame] | 65 | return mp_import_stat(path); |
| 66 | } |
| 67 | |
Damien George | d9047d3 | 2016-12-13 15:09:48 +1100 | [diff] [blame] | 68 | STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) { |
Paul Sokolovsky | fb742cd | 2016-05-21 21:33:42 +0300 | [diff] [blame] | 69 | mp_import_stat_t stat = mp_import_stat_any(vstr_null_terminated_str(path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 70 | if (stat == MP_IMPORT_STAT_FILE) { |
| 71 | return stat; |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 72 | } |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 73 | |
| 74 | #if MICROPY_PERSISTENT_CODE_LOAD |
| 75 | vstr_ins_byte(path, path->len - 2, 'm'); |
Paul Sokolovsky | fb742cd | 2016-05-21 21:33:42 +0300 | [diff] [blame] | 76 | stat = mp_import_stat_any(vstr_null_terminated_str(path)); |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 77 | if (stat == MP_IMPORT_STAT_FILE) { |
| 78 | return stat; |
| 79 | } |
| 80 | #endif |
| 81 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 82 | return MP_IMPORT_STAT_NO_EXIST; |
| 83 | } |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 84 | |
Damien George | d9047d3 | 2016-12-13 15:09:48 +1100 | [diff] [blame] | 85 | STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) { |
| 86 | mp_import_stat_t stat = mp_import_stat_any(vstr_null_terminated_str(path)); |
| 87 | DEBUG_printf("stat %s: %d\n", vstr_str(path), stat); |
| 88 | if (stat == MP_IMPORT_STAT_DIR) { |
| 89 | return stat; |
| 90 | } |
| 91 | |
| 92 | // not a directory, add .py and try as a file |
| 93 | vstr_add_str(path, ".py"); |
| 94 | return stat_file_py_or_mpy(path); |
| 95 | } |
| 96 | |
Damien George | 8a0801a | 2014-06-11 19:55:46 +0100 | [diff] [blame] | 97 | 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] | 98 | #if MICROPY_PY_SYS |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 99 | // extract the list of paths |
| 100 | mp_uint_t path_num; |
| 101 | mp_obj_t *path_items; |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 102 | mp_obj_list_get(mp_sys_path, &path_num, &path_items); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 103 | |
| 104 | if (path_num == 0) { |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 105 | #endif |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 106 | // mp_sys_path is empty, so just use the given file name |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 107 | vstr_add_strn(dest, file_str, file_len); |
| 108 | return stat_dir_or_file(dest); |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 109 | #if MICROPY_PY_SYS |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 110 | } else { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 111 | // go through each path looking for a directory or file |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 112 | for (mp_uint_t i = 0; i < path_num; i++) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 113 | vstr_reset(dest); |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 114 | mp_uint_t p_len; |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 115 | 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] | 116 | if (p_len > 0) { |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 117 | vstr_add_strn(dest, p, p_len); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 118 | vstr_add_char(dest, PATH_SEP_CHAR); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 119 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 120 | vstr_add_strn(dest, file_str, file_len); |
| 121 | mp_import_stat_t stat = stat_dir_or_file(dest); |
| 122 | if (stat != MP_IMPORT_STAT_NO_EXIST) { |
| 123 | return stat; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 126 | |
| 127 | // could not find a directory or file |
| 128 | return MP_IMPORT_STAT_NO_EXIST; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 129 | } |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 130 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 133 | #if MICROPY_ENABLE_COMPILER |
Damien George | 1831034 | 2017-03-14 11:16:31 +1100 | [diff] [blame^] | 134 | STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) { |
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 | } |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 144 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 145 | |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 146 | #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 147 | STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) { |
| 148 | #if MICROPY_PY___FILE__ |
| 149 | // TODO |
| 150 | //qstr source_name = lex->source_name; |
| 151 | //mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); |
| 152 | #endif |
| 153 | |
| 154 | // execute the module in its context |
| 155 | mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj); |
| 156 | |
| 157 | // save context |
| 158 | mp_obj_dict_t *volatile old_globals = mp_globals_get(); |
| 159 | mp_obj_dict_t *volatile old_locals = mp_locals_get(); |
| 160 | |
| 161 | // set new context |
| 162 | mp_globals_set(mod_globals); |
| 163 | mp_locals_set(mod_globals); |
| 164 | |
| 165 | nlr_buf_t nlr; |
| 166 | if (nlr_push(&nlr) == 0) { |
| 167 | mp_obj_t module_fun = mp_make_function_from_raw_code(raw_code, MP_OBJ_NULL, MP_OBJ_NULL); |
| 168 | mp_call_function_0(module_fun); |
| 169 | |
| 170 | // finish nlr block, restore context |
| 171 | nlr_pop(); |
| 172 | mp_globals_set(old_globals); |
| 173 | mp_locals_set(old_locals); |
| 174 | } else { |
| 175 | // exception; restore context and re-raise same exception |
| 176 | mp_globals_set(old_globals); |
| 177 | mp_locals_set(old_locals); |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 178 | nlr_jump(nlr.ret_val); |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | #endif |
| 182 | |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 183 | STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 184 | #if MICROPY_MODULE_FROZEN || MICROPY_PERSISTENT_CODE_LOAD || MICROPY_ENABLE_COMPILER |
Damien George | 827b0f7 | 2015-01-29 13:57:23 +0000 | [diff] [blame] | 185 | char *file_str = vstr_null_terminated_str(file); |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 186 | #endif |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 187 | |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 188 | // If we support frozen modules (either as str or mpy) then try to find the |
| 189 | // requested filename in the list of frozen module filenames. |
| 190 | #if MICROPY_MODULE_FROZEN |
| 191 | void *modref; |
| 192 | int frozen_type = mp_find_frozen_module(file_str, file->len, &modref); |
| 193 | #endif |
| 194 | |
| 195 | // If we support frozen str modules and the compiler is enabled, and we |
| 196 | // found the filename in the list of frozen files, then load and execute it. |
| 197 | #if MICROPY_MODULE_FROZEN_STR |
| 198 | if (frozen_type == MP_FROZEN_STR) { |
Damien George | 1831034 | 2017-03-14 11:16:31 +1100 | [diff] [blame^] | 199 | do_load_from_lexer(module_obj, modref); |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | #endif |
| 203 | |
| 204 | // If we support frozen mpy modules and we found a corresponding file (and |
| 205 | // its data) in the list of frozen files, execute it. |
| 206 | #if MICROPY_MODULE_FROZEN_MPY |
| 207 | if (frozen_type == MP_FROZEN_MPY) { |
| 208 | do_execute_raw_code(module_obj, modref); |
| 209 | return; |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | // If we support loading .mpy files then check if the file extension is of |
| 214 | // the correct format and, if so, load and execute the file. |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 215 | #if MICROPY_PERSISTENT_CODE_LOAD |
| 216 | if (file_str[file->len - 3] == 'm') { |
| 217 | mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str); |
| 218 | do_execute_raw_code(module_obj, raw_code); |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 219 | return; |
| 220 | } |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 221 | #endif |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 222 | |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 223 | // If we can compile scripts then load the file and compile and execute it. |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 224 | #if MICROPY_ENABLE_COMPILER |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 225 | { |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 226 | mp_lexer_t *lex = mp_lexer_new_from_file(file_str); |
Damien George | 1831034 | 2017-03-14 11:16:31 +1100 | [diff] [blame^] | 227 | do_load_from_lexer(module_obj, lex); |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 228 | return; |
Damien George | 432e827 | 2015-11-02 21:57:42 +0000 | [diff] [blame] | 229 | } |
Damien George | 274952a | 2016-05-23 12:42:23 +0100 | [diff] [blame] | 230 | #endif |
| 231 | |
| 232 | // If we get here then the file was not frozen and we can't compile scripts. |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 233 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, |
| 234 | "script compilation not supported")); |
Paul Sokolovsky | 640e0b2 | 2015-01-20 11:52:12 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 237 | STATIC void chop_component(const char *start, const char **end) { |
| 238 | const char *p = *end; |
| 239 | while (p > start) { |
| 240 | if (*--p == '.') { |
| 241 | *end = p; |
| 242 | return; |
| 243 | } |
| 244 | } |
| 245 | *end = p; |
| 246 | } |
| 247 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 248 | mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 249 | #if DEBUG_PRINT |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 250 | DEBUG_printf("__import__:\n"); |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 251 | for (mp_uint_t i = 0; i < n_args; i++) { |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 252 | DEBUG_printf(" "); |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 253 | mp_obj_print(args[i], PRINT_REPR); |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 254 | DEBUG_printf("\n"); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 255 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 256 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 257 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 258 | mp_obj_t module_name = args[0]; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 259 | mp_obj_t fromtuple = mp_const_none; |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 260 | mp_int_t level = 0; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 261 | if (n_args >= 4) { |
| 262 | fromtuple = args[3]; |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 263 | if (n_args >= 5) { |
| 264 | level = MP_OBJ_SMALL_INT_VALUE(args[4]); |
| 265 | } |
| 266 | } |
| 267 | |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 268 | mp_uint_t mod_len; |
| 269 | 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] | 270 | |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 271 | if (level != 0) { |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 272 | // What we want to do here is to take name of current module, |
| 273 | // chop <level> trailing components, and concatenate with passed-in |
| 274 | // module name, thus resolving relative import name into absolue. |
| 275 | // This even appears to be correct per |
| 276 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 277 | // "Relative imports use a module's __name__ attribute to determine that |
| 278 | // module's position in the package hierarchy." |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 279 | level--; |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 280 | mp_obj_t this_name_q = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___name__)); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 281 | assert(this_name_q != MP_OBJ_NULL); |
Paul Sokolovsky | 9780e55 | 2015-06-29 00:21:36 +0300 | [diff] [blame] | 282 | #if MICROPY_CPYTHON_COMPAT |
| 283 | if (MP_OBJ_QSTR_VALUE(this_name_q) == MP_QSTR___main__) { |
| 284 | // This is a module run by -m command-line switch, get its real name from backup attribute |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 285 | this_name_q = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___main__)); |
Paul Sokolovsky | 9780e55 | 2015-06-29 00:21:36 +0300 | [diff] [blame] | 286 | } |
| 287 | #endif |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 288 | mp_map_t *globals_map = &mp_globals_get()->map; |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 289 | mp_map_elem_t *elem = mp_map_lookup(globals_map, MP_OBJ_NEW_QSTR(MP_QSTR___path__), MP_MAP_LOOKUP); |
| 290 | bool is_pkg = (elem != NULL); |
| 291 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 292 | #if DEBUG_PRINT |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 293 | DEBUG_printf("Current module/package: "); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 294 | mp_obj_print(this_name_q, PRINT_REPR); |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 295 | DEBUG_printf(", is_package: %d", is_pkg); |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 296 | DEBUG_printf("\n"); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 297 | #endif |
| 298 | |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 299 | mp_uint_t this_name_l; |
| 300 | 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] | 301 | |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 302 | const char *p = this_name + this_name_l; |
| 303 | if (!is_pkg) { |
| 304 | // We have module, but relative imports are anchored at package, so |
| 305 | // go there. |
| 306 | chop_component(this_name, &p); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 307 | } |
| 308 | |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 309 | |
| 310 | uint dots_seen = 0; |
| 311 | while (level--) { |
| 312 | chop_component(this_name, &p); |
| 313 | dots_seen++; |
| 314 | } |
| 315 | |
| 316 | if (dots_seen == 0 && level >= 1) { |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 317 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 318 | // "If the module's name does not contain any package information |
| 319 | // (e.g. it is set to '__main__') then relative imports are |
| 320 | // resolved as if the module were a top level module, regardless |
| 321 | // of where the module is actually located on the file system." |
| 322 | // Supposedly this if catches this condition and resolve it properly |
| 323 | // TODO: But nobody knows for sure. This condition happens when |
| 324 | // package's __init__.py does something like "import .submod". So, |
| 325 | // maybe we should check for package here? But quote above doesn't |
| 326 | // talk about packages, it talks about dot-less module names. |
Paul Sokolovsky | 078172d | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 327 | 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] | 328 | p = this_name + this_name_l; |
Paul Sokolovsky | 9e6c829 | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 329 | } else if (level != -1) { |
Damien George | 7d0d721 | 2016-10-17 12:17:37 +1100 | [diff] [blame] | 330 | mp_raise_msg(&mp_type_ImportError, "invalid relative import"); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 331 | } |
| 332 | |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 333 | 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] | 334 | char *new_mod = alloca(new_mod_l); |
| 335 | memcpy(new_mod, this_name, p - this_name); |
| 336 | if (mod_len != 0) { |
| 337 | new_mod[p - this_name] = '.'; |
| 338 | memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len); |
| 339 | } |
| 340 | |
| 341 | qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l); |
Paul Sokolovsky | c4045f5 | 2015-06-27 00:34:53 +0300 | [diff] [blame] | 342 | DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q)); |
| 343 | if (new_mod_q == MP_QSTR_) { |
Damien George | 63e291d | 2017-01-16 16:21:04 +1100 | [diff] [blame] | 344 | mp_raise_msg(&mp_type_ValueError, "cannot perform relative import"); |
Paul Sokolovsky | c4045f5 | 2015-06-27 00:34:53 +0300 | [diff] [blame] | 345 | } |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 346 | module_name = MP_OBJ_NEW_QSTR(new_mod_q); |
| 347 | mod_str = new_mod; |
| 348 | mod_len = new_mod_l; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 351 | // check if module already exists |
Paul Sokolovsky | 8064892 | 2015-01-21 23:14:46 +0200 | [diff] [blame] | 352 | qstr module_name_qstr = mp_obj_str_get_qstr(module_name); |
| 353 | mp_obj_t module_obj = mp_module_get(module_name_qstr); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 354 | if (module_obj != MP_OBJ_NULL) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 355 | DEBUG_printf("Module already loaded\n"); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 356 | // If it's not a package, return module right away |
| 357 | char *p = strchr(mod_str, '.'); |
| 358 | if (p == NULL) { |
| 359 | return module_obj; |
| 360 | } |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 361 | // If fromlist is not empty, return leaf module |
| 362 | if (fromtuple != mp_const_none) { |
| 363 | return module_obj; |
| 364 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 365 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 366 | qstr pkg_name = qstr_from_strn(mod_str, p - mod_str); |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 367 | return mp_module_get(pkg_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 368 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 369 | DEBUG_printf("Module not yet loaded\n"); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 370 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 371 | uint last = 0; |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 372 | VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX) |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 373 | module_obj = MP_OBJ_NULL; |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 374 | mp_obj_t top_module_obj = MP_OBJ_NULL; |
| 375 | mp_obj_t outer_module_obj = MP_OBJ_NULL; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 376 | uint i; |
| 377 | for (i = 1; i <= mod_len; i++) { |
| 378 | if (i == mod_len || mod_str[i] == '.') { |
| 379 | // create a qstr for the module name up to this depth |
| 380 | qstr mod_name = qstr_from_strn(mod_str, i); |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 381 | DEBUG_printf("Processing module: %s\n", qstr_str(mod_name)); |
Paul Sokolovsky | 078172d | 2015-02-16 12:10:13 +0200 | [diff] [blame] | 382 | DEBUG_printf("Previous path: =%.*s=\n", vstr_len(&path), vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 383 | |
| 384 | // find the file corresponding to the module name |
| 385 | mp_import_stat_t stat; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 386 | if (vstr_len(&path) == 0) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 387 | // first module in the dotted-name; search for a directory or file |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 388 | stat = find_file(mod_str, i, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 389 | } else { |
| 390 | // latter module in the dotted-name; append to path |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 391 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 392 | vstr_add_strn(&path, mod_str + last, i - last); |
| 393 | stat = stat_dir_or_file(&path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 394 | } |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 395 | DEBUG_printf("Current path: %.*s\n", vstr_len(&path), vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 396 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 397 | if (stat == MP_IMPORT_STAT_NO_EXIST) { |
Damien George | c14a816 | 2014-10-12 11:46:04 +0100 | [diff] [blame] | 398 | #if MICROPY_MODULE_WEAK_LINKS |
| 399 | // check if there is a weak link to this module |
| 400 | if (i == mod_len) { |
Damien George | 78d702c | 2014-12-09 16:19:48 +0000 | [diff] [blame] | 401 | 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] | 402 | if (el == NULL) { |
| 403 | goto no_exist; |
| 404 | } |
| 405 | // found weak linked module |
| 406 | module_obj = el->value; |
| 407 | } else { |
| 408 | no_exist: |
| 409 | #else |
| 410 | { |
| 411 | #endif |
| 412 | // couldn't find the file, so fail |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 413 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7d0d721 | 2016-10-17 12:17:37 +1100 | [diff] [blame] | 414 | mp_raise_msg(&mp_type_ImportError, "module not found"); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 415 | } else { |
| 416 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 417 | "no module named '%q'", mod_name)); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 418 | } |
Damien George | c14a816 | 2014-10-12 11:46:04 +0100 | [diff] [blame] | 419 | } |
| 420 | } else { |
| 421 | // found the file, so get the module |
| 422 | module_obj = mp_module_get(mod_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 425 | if (module_obj == MP_OBJ_NULL) { |
| 426 | // module not already loaded, so load it! |
| 427 | |
| 428 | module_obj = mp_obj_new_module(mod_name); |
| 429 | |
Paul Sokolovsky | e503512 | 2014-10-25 21:16:24 +0300 | [diff] [blame] | 430 | // if args[3] (fromtuple) has magic value False, set up |
| 431 | // this module for command-line "-m" option (set module's |
| 432 | // name to __main__ instead of real name). |
| 433 | if (i == mod_len && fromtuple == mp_const_false) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 434 | mp_obj_module_t *o = MP_OBJ_TO_PTR(module_obj); |
| 435 | mp_obj_dict_store(MP_OBJ_FROM_PTR(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] | 436 | #if MICROPY_CPYTHON_COMPAT |
Delio Brignoli | 21c719b | 2016-08-22 12:28:22 +0200 | [diff] [blame] | 437 | // Store module as "__main__" in the dictionary of loaded modules (returned by sys.modules). |
Paul Sokolovsky | 7ea3fa2 | 2016-09-20 17:55:42 +0300 | [diff] [blame] | 438 | mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)), MP_OBJ_NEW_QSTR(MP_QSTR___main__), module_obj); |
Paul Sokolovsky | 9780e55 | 2015-06-29 00:21:36 +0300 | [diff] [blame] | 439 | // Store real name in "__main__" attribute. Choosen semi-randonly, to reuse existing qstr's. |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 440 | mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(mod_name)); |
Paul Sokolovsky | 9780e55 | 2015-06-29 00:21:36 +0300 | [diff] [blame] | 441 | #endif |
Paul Sokolovsky | e503512 | 2014-10-25 21:16:24 +0300 | [diff] [blame] | 442 | } |
| 443 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 444 | if (stat == MP_IMPORT_STAT_DIR) { |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 445 | DEBUG_printf("%.*s is dir\n", vstr_len(&path), vstr_str(&path)); |
Chris Angelico | daf973a | 2014-06-06 03:51:03 +1000 | [diff] [blame] | 446 | // https://docs.python.org/3/reference/import.html |
Paul Sokolovsky | f9589d2 | 2014-05-10 18:46:02 +0300 | [diff] [blame] | 447 | // "Specifically, any module that contains a __path__ attribute is considered a package." |
Damien George | 2617eeb | 2014-05-25 22:27:57 +0100 | [diff] [blame] | 448 | mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false)); |
Damien George | b528e9a | 2017-01-08 20:17:23 +1100 | [diff] [blame] | 449 | size_t orig_path_len = path.len; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 450 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 451 | vstr_add_str(&path, "__init__.py"); |
Damien George | d9047d3 | 2016-12-13 15:09:48 +1100 | [diff] [blame] | 452 | if (stat_file_py_or_mpy(&path) != MP_IMPORT_STAT_FILE) { |
Paul Sokolovsky | ae184cb | 2016-07-02 14:45:49 +0300 | [diff] [blame] | 453 | //mp_warning("%s is imported as namespace package", vstr_str(&path)); |
Paul Sokolovsky | a5854d2 | 2014-04-15 01:23:40 +0300 | [diff] [blame] | 454 | } else { |
| 455 | do_load(module_obj, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 456 | } |
Damien George | b528e9a | 2017-01-08 20:17:23 +1100 | [diff] [blame] | 457 | path.len = orig_path_len; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 458 | } else { // MP_IMPORT_STAT_FILE |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 459 | do_load(module_obj, &path); |
Damien George | d23834b | 2017-01-16 16:40:47 +1100 | [diff] [blame] | 460 | // This should be the last component in the import path. If there are |
| 461 | // remaining components then it's an ImportError because the current path |
| 462 | // (the module that was just loaded) is not a package. This will be caught |
| 463 | // on the next iteration because the file will not exist. |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 466 | if (outer_module_obj != MP_OBJ_NULL) { |
| 467 | qstr s = qstr_from_strn(mod_str + last, i - last); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 468 | mp_store_attr(outer_module_obj, s, module_obj); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 469 | } |
| 470 | outer_module_obj = module_obj; |
| 471 | if (top_module_obj == MP_OBJ_NULL) { |
| 472 | top_module_obj = module_obj; |
| 473 | } |
| 474 | last = i + 1; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 478 | // If fromlist is not empty, return leaf module |
| 479 | if (fromtuple != mp_const_none) { |
| 480 | return module_obj; |
| 481 | } |
| 482 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 483 | return top_module_obj; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 484 | } |
Paul Sokolovsky | 1d938c9 | 2014-02-04 00:46:17 +0200 | [diff] [blame] | 485 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__); |