blob: 43bb36b98c84d6c7b0a1e79a34961aab3083062a [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien George28708622014-01-02 21:30:26 +000027#include <stdlib.h>
Damien George28708622014-01-02 21:30:26 +000028#include <assert.h>
29
Damien Georgeb4b10fd2015-01-01 23:30:53 +000030#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/objmodule.h"
33#include "py/runtime.h"
34#include "py/builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000035
Damien George7f9d1d62015-04-09 23:56:15 +010036STATIC void module_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000037 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000038 mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
Damien George377b80b2014-09-08 10:45:23 +010039
Damien George93c4a6a2016-09-21 10:52:53 +100040 const char *module_name = "";
41 mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_MAP_LOOKUP);
42 if (elem != NULL) {
43 module_name = mp_obj_str_get_str(elem->value);
44 }
45
Damien George377b80b2014-09-08 10:45:23 +010046#if MICROPY_PY___FILE__
47 // If we store __file__ to imported modules then try to lookup this
48 // symbol to give more information about the module.
Damien George93c4a6a2016-09-21 10:52:53 +100049 elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP);
Damien George377b80b2014-09-08 10:45:23 +010050 if (elem != NULL) {
Damien George93c4a6a2016-09-21 10:52:53 +100051 mp_printf(print, "<module '%s' from '%s'>", module_name, mp_obj_str_get_str(elem->value));
Damien George377b80b2014-09-08 10:45:23 +010052 return;
53 }
54#endif
55
Damien George93c4a6a2016-09-21 10:52:53 +100056 mp_printf(print, "<module '%s'>", module_name);
Damien George28708622014-01-02 21:30:26 +000057}
58
Damien Georgeb1bbe962015-04-01 14:10:50 +000059STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
Damien George999cedb2015-11-27 17:01:44 +000060 mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb1bbe962015-04-01 14:10:50 +000061 if (dest[0] == MP_OBJ_NULL) {
62 // load attribute
63 mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
64 if (elem != NULL) {
65 dest[0] = elem->value;
Damien George78d702c2014-12-09 16:19:48 +000066 }
Damien George1d24ea52014-04-08 21:11:49 +010067 } else {
Damien Georgeb1bbe962015-04-01 14:10:50 +000068 // delete/store attribute
69 mp_obj_dict_t *dict = self->globals;
70 if (dict->map.is_fixed) {
71 #if MICROPY_CAN_OVERRIDE_BUILTINS
72 if (dict == &mp_module_builtins_globals) {
73 if (MP_STATE_VM(mp_module_builtins_override_dict) == NULL) {
Damien George999cedb2015-11-27 17:01:44 +000074 MP_STATE_VM(mp_module_builtins_override_dict) = MP_OBJ_TO_PTR(mp_obj_new_dict(1));
Damien Georgeb1bbe962015-04-01 14:10:50 +000075 }
76 dict = MP_STATE_VM(mp_module_builtins_override_dict);
77 } else
78 #endif
79 {
80 // can't delete or store to fixed map
81 return;
82 }
83 }
84 if (dest[1] == MP_OBJ_NULL) {
85 // delete attribute
Damien George999cedb2015-11-27 17:01:44 +000086 mp_obj_dict_delete(MP_OBJ_FROM_PTR(dict), MP_OBJ_NEW_QSTR(attr));
Damien Georgeb1bbe962015-04-01 14:10:50 +000087 } else {
88 // store attribute
89 // TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
Damien George999cedb2015-11-27 17:01:44 +000090 mp_obj_dict_store(MP_OBJ_FROM_PTR(dict), MP_OBJ_NEW_QSTR(attr), dest[1]);
Damien Georgeb1bbe962015-04-01 14:10:50 +000091 }
92 dest[0] = MP_OBJ_NULL; // indicate success
Damien George1d24ea52014-04-08 21:11:49 +010093 }
Damien George062478e2014-01-09 20:57:50 +000094}
95
Damien George0c36da02014-03-08 15:24:39 +000096const mp_obj_type_t mp_type_module = {
Damien Georgec5966122014-02-15 16:10:44 +000097 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000098 .name = MP_QSTR_module,
Damien George97209d32014-01-07 15:58:30 +000099 .print = module_print,
Damien Georgeb1bbe962015-04-01 14:10:50 +0000100 .attr = module_attr,
Damien George28708622014-01-02 21:30:26 +0000101};
102
103mp_obj_t mp_obj_new_module(qstr module_name) {
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200104 mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
105 mp_map_elem_t *el = mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200106 // We could error out if module already exists, but let C extensions
107 // add new members to existing modules.
108 if (el->value != MP_OBJ_NULL) {
109 return el->value;
110 }
111
Damien George0c36da02014-03-08 15:24:39 +0000112 // create new module object
Damien George28708622014-01-02 21:30:26 +0000113 mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
Damien George0c36da02014-03-08 15:24:39 +0000114 o->base.type = &mp_type_module;
Damien George999cedb2015-11-27 17:01:44 +0000115 o->globals = MP_OBJ_TO_PTR(mp_obj_new_dict(MICROPY_MODULE_DICT_SIZE));
Damien George0c36da02014-03-08 15:24:39 +0000116
117 // store __name__ entry in the module
Damien George999cedb2015-11-27 17:01:44 +0000118 mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(module_name));
Damien George0c36da02014-03-08 15:24:39 +0000119
120 // store the new module into the slot in the global dict holding all modules
Damien George999cedb2015-11-27 17:01:44 +0000121 el->value = MP_OBJ_FROM_PTR(o);
Damien George0c36da02014-03-08 15:24:39 +0000122
123 // return the new module
Damien George999cedb2015-11-27 17:01:44 +0000124 return MP_OBJ_FROM_PTR(o);
Damien George28708622014-01-02 21:30:26 +0000125}
126
Damien George8b0535e2014-04-05 21:53:54 +0100127mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in) {
Damien Georgecaac5422014-03-25 14:18:18 +0000128 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module));
Damien George999cedb2015-11-27 17:01:44 +0000129 mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgecaac5422014-03-25 14:18:18 +0000130 return self->globals;
131}
132
133/******************************************************************************/
134// Global module table and related functions
135
Damien Georgecbf76742015-11-27 13:38:15 +0000136STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
137 { MP_ROM_QSTR(MP_QSTR___main__), MP_ROM_PTR(&mp_module___main__) },
138 { MP_ROM_QSTR(MP_QSTR_builtins), MP_ROM_PTR(&mp_module_builtins) },
139 { MP_ROM_QSTR(MP_QSTR_micropython), MP_ROM_PTR(&mp_module_micropython) },
Damien George78d702c2014-12-09 16:19:48 +0000140
141#if MICROPY_PY_ARRAY
Damien Georgecbf76742015-11-27 13:38:15 +0000142 { MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mp_module_array) },
Damien George78d702c2014-12-09 16:19:48 +0000143#endif
144#if MICROPY_PY_IO
Paul Sokolovskyddb9dba2016-05-02 13:56:33 +0300145 { MP_ROM_QSTR(MP_QSTR_uio), MP_ROM_PTR(&mp_module_io) },
Damien George78d702c2014-12-09 16:19:48 +0000146#endif
147#if MICROPY_PY_COLLECTIONS
Paul Sokolovsky95495902016-05-02 13:57:46 +0300148 { MP_ROM_QSTR(MP_QSTR_ucollections), MP_ROM_PTR(&mp_module_collections) },
Damien George78d702c2014-12-09 16:19:48 +0000149#endif
150#if MICROPY_PY_STRUCT
Damien Georgecbf76742015-11-27 13:38:15 +0000151 { MP_ROM_QSTR(MP_QSTR_ustruct), MP_ROM_PTR(&mp_module_ustruct) },
Damien George78d702c2014-12-09 16:19:48 +0000152#endif
153
154#if MICROPY_PY_BUILTINS_FLOAT
155#if MICROPY_PY_MATH
Damien Georgecbf76742015-11-27 13:38:15 +0000156 { MP_ROM_QSTR(MP_QSTR_math), MP_ROM_PTR(&mp_module_math) },
Damien George78d702c2014-12-09 16:19:48 +0000157#endif
Paul Sokolovsky2b7236d2015-12-07 00:18:44 +0200158#if MICROPY_PY_BUILTINS_COMPLEX && MICROPY_PY_CMATH
Damien Georgecbf76742015-11-27 13:38:15 +0000159 { MP_ROM_QSTR(MP_QSTR_cmath), MP_ROM_PTR(&mp_module_cmath) },
Damien George78d702c2014-12-09 16:19:48 +0000160#endif
161#endif
162#if MICROPY_PY_SYS
Damien Georgecbf76742015-11-27 13:38:15 +0000163 { MP_ROM_QSTR(MP_QSTR_sys), MP_ROM_PTR(&mp_module_sys) },
Damien George78d702c2014-12-09 16:19:48 +0000164#endif
165#if MICROPY_PY_GC && MICROPY_ENABLE_GC
Damien Georgecbf76742015-11-27 13:38:15 +0000166 { MP_ROM_QSTR(MP_QSTR_gc), MP_ROM_PTR(&mp_module_gc) },
Damien George78d702c2014-12-09 16:19:48 +0000167#endif
Damien George27cc0772016-04-22 22:52:33 +0000168#if MICROPY_PY_THREAD
169 { MP_ROM_QSTR(MP_QSTR__thread), MP_ROM_PTR(&mp_module_thread) },
170#endif
Damien George78d702c2014-12-09 16:19:48 +0000171
172 // extmod modules
173
Damien George596a3fe2016-05-10 10:54:25 +0100174#if MICROPY_PY_UERRNO
175 { MP_ROM_QSTR(MP_QSTR_uerrno), MP_ROM_PTR(&mp_module_uerrno) },
176#endif
Damien George78d702c2014-12-09 16:19:48 +0000177#if MICROPY_PY_UCTYPES
Damien Georgecbf76742015-11-27 13:38:15 +0000178 { MP_ROM_QSTR(MP_QSTR_uctypes), MP_ROM_PTR(&mp_module_uctypes) },
Damien George78d702c2014-12-09 16:19:48 +0000179#endif
180#if MICROPY_PY_UZLIB
Damien Georgecbf76742015-11-27 13:38:15 +0000181 { MP_ROM_QSTR(MP_QSTR_uzlib), MP_ROM_PTR(&mp_module_uzlib) },
Damien George78d702c2014-12-09 16:19:48 +0000182#endif
183#if MICROPY_PY_UJSON
Damien Georgecbf76742015-11-27 13:38:15 +0000184 { MP_ROM_QSTR(MP_QSTR_ujson), MP_ROM_PTR(&mp_module_ujson) },
Damien George78d702c2014-12-09 16:19:48 +0000185#endif
186#if MICROPY_PY_URE
Damien Georgecbf76742015-11-27 13:38:15 +0000187 { MP_ROM_QSTR(MP_QSTR_ure), MP_ROM_PTR(&mp_module_ure) },
Damien George78d702c2014-12-09 16:19:48 +0000188#endif
189#if MICROPY_PY_UHEAPQ
Damien Georgecbf76742015-11-27 13:38:15 +0000190 { MP_ROM_QSTR(MP_QSTR_uheapq), MP_ROM_PTR(&mp_module_uheapq) },
Damien George78d702c2014-12-09 16:19:48 +0000191#endif
Paul Sokolovskyd02f6a92016-12-22 00:29:32 +0300192#if MICROPY_PY_UTIMEQ
193 { MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&mp_module_utimeq) },
194#endif
Damien George78d702c2014-12-09 16:19:48 +0000195#if MICROPY_PY_UHASHLIB
Damien Georgecbf76742015-11-27 13:38:15 +0000196 { MP_ROM_QSTR(MP_QSTR_uhashlib), MP_ROM_PTR(&mp_module_uhashlib) },
Damien George78d702c2014-12-09 16:19:48 +0000197#endif
198#if MICROPY_PY_UBINASCII
Damien Georgecbf76742015-11-27 13:38:15 +0000199 { MP_ROM_QSTR(MP_QSTR_ubinascii), MP_ROM_PTR(&mp_module_ubinascii) },
Damien George78d702c2014-12-09 16:19:48 +0000200#endif
Paul Sokolovskya58a91e2016-01-17 12:10:28 +0200201#if MICROPY_PY_URANDOM
202 { MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&mp_module_urandom) },
203#endif
Paul Sokolovsky8f5bc3f2016-11-20 23:49:45 +0300204#if MICROPY_PY_USELECT
205 { MP_ROM_QSTR(MP_QSTR_uselect), MP_ROM_PTR(&mp_module_uselect) },
206#endif
Paul Sokolovskyaaa88672015-10-06 18:10:00 +0300207#if MICROPY_PY_USSL
Damien Georgecbf76742015-11-27 13:38:15 +0000208 { MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) },
Paul Sokolovskyaaa88672015-10-06 18:10:00 +0300209#endif
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300210#if MICROPY_PY_LWIP
Damien Georgecbf76742015-11-27 13:38:15 +0000211 { MP_ROM_QSTR(MP_QSTR_lwip), MP_ROM_PTR(&mp_module_lwip) },
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300212#endif
Paul Sokolovsky24342dd2016-03-24 19:14:12 +0200213#if MICROPY_PY_WEBSOCKET
214 { MP_ROM_QSTR(MP_QSTR_websocket), MP_ROM_PTR(&mp_module_websocket) },
215#endif
Paul Sokolovsky25d0f7d2016-04-29 00:52:52 +0300216#if MICROPY_PY_WEBREPL
217 { MP_ROM_QSTR(MP_QSTR__webrepl), MP_ROM_PTR(&mp_module_webrepl) },
218#endif
Damien George53ad6812016-04-08 11:08:37 +0100219#if MICROPY_PY_FRAMEBUF
220 { MP_ROM_QSTR(MP_QSTR_framebuf), MP_ROM_PTR(&mp_module_framebuf) },
221#endif
Paul Sokolovsky337111b2016-06-15 00:52:45 +0300222#if MICROPY_PY_BTREE
223 { MP_ROM_QSTR(MP_QSTR_btree), MP_ROM_PTR(&mp_module_btree) },
224#endif
Damien George78d702c2014-12-09 16:19:48 +0000225
226 // extra builtin modules as defined by a port
227 MICROPY_PORT_BUILTIN_MODULES
228};
229
Damien George9de91912017-01-20 10:21:30 +1100230MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table);
231
232#if MICROPY_MODULE_WEAK_LINKS
233STATIC const mp_rom_map_elem_t mp_builtin_module_weak_links_table[] = {
234 MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS
235};
236
237MP_DEFINE_CONST_MAP(mp_builtin_module_weak_links_map, mp_builtin_module_weak_links_table);
238#endif
Damien George78d702c2014-12-09 16:19:48 +0000239
Damien Georgecaac5422014-03-25 14:18:18 +0000240// returns MP_OBJ_NULL if not found
241mp_obj_t mp_module_get(qstr module_name) {
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200242 mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
Damien George0c36da02014-03-08 15:24:39 +0000243 // lookup module
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200244 mp_map_elem_t *el = mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
Damien George0c36da02014-03-08 15:24:39 +0000245
Damien George7efc5b32014-04-05 22:36:42 +0100246 if (el == NULL) {
247 // module not found, look for builtin module names
Damien George78d702c2014-12-09 16:19:48 +0000248 el = mp_map_lookup((mp_map_t*)&mp_builtin_module_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
Damien George7efc5b32014-04-05 22:36:42 +0100249 if (el == NULL) {
250 return MP_OBJ_NULL;
251 }
Damien George3c9c3682015-09-15 14:56:13 +0100252
253 if (MICROPY_MODULE_BUILTIN_INIT) {
254 // look for __init__ and call it if it exists
255 mp_obj_t dest[2];
256 mp_load_method_maybe(el->value, MP_QSTR___init__, dest);
257 if (dest[0] != MP_OBJ_NULL) {
258 mp_call_method_n_kw(0, 0, dest);
259 // register module so __init__ is not called again
260 mp_module_register(module_name, el->value);
261 }
262 }
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200263 }
Damien George0c36da02014-03-08 15:24:39 +0000264
Damien George7efc5b32014-04-05 22:36:42 +0100265 // module found, return it
266 return el->value;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200267}
268
Damien George50912e72015-01-20 11:55:10 +0000269void mp_module_register(qstr qst, mp_obj_t module) {
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200270 mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
271 mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
Damien George28708622014-01-02 21:30:26 +0000272}