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