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 <stdint.h> |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | #include <assert.h> |
| 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 |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 52 | #define DEBUG_PRINT (0) |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 53 | #define DEBUG_printf(...) (void)0 |
| 54 | #endif |
| 55 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 56 | #define PATH_SEP_CHAR '/' |
| 57 | |
Damien George | 78d702c | 2014-12-09 16:19:48 +0000 | [diff] [blame] | 58 | #if MICROPY_MODULE_WEAK_LINKS |
| 59 | STATIC const mp_map_elem_t mp_builtin_module_weak_links_table[] = { |
| 60 | MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS |
| 61 | }; |
| 62 | |
| 63 | STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_weak_links_map, mp_builtin_module_weak_links_table); |
| 64 | #endif |
| 65 | |
Paul Sokolovsky | e5a3759 | 2014-10-25 21:04:13 +0300 | [diff] [blame] | 66 | bool mp_obj_is_package(mp_obj_t module) { |
| 67 | mp_obj_t dest[2]; |
| 68 | mp_load_method_maybe(module, MP_QSTR___path__, dest); |
| 69 | return dest[0] != MP_OBJ_NULL; |
| 70 | } |
| 71 | |
Damien George | 8a0801a | 2014-06-11 19:55:46 +0100 | [diff] [blame] | 72 | STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 73 | //printf("stat %s\n", vstr_str(path)); |
| 74 | mp_import_stat_t stat = mp_import_stat(vstr_str(path)); |
| 75 | if (stat == MP_IMPORT_STAT_DIR) { |
| 76 | return stat; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 77 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 78 | vstr_add_str(path, ".py"); |
| 79 | stat = mp_import_stat(vstr_str(path)); |
| 80 | if (stat == MP_IMPORT_STAT_FILE) { |
| 81 | return stat; |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 82 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 83 | return MP_IMPORT_STAT_NO_EXIST; |
| 84 | } |
Paul Sokolovsky | d720ab5 | 2014-01-20 00:03:34 +0200 | [diff] [blame] | 85 | |
Damien George | 8a0801a | 2014-06-11 19:55:46 +0100 | [diff] [blame] | 86 | 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] | 87 | #if MICROPY_PY_SYS |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 88 | // extract the list of paths |
| 89 | mp_uint_t path_num; |
| 90 | mp_obj_t *path_items; |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 91 | mp_obj_list_get(mp_sys_path, &path_num, &path_items); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 92 | |
| 93 | if (path_num == 0) { |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 94 | #endif |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 95 | // mp_sys_path is empty, so just use the given file name |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 96 | vstr_add_strn(dest, file_str, file_len); |
| 97 | return stat_dir_or_file(dest); |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 98 | #if MICROPY_PY_SYS |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 99 | } else { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 100 | // go through each path looking for a directory or file |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 101 | for (mp_uint_t i = 0; i < path_num; i++) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 102 | vstr_reset(dest); |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 103 | mp_uint_t p_len; |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 104 | 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] | 105 | if (p_len > 0) { |
Damien George | 698ec21 | 2014-02-08 18:17:23 +0000 | [diff] [blame] | 106 | vstr_add_strn(dest, p, p_len); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 107 | vstr_add_char(dest, PATH_SEP_CHAR); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 108 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 109 | vstr_add_strn(dest, file_str, file_len); |
| 110 | mp_import_stat_t stat = stat_dir_or_file(dest); |
| 111 | if (stat != MP_IMPORT_STAT_NO_EXIST) { |
| 112 | return stat; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 113 | } |
| 114 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 115 | |
| 116 | // could not find a directory or file |
| 117 | return MP_IMPORT_STAT_NO_EXIST; |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 118 | } |
Sven Wegener | 238ab50 | 2014-11-05 21:02:33 +0100 | [diff] [blame] | 119 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Damien George | 8a0801a | 2014-06-11 19:55:46 +0100 | [diff] [blame] | 122 | STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 123 | // create the lexer |
| 124 | mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file)); |
Paul Sokolovsky | e11b17c | 2014-02-05 00:47:06 +0200 | [diff] [blame] | 125 | |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 126 | if (lex == NULL) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 127 | // we verified the file exists using stat, but lexer could still fail |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 128 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 129 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found")); |
| 130 | } else { |
| 131 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, |
| 132 | "no module named '%s'", vstr_str(file))); |
| 133 | } |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Paul Sokolovsky | d0f5e61 | 2014-07-25 11:00:15 +0300 | [diff] [blame] | 136 | #if MICROPY_PY___FILE__ |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 137 | qstr source_name = lex->source_name; |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 138 | 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] | 139 | #endif |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 140 | |
Damien George | c4d0868 | 2014-10-05 20:13:34 +0100 | [diff] [blame] | 141 | // parse, compile and execute the module in its context |
| 142 | mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj); |
| 143 | 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] | 144 | } |
| 145 | |
Paul Sokolovsky | 8becca7 | 2014-10-25 21:03:20 +0300 | [diff] [blame] | 146 | 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] | 147 | #if DEBUG_PRINT |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 148 | DEBUG_printf("__import__:\n"); |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 149 | for (mp_uint_t i = 0; i < n_args; i++) { |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 150 | DEBUG_printf(" "); |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 151 | mp_obj_print(args[i], PRINT_REPR); |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 152 | DEBUG_printf("\n"); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 153 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 154 | #endif |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 155 | |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 156 | mp_obj_t module_name = args[0]; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 157 | mp_obj_t fromtuple = mp_const_none; |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 158 | mp_int_t level = 0; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 159 | if (n_args >= 4) { |
| 160 | fromtuple = args[3]; |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 161 | if (n_args >= 5) { |
| 162 | level = MP_OBJ_SMALL_INT_VALUE(args[4]); |
| 163 | } |
| 164 | } |
| 165 | |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 166 | mp_uint_t mod_len; |
| 167 | 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] | 168 | |
Paul Sokolovsky | feacaa1 | 2014-02-21 01:15:20 +0200 | [diff] [blame] | 169 | if (level != 0) { |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 170 | // What we want to do here is to take name of current module, |
| 171 | // chop <level> trailing components, and concatenate with passed-in |
| 172 | // module name, thus resolving relative import name into absolue. |
| 173 | // This even appears to be correct per |
| 174 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 175 | // "Relative imports use a module's __name__ attribute to determine that |
| 176 | // module's position in the package hierarchy." |
| 177 | mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__)); |
| 178 | assert(this_name_q != MP_OBJ_NULL); |
| 179 | #if DEBUG_PRINT |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 180 | DEBUG_printf("Current module: "); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 181 | mp_obj_print(this_name_q, PRINT_REPR); |
Damien George | eaaebf3 | 2014-09-23 10:59:05 +0100 | [diff] [blame] | 182 | DEBUG_printf("\n"); |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 183 | #endif |
| 184 | |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 185 | mp_uint_t this_name_l; |
| 186 | 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] | 187 | |
| 188 | uint dots_seen = 0; |
| 189 | const char *p = this_name + this_name_l - 1; |
| 190 | while (p > this_name) { |
| 191 | if (*p == '.') { |
| 192 | dots_seen++; |
| 193 | if (--level == 0) { |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | p--; |
| 198 | } |
| 199 | |
| 200 | if (dots_seen == 0 && level == 1) { |
| 201 | // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name |
| 202 | // "If the module's name does not contain any package information |
| 203 | // (e.g. it is set to '__main__') then relative imports are |
| 204 | // resolved as if the module were a top level module, regardless |
| 205 | // of where the module is actually located on the file system." |
| 206 | // Supposedly this if catches this condition and resolve it properly |
| 207 | // TODO: But nobody knows for sure. This condition happens when |
| 208 | // package's __init__.py does something like "import .submod". So, |
| 209 | // maybe we should check for package here? But quote above doesn't |
| 210 | // talk about packages, it talks about dot-less module names. |
| 211 | p = this_name + this_name_l; |
| 212 | } else if (level != 0) { |
| 213 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "Invalid relative import")); |
| 214 | } |
| 215 | |
| 216 | uint new_mod_l = (mod_len == 0 ? p - this_name : p - this_name + 1 + mod_len); |
| 217 | char *new_mod = alloca(new_mod_l); |
| 218 | memcpy(new_mod, this_name, p - this_name); |
| 219 | if (mod_len != 0) { |
| 220 | new_mod[p - this_name] = '.'; |
| 221 | memcpy(new_mod + (p - this_name) + 1, mod_str, mod_len); |
| 222 | } |
| 223 | |
| 224 | qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l); |
| 225 | DEBUG_printf("Resolved relative name: %s\n", qstr_str(new_mod_q)); |
| 226 | module_name = MP_OBJ_NEW_QSTR(new_mod_q); |
| 227 | mod_str = new_mod; |
| 228 | mod_len = new_mod_l; |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 231 | // check if module already exists |
Paul Sokolovsky | a5afc90 | 2014-04-12 17:46:54 +0300 | [diff] [blame] | 232 | 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] | 233 | if (module_obj != MP_OBJ_NULL) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 234 | DEBUG_printf("Module already loaded\n"); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 235 | // If it's not a package, return module right away |
| 236 | char *p = strchr(mod_str, '.'); |
| 237 | if (p == NULL) { |
| 238 | return module_obj; |
| 239 | } |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 240 | // If fromlist is not empty, return leaf module |
| 241 | if (fromtuple != mp_const_none) { |
| 242 | return module_obj; |
| 243 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 244 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 245 | qstr pkg_name = qstr_from_strn(mod_str, p - mod_str); |
Damien George | caac542 | 2014-03-25 14:18:18 +0000 | [diff] [blame] | 246 | return mp_module_get(pkg_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 247 | } |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 248 | DEBUG_printf("Module not yet loaded\n"); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 249 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 250 | uint last = 0; |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 251 | VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX) |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 252 | module_obj = MP_OBJ_NULL; |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 253 | mp_obj_t top_module_obj = MP_OBJ_NULL; |
| 254 | mp_obj_t outer_module_obj = MP_OBJ_NULL; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 255 | uint i; |
| 256 | for (i = 1; i <= mod_len; i++) { |
| 257 | if (i == mod_len || mod_str[i] == '.') { |
| 258 | // create a qstr for the module name up to this depth |
| 259 | qstr mod_name = qstr_from_strn(mod_str, i); |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 260 | DEBUG_printf("Processing module: %s\n", qstr_str(mod_name)); |
Paul Sokolovsky | ad6178b | 2014-05-10 19:00:03 +0300 | [diff] [blame] | 261 | DEBUG_printf("Previous path: %s\n", vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 262 | |
| 263 | // find the file corresponding to the module name |
| 264 | mp_import_stat_t stat; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 265 | if (vstr_len(&path) == 0) { |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 266 | // first module in the dotted-name; search for a directory or file |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 267 | stat = find_file(mod_str, i, &path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 268 | } else { |
| 269 | // latter module in the dotted-name; append to path |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 270 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 271 | vstr_add_strn(&path, mod_str + last, i - last); |
| 272 | stat = stat_dir_or_file(&path); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 273 | } |
Paul Sokolovsky | ad6178b | 2014-05-10 19:00:03 +0300 | [diff] [blame] | 274 | DEBUG_printf("Current path: %s\n", vstr_str(&path)); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 275 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 276 | if (stat == MP_IMPORT_STAT_NO_EXIST) { |
Damien George | c14a816 | 2014-10-12 11:46:04 +0100 | [diff] [blame] | 277 | #if MICROPY_MODULE_WEAK_LINKS |
| 278 | // check if there is a weak link to this module |
| 279 | if (i == mod_len) { |
Damien George | 78d702c | 2014-12-09 16:19:48 +0000 | [diff] [blame] | 280 | 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] | 281 | if (el == NULL) { |
| 282 | goto no_exist; |
| 283 | } |
| 284 | // found weak linked module |
| 285 | module_obj = el->value; |
| 286 | } else { |
| 287 | no_exist: |
| 288 | #else |
| 289 | { |
| 290 | #endif |
| 291 | // couldn't find the file, so fail |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 292 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 293 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found")); |
| 294 | } else { |
| 295 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, |
| 296 | "no module named '%s'", qstr_str(mod_name))); |
| 297 | } |
Damien George | c14a816 | 2014-10-12 11:46:04 +0100 | [diff] [blame] | 298 | } |
| 299 | } else { |
| 300 | // found the file, so get the module |
| 301 | module_obj = mp_module_get(mod_name); |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 304 | if (module_obj == MP_OBJ_NULL) { |
| 305 | // module not already loaded, so load it! |
| 306 | |
| 307 | module_obj = mp_obj_new_module(mod_name); |
| 308 | |
Paul Sokolovsky | e503512 | 2014-10-25 21:16:24 +0300 | [diff] [blame] | 309 | // if args[3] (fromtuple) has magic value False, set up |
| 310 | // this module for command-line "-m" option (set module's |
| 311 | // name to __main__ instead of real name). |
| 312 | if (i == mod_len && fromtuple == mp_const_false) { |
| 313 | mp_obj_module_t *o = module_obj; |
| 314 | mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__)); |
| 315 | } |
| 316 | |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 317 | if (stat == MP_IMPORT_STAT_DIR) { |
Paul Sokolovsky | e081329 | 2014-04-11 23:08:29 +0300 | [diff] [blame] | 318 | DEBUG_printf("%s is dir\n", vstr_str(&path)); |
Chris Angelico | daf973a | 2014-06-06 03:51:03 +1000 | [diff] [blame] | 319 | // https://docs.python.org/3/reference/import.html |
Paul Sokolovsky | f9589d2 | 2014-05-10 18:46:02 +0300 | [diff] [blame] | 320 | // "Specifically, any module that contains a __path__ attribute is considered a package." |
Damien George | 2617eeb | 2014-05-25 22:27:57 +0100 | [diff] [blame] | 321 | 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] | 322 | vstr_add_char(&path, PATH_SEP_CHAR); |
| 323 | vstr_add_str(&path, "__init__.py"); |
Paul Sokolovsky | d378357 | 2014-02-16 01:51:46 +0200 | [diff] [blame] | 324 | if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) { |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 325 | 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^] | 326 | mp_warning("%s is imported as namespace package", vstr_str(&path)); |
Paul Sokolovsky | a5854d2 | 2014-04-15 01:23:40 +0300 | [diff] [blame] | 327 | } else { |
| 328 | do_load(module_obj, &path); |
Paul Sokolovsky | ad6178b | 2014-05-10 19:00:03 +0300 | [diff] [blame] | 329 | 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] | 330 | } |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 331 | } else { // MP_IMPORT_STAT_FILE |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 332 | do_load(module_obj, &path); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 333 | // TODO: We cannot just break here, at the very least, we must execute |
| 334 | // trailer code below. But otherwise if there're remaining components, |
| 335 | // that would be (??) object path within module, not modules path within FS. |
| 336 | // break; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 339 | if (outer_module_obj != MP_OBJ_NULL) { |
| 340 | qstr s = qstr_from_strn(mod_str + last, i - last); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 341 | mp_store_attr(outer_module_obj, s, module_obj); |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 342 | } |
| 343 | outer_module_obj = module_obj; |
| 344 | if (top_module_obj == MP_OBJ_NULL) { |
| 345 | top_module_obj = module_obj; |
| 346 | } |
| 347 | last = i + 1; |
Damien George | e09ffa1 | 2014-02-05 23:57:48 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | if (i < mod_len) { |
| 352 | // we loaded a package, now need to load objects from within that package |
| 353 | // TODO |
| 354 | assert(0); |
| 355 | } |
| 356 | |
Paul Sokolovsky | fb7f943 | 2014-02-20 00:29:54 +0200 | [diff] [blame] | 357 | // If fromlist is not empty, return leaf module |
| 358 | if (fromtuple != mp_const_none) { |
| 359 | return module_obj; |
| 360 | } |
| 361 | // Otherwise, we need to return top-level package |
Paul Sokolovsky | 91ba7a5 | 2014-02-16 02:53:44 +0200 | [diff] [blame] | 362 | return top_module_obj; |
Damien George | 66028ab | 2014-01-03 14:03: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__); |