Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objmodule.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython 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 
27 #include <stdlib.h>
28 #include <assert.h>
29 
30 #include "py/objmodule.h"
31 #include "py/runtime.h"
32 #include "py/builtin.h"
33 
34 STATIC void module_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
35  (void)kind;
36  mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
37 
38  const char *module_name = "";
39  mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_MAP_LOOKUP);
40  if (elem != NULL) {
41  module_name = mp_obj_str_get_str(elem->value);
42  }
43 
44 #if MICROPY_PY___FILE__
45  // If we store __file__ to imported modules then try to lookup this
46  // symbol to give more information about the module.
47  elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP);
48  if (elem != NULL) {
49  mp_printf(print, "<module '%s' from '%s'>", module_name, mp_obj_str_get_str(elem->value));
50  return;
51  }
52 #endif
53 
54  mp_printf(print, "<module '%s'>", module_name);
55 }
56 
57 STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
58  mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
59  if (dest[0] == MP_OBJ_NULL) {
60  // load attribute
61  mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
62  if (elem != NULL) {
63  dest[0] = elem->value;
64  }
65  } else {
66  // delete/store attribute
67  mp_obj_dict_t *dict = self->globals;
68  if (dict->map.is_fixed) {
69  #if MICROPY_CAN_OVERRIDE_BUILTINS
70  if (dict == &mp_module_builtins_globals) {
71  if (MP_STATE_VM(mp_module_builtins_override_dict) == NULL) {
72  MP_STATE_VM(mp_module_builtins_override_dict) = MP_OBJ_TO_PTR(mp_obj_new_dict(1));
73  }
74  dict = MP_STATE_VM(mp_module_builtins_override_dict);
75  } else
76  #endif
77  {
78  // can't delete or store to fixed map
79  return;
80  }
81  }
82  if (dest[1] == MP_OBJ_NULL) {
83  // delete attribute
85  } else {
86  // store attribute
87  // TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
89  }
90  dest[0] = MP_OBJ_NULL; // indicate success
91  }
92 }
93 
95  { &mp_type_type },
96  .name = MP_QSTR_module,
97  .print = module_print,
98  .attr = module_attr,
99 };
100 
102  mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
103  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);
104  // We could error out if module already exists, but let C extensions
105  // add new members to existing modules.
106  if (el->value != MP_OBJ_NULL) {
107  return el->value;
108  }
109 
110  // create new module object
112  o->base.type = &mp_type_module;
114 
115  // store __name__ entry in the module
116  mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(module_name));
117 
118  // store the new module into the slot in the global dict holding all modules
119  el->value = MP_OBJ_FROM_PTR(o);
120 
121  // return the new module
122  return MP_OBJ_FROM_PTR(o);
123 }
124 
127  mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
128  return self->globals;
129 }
130 
131 /******************************************************************************/
132 // Global module table and related functions
133 
135  { MP_ROM_QSTR(MP_QSTR___main__), MP_ROM_PTR(&mp_module___main__) },
136  { MP_ROM_QSTR(MP_QSTR_builtins), MP_ROM_PTR(&mp_module_builtins) },
137  { MP_ROM_QSTR(MP_QSTR_micropython), MP_ROM_PTR(&mp_module_micropython) },
138 
139 #if MICROPY_PY_ARRAY
140  { MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mp_module_array) },
141 #endif
142 #if MICROPY_PY_IO
143  { MP_ROM_QSTR(MP_QSTR_uio), MP_ROM_PTR(&mp_module_io) },
144 #endif
145 #if MICROPY_PY_COLLECTIONS
146  { MP_ROM_QSTR(MP_QSTR_ucollections), MP_ROM_PTR(&mp_module_collections) },
147 #endif
148 #if MICROPY_PY_STRUCT
149  { MP_ROM_QSTR(MP_QSTR_ustruct), MP_ROM_PTR(&mp_module_ustruct) },
150 #endif
151 
152 #if MICROPY_PY_BUILTINS_FLOAT
153 #if MICROPY_PY_MATH
154  { MP_ROM_QSTR(MP_QSTR_math), MP_ROM_PTR(&mp_module_math) },
155 #endif
156 #if MICROPY_PY_BUILTINS_COMPLEX && MICROPY_PY_CMATH
157  { MP_ROM_QSTR(MP_QSTR_cmath), MP_ROM_PTR(&mp_module_cmath) },
158 #endif
159 #endif
160 #if MICROPY_PY_SYS
161  { MP_ROM_QSTR(MP_QSTR_sys), MP_ROM_PTR(&mp_module_sys) },
162 #endif
163 #if MICROPY_PY_GC && MICROPY_ENABLE_GC
164  { MP_ROM_QSTR(MP_QSTR_gc), MP_ROM_PTR(&mp_module_gc) },
165 #endif
166 #if MICROPY_PY_THREAD
167  { MP_ROM_QSTR(MP_QSTR__thread), MP_ROM_PTR(&mp_module_thread) },
168 #endif
169 
170  // extmod modules
171 
172 #if MICROPY_PY_UERRNO
173  { MP_ROM_QSTR(MP_QSTR_uerrno), MP_ROM_PTR(&mp_module_uerrno) },
174 #endif
175 #if MICROPY_PY_UCTYPES
176  { MP_ROM_QSTR(MP_QSTR_uctypes), MP_ROM_PTR(&mp_module_uctypes) },
177 #endif
178 #if MICROPY_PY_UZLIB
179  { MP_ROM_QSTR(MP_QSTR_uzlib), MP_ROM_PTR(&mp_module_uzlib) },
180 #endif
181 #if MICROPY_PY_UJSON
182  { MP_ROM_QSTR(MP_QSTR_ujson), MP_ROM_PTR(&mp_module_ujson) },
183 #endif
184 #if MICROPY_PY_URE
185  { MP_ROM_QSTR(MP_QSTR_ure), MP_ROM_PTR(&mp_module_ure) },
186 #endif
187 #if MICROPY_PY_UHEAPQ
188  { MP_ROM_QSTR(MP_QSTR_uheapq), MP_ROM_PTR(&mp_module_uheapq) },
189 #endif
190 #if MICROPY_PY_UTIMEQ
191  { MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&mp_module_utimeq) },
192 #endif
193 #if MICROPY_PY_UHASHLIB
194  { MP_ROM_QSTR(MP_QSTR_uhashlib), MP_ROM_PTR(&mp_module_uhashlib) },
195 #endif
196 #if MICROPY_PY_UBINASCII
197  { MP_ROM_QSTR(MP_QSTR_ubinascii), MP_ROM_PTR(&mp_module_ubinascii) },
198 #endif
199 #if MICROPY_PY_URANDOM
200  { MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&mp_module_urandom) },
201 #endif
202 #if MICROPY_PY_USELECT
203  { MP_ROM_QSTR(MP_QSTR_uselect), MP_ROM_PTR(&mp_module_uselect) },
204 #endif
205 #if MICROPY_PY_USSL
206  { MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) },
207 #endif
208 #if MICROPY_PY_LWIP
209  { MP_ROM_QSTR(MP_QSTR_lwip), MP_ROM_PTR(&mp_module_lwip) },
210 #endif
211 #if MICROPY_PY_WEBSOCKET
212  { MP_ROM_QSTR(MP_QSTR_websocket), MP_ROM_PTR(&mp_module_websocket) },
213 #endif
214 #if MICROPY_PY_WEBREPL
215  { MP_ROM_QSTR(MP_QSTR__webrepl), MP_ROM_PTR(&mp_module_webrepl) },
216 #endif
217 #if MICROPY_PY_FRAMEBUF
218  { MP_ROM_QSTR(MP_QSTR_framebuf), MP_ROM_PTR(&mp_module_framebuf) },
219 #endif
220 #if MICROPY_PY_BTREE
221  { MP_ROM_QSTR(MP_QSTR_btree), MP_ROM_PTR(&mp_module_btree) },
222 #endif
223 
224  // extra builtin modules as defined by a port
226 };
227 
229 
230 #if MICROPY_MODULE_WEAK_LINKS
231 STATIC const mp_rom_map_elem_t mp_builtin_module_weak_links_table[] = {
233 };
234 
235 MP_DEFINE_CONST_MAP(mp_builtin_module_weak_links_map, mp_builtin_module_weak_links_table);
236 #endif
237 
238 // returns MP_OBJ_NULL if not found
240  mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
241  // lookup module
242  mp_map_elem_t *el = mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
243 
244  if (el == NULL) {
245  // module not found, look for builtin module names
247  if (el == NULL) {
248  return MP_OBJ_NULL;
249  }
250 
252  // look for __init__ and call it if it exists
253  mp_obj_t dest[2];
254  mp_load_method_maybe(el->value, MP_QSTR___init__, dest);
255  if (dest[0] != MP_OBJ_NULL) {
256  mp_call_method_n_kw(0, 0, dest);
257  // register module so __init__ is not called again
258  mp_module_register(module_name, el->value);
259  }
260  }
261  }
262 
263  // module found, return it
264  return el->value;
265 }
266 
267 void mp_module_register(qstr qst, mp_obj_t module) {
268  mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
269  mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
270 }
const mp_map_t mp_builtin_module_map
const mp_obj_dict_t mp_module_builtins_globals
mp_obj_t mp_obj_new_module(qstr module_name)
Definition: objmodule.c:101
const mp_obj_module_t mp_module_array
const mp_obj_module_t mp_module_lwip
const mp_obj_module_t mp_module_websocket
void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest)
Definition: runtime.c:1040
#define assert(e)
Definition: assert.h:9
const mp_map_t mp_builtin_module_weak_links_map
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
STATIC const uint8_t attr[]
Definition: unicode.c:51
mp_obj_dict_t * mp_obj_module_get_globals(mp_obj_t self_in)
Definition: objmodule.c:125
#define MICROPY_MODULE_BUILTIN_INIT
Definition: mpconfig.h:604
const mp_obj_module_t mp_module_collections
const mp_obj_module_t mp_module_ubinascii
#define MP_ROM_QSTR(q)
Definition: obj.h:241
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
const mp_obj_module_t mp_module_ussl
#define MP_OBJ_NEW_QSTR(qst)
Definition: obj.h:92
#define MP_ROM_PTR(p)
Definition: obj.h:242
const mp_obj_module_t mp_module_ustruct
mp_obj_base_t base
Definition: obj.h:814
mp_obj_t mp_module_get(qstr module_name)
Definition: objmodule.c:239
#define MP_STATE_VM(x)
Definition: mpstate.h:241
const mp_obj_module_t mp_module_ujson
size_t is_fixed
Definition: obj.h:358
#define STATIC
Definition: mpconfig.h:1178
const mp_obj_module_t mp_module___main__
Definition: runtime.c:53
mp_obj_t mp_obj_new_dict(size_t n_args)
Definition: objdict.c:584
MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table)
mp_map_elem_t * mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind)
Definition: map.c:138
const mp_obj_module_t mp_module_webrepl
const mp_obj_module_t mp_module_uhashlib
mp_print_kind_t
Definition: obj.h:412
const mp_obj_module_t mp_module_urandom
const mp_obj_module_t mp_module_io
const mp_obj_module_t mp_module_uheapq
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
const mp_obj_module_t mp_module_sys
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS
Definition: mpconfig.h:1143
#define MICROPY_MODULE_DICT_SIZE
Definition: mpconfig.h:192
size_t qstr
Definition: qstr.h:48
mp_obj_t value
Definition: obj.h:343
const mp_obj_module_t mp_module_ure
const mp_obj_module_t mp_module_micropython
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key)
Definition: objdict.c:602
const mp_obj_module_t mp_module_thread
mp_obj_dict_t * globals
Definition: obj.h:815
Definition: obj.h:356
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
const mp_obj_module_t mp_module_btree
mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args)
Definition: runtime.c:639
const mp_obj_module_t mp_module_gc
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value)
Definition: objdict.c:595
STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest)
Definition: objmodule.c:57
const mp_obj_module_t mp_module_utimeq
const char * mp_obj_str_get_str(mp_obj_t self_in)
Definition: objstr.c:2095
STATIC const mp_rom_map_elem_t mp_builtin_module_table[]
Definition: objmodule.c:134
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
const mp_obj_module_t mp_module_uerrno
const mp_obj_module_t mp_module_uzlib
void mp_module_register(qstr qst, mp_obj_t module)
Definition: objmodule.c:267
qstr name
Definition: obj.h:478
const mp_obj_module_t mp_module_cmath
mp_map_t map
Definition: obj.h:766
uint64_t mp_obj_t
Definition: obj.h:39
const mp_obj_module_t mp_module_builtins
Definition: modbuiltins.c:735
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
const mp_obj_module_t mp_module_uselect
const mp_obj_module_t mp_module_uctypes
#define m_new_obj(type)
Definition: misc.h:60
STATIC void module_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
Definition: objmodule.c:34
const mp_obj_module_t mp_module_framebuf
const mp_obj_module_t mp_module_math
#define MICROPY_PORT_BUILTIN_MODULES
Definition: mpconfigport.h:110