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