blob: b3757d6a1e697b9e432fa8da6a6b66d4ae188505 [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 George28708622014-01-02 21:30:26 +000038 mp_obj_module_t *self = self_in;
Damien George377b80b2014-09-08 10:45:23 +010039
40#if MICROPY_PY___FILE__
41 // If we store __file__ to imported modules then try to lookup this
42 // symbol to give more information about the module.
43 mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP);
44 if (elem != NULL) {
Damien George044c4732015-04-11 13:03:37 +010045 mp_printf(print, "<module '%q' from '%s'>", self->name, mp_obj_str_get_str(elem->value));
Damien George377b80b2014-09-08 10:45:23 +010046 return;
47 }
48#endif
49
Damien George044c4732015-04-11 13:03:37 +010050 mp_printf(print, "<module '%q'>", self->name);
Damien George28708622014-01-02 21:30:26 +000051}
52
Damien Georgeb1bbe962015-04-01 14:10:50 +000053STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +000054 mp_obj_module_t *self = self_in;
Damien Georgeb1bbe962015-04-01 14:10:50 +000055 if (dest[0] == MP_OBJ_NULL) {
56 // load attribute
57 mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
58 if (elem != NULL) {
59 dest[0] = elem->value;
Damien George78d702c2014-12-09 16:19:48 +000060 }
Damien George1d24ea52014-04-08 21:11:49 +010061 } else {
Damien Georgeb1bbe962015-04-01 14:10:50 +000062 // delete/store attribute
63 mp_obj_dict_t *dict = self->globals;
64 if (dict->map.is_fixed) {
65 #if MICROPY_CAN_OVERRIDE_BUILTINS
66 if (dict == &mp_module_builtins_globals) {
67 if (MP_STATE_VM(mp_module_builtins_override_dict) == NULL) {
68 MP_STATE_VM(mp_module_builtins_override_dict) = mp_obj_new_dict(1);
69 }
70 dict = MP_STATE_VM(mp_module_builtins_override_dict);
71 } else
72 #endif
73 {
74 // can't delete or store to fixed map
75 return;
76 }
77 }
78 if (dest[1] == MP_OBJ_NULL) {
79 // delete attribute
80 mp_obj_dict_delete(dict, MP_OBJ_NEW_QSTR(attr));
81 } else {
82 // store attribute
83 // TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
84 mp_obj_dict_store(dict, MP_OBJ_NEW_QSTR(attr), dest[1]);
85 }
86 dest[0] = MP_OBJ_NULL; // indicate success
Damien George1d24ea52014-04-08 21:11:49 +010087 }
Damien George062478e2014-01-09 20:57:50 +000088}
89
Damien George0c36da02014-03-08 15:24:39 +000090const mp_obj_type_t mp_type_module = {
Damien Georgec5966122014-02-15 16:10:44 +000091 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000092 .name = MP_QSTR_module,
Damien George97209d32014-01-07 15:58:30 +000093 .print = module_print,
Damien Georgeb1bbe962015-04-01 14:10:50 +000094 .attr = module_attr,
Damien George28708622014-01-02 21:30:26 +000095};
96
97mp_obj_t mp_obj_new_module(qstr module_name) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +000098 mp_map_elem_t *el = mp_map_lookup(&MP_STATE_VM(mp_loaded_modules_map), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Paul Sokolovskyd720ab52014-01-20 00:03:34 +020099 // We could error out if module already exists, but let C extensions
100 // add new members to existing modules.
101 if (el->value != MP_OBJ_NULL) {
102 return el->value;
103 }
104
Damien George0c36da02014-03-08 15:24:39 +0000105 // create new module object
Damien George28708622014-01-02 21:30:26 +0000106 mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
Damien George0c36da02014-03-08 15:24:39 +0000107 o->base.type = &mp_type_module;
Damien George28708622014-01-02 21:30:26 +0000108 o->name = module_name;
Paul Sokolovsky346aacf2014-11-05 00:27:15 +0200109 o->globals = mp_obj_new_dict(MICROPY_MODULE_DICT_SIZE);
Damien George0c36da02014-03-08 15:24:39 +0000110
111 // store __name__ entry in the module
Damien George8b0535e2014-04-05 21:53:54 +0100112 mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(module_name));
Damien George0c36da02014-03-08 15:24:39 +0000113
114 // store the new module into the slot in the global dict holding all modules
115 el->value = o;
116
117 // return the new module
Damien George28708622014-01-02 21:30:26 +0000118 return o;
119}
120
Damien George8b0535e2014-04-05 21:53:54 +0100121mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in) {
Damien Georgecaac5422014-03-25 14:18:18 +0000122 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module));
123 mp_obj_module_t *self = self_in;
124 return self->globals;
125}
126
127/******************************************************************************/
128// Global module table and related functions
129
Damien Georgecbf76742015-11-27 13:38:15 +0000130STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
131 { MP_ROM_QSTR(MP_QSTR___main__), MP_ROM_PTR(&mp_module___main__) },
132 { MP_ROM_QSTR(MP_QSTR_builtins), MP_ROM_PTR(&mp_module_builtins) },
133 { MP_ROM_QSTR(MP_QSTR_micropython), MP_ROM_PTR(&mp_module_micropython) },
Damien George78d702c2014-12-09 16:19:48 +0000134
135#if MICROPY_PY_ARRAY
Damien Georgecbf76742015-11-27 13:38:15 +0000136 { MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mp_module_array) },
Damien George78d702c2014-12-09 16:19:48 +0000137#endif
138#if MICROPY_PY_IO
Damien Georgecbf76742015-11-27 13:38:15 +0000139 { MP_ROM_QSTR(MP_QSTR__io), MP_ROM_PTR(&mp_module_io) },
Damien George78d702c2014-12-09 16:19:48 +0000140#endif
141#if MICROPY_PY_COLLECTIONS
Damien Georgecbf76742015-11-27 13:38:15 +0000142 { MP_ROM_QSTR(MP_QSTR__collections), MP_ROM_PTR(&mp_module_collections) },
Damien George78d702c2014-12-09 16:19:48 +0000143#endif
144#if MICROPY_PY_STRUCT
Damien Georgecbf76742015-11-27 13:38:15 +0000145 { MP_ROM_QSTR(MP_QSTR_ustruct), MP_ROM_PTR(&mp_module_ustruct) },
Damien George78d702c2014-12-09 16:19:48 +0000146#endif
147
148#if MICROPY_PY_BUILTINS_FLOAT
149#if MICROPY_PY_MATH
Damien Georgecbf76742015-11-27 13:38:15 +0000150 { MP_ROM_QSTR(MP_QSTR_math), MP_ROM_PTR(&mp_module_math) },
Damien George78d702c2014-12-09 16:19:48 +0000151#endif
152#if MICROPY_PY_CMATH
Damien Georgecbf76742015-11-27 13:38:15 +0000153 { MP_ROM_QSTR(MP_QSTR_cmath), MP_ROM_PTR(&mp_module_cmath) },
Damien George78d702c2014-12-09 16:19:48 +0000154#endif
155#endif
156#if MICROPY_PY_SYS
Damien Georgecbf76742015-11-27 13:38:15 +0000157 { MP_ROM_QSTR(MP_QSTR_sys), MP_ROM_PTR(&mp_module_sys) },
Damien George78d702c2014-12-09 16:19:48 +0000158#endif
159#if MICROPY_PY_GC && MICROPY_ENABLE_GC
Damien Georgecbf76742015-11-27 13:38:15 +0000160 { MP_ROM_QSTR(MP_QSTR_gc), MP_ROM_PTR(&mp_module_gc) },
Damien George78d702c2014-12-09 16:19:48 +0000161#endif
162
163 // extmod modules
164
165#if MICROPY_PY_UCTYPES
Damien Georgecbf76742015-11-27 13:38:15 +0000166 { MP_ROM_QSTR(MP_QSTR_uctypes), MP_ROM_PTR(&mp_module_uctypes) },
Damien George78d702c2014-12-09 16:19:48 +0000167#endif
168#if MICROPY_PY_UZLIB
Damien Georgecbf76742015-11-27 13:38:15 +0000169 { MP_ROM_QSTR(MP_QSTR_uzlib), MP_ROM_PTR(&mp_module_uzlib) },
Damien George78d702c2014-12-09 16:19:48 +0000170#endif
171#if MICROPY_PY_UJSON
Damien Georgecbf76742015-11-27 13:38:15 +0000172 { MP_ROM_QSTR(MP_QSTR_ujson), MP_ROM_PTR(&mp_module_ujson) },
Damien George78d702c2014-12-09 16:19:48 +0000173#endif
174#if MICROPY_PY_URE
Damien Georgecbf76742015-11-27 13:38:15 +0000175 { MP_ROM_QSTR(MP_QSTR_ure), MP_ROM_PTR(&mp_module_ure) },
Damien George78d702c2014-12-09 16:19:48 +0000176#endif
177#if MICROPY_PY_UHEAPQ
Damien Georgecbf76742015-11-27 13:38:15 +0000178 { MP_ROM_QSTR(MP_QSTR_uheapq), MP_ROM_PTR(&mp_module_uheapq) },
Damien George78d702c2014-12-09 16:19:48 +0000179#endif
180#if MICROPY_PY_UHASHLIB
Damien Georgecbf76742015-11-27 13:38:15 +0000181 { MP_ROM_QSTR(MP_QSTR_uhashlib), MP_ROM_PTR(&mp_module_uhashlib) },
Damien George78d702c2014-12-09 16:19:48 +0000182#endif
183#if MICROPY_PY_UBINASCII
Damien Georgecbf76742015-11-27 13:38:15 +0000184 { MP_ROM_QSTR(MP_QSTR_ubinascii), MP_ROM_PTR(&mp_module_ubinascii) },
Damien George78d702c2014-12-09 16:19:48 +0000185#endif
Paul Sokolovsky01162182015-05-03 20:25:40 +0300186#if MICROPY_PY_MACHINE
Damien Georgecbf76742015-11-27 13:38:15 +0000187 { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) },
Paul Sokolovsky01162182015-05-03 20:25:40 +0300188#endif
Paul Sokolovskyaaa88672015-10-06 18:10:00 +0300189#if MICROPY_PY_USSL
Damien Georgecbf76742015-11-27 13:38:15 +0000190 { MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) },
Paul Sokolovskyaaa88672015-10-06 18:10:00 +0300191#endif
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300192#if MICROPY_PY_LWIP
Damien Georgecbf76742015-11-27 13:38:15 +0000193 { MP_ROM_QSTR(MP_QSTR_lwip), MP_ROM_PTR(&mp_module_lwip) },
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300194#endif
Damien George78d702c2014-12-09 16:19:48 +0000195
196 // extra builtin modules as defined by a port
197 MICROPY_PORT_BUILTIN_MODULES
198};
199
200STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table);
201
Damien Georgecaac5422014-03-25 14:18:18 +0000202void mp_module_init(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000203 mp_map_init(&MP_STATE_VM(mp_loaded_modules_map), 3);
Damien Georgecaac5422014-03-25 14:18:18 +0000204}
205
206void mp_module_deinit(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000207 mp_map_deinit(&MP_STATE_VM(mp_loaded_modules_map));
Damien Georgecaac5422014-03-25 14:18:18 +0000208}
209
210// returns MP_OBJ_NULL if not found
211mp_obj_t mp_module_get(qstr module_name) {
Damien George0c36da02014-03-08 15:24:39 +0000212 // lookup module
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000213 mp_map_elem_t *el = mp_map_lookup(&MP_STATE_VM(mp_loaded_modules_map), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
Damien George0c36da02014-03-08 15:24:39 +0000214
Damien George7efc5b32014-04-05 22:36:42 +0100215 if (el == NULL) {
216 // module not found, look for builtin module names
Damien George78d702c2014-12-09 16:19:48 +0000217 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 +0100218 if (el == NULL) {
219 return MP_OBJ_NULL;
220 }
Damien George3c9c3682015-09-15 14:56:13 +0100221
222 if (MICROPY_MODULE_BUILTIN_INIT) {
223 // look for __init__ and call it if it exists
224 mp_obj_t dest[2];
225 mp_load_method_maybe(el->value, MP_QSTR___init__, dest);
226 if (dest[0] != MP_OBJ_NULL) {
227 mp_call_method_n_kw(0, 0, dest);
228 // register module so __init__ is not called again
229 mp_module_register(module_name, el->value);
230 }
231 }
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200232 }
Damien George0c36da02014-03-08 15:24:39 +0000233
Damien George7efc5b32014-04-05 22:36:42 +0100234 // module found, return it
235 return el->value;
Paul Sokolovskyd720ab52014-01-20 00:03:34 +0200236}
237
Damien George50912e72015-01-20 11:55:10 +0000238void mp_module_register(qstr qst, mp_obj_t module) {
239 mp_map_lookup(&MP_STATE_VM(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 +0000240}