Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.

Dependents:   micropython-repl

This a port of MicroPython to the mbed Classic platform.

This provides an interpreter running on the board's USB serial connection.

Getting Started

Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:

  MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM
  Type "help()" for more information.
  >>>

Then you can start using micropython. For example:

  >>> from mbed import DigitalOut
  >>> from pins import LED1
  >>> led = DigitalOut(LED1)
  >>> led.write(1)

Requirements

You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.

Caveats

This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.

  • Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
  • Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
  • The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
  • Exceptions from the mbed classes are not yet handled.
  • Asynchronous processing (e.g. events on inputs) is not supported.

Credits

  • Damien P. George and other contributors who created micropython.
  • Colin Hogben, author of this port.
Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
2:c89e95946844
Update README and version

Who changed what in which revision?

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