objmodule.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2019 Damien P. George
  7. * Copyright (c) 2014-2015 Paul Sokolovsky
  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. #include <stdlib.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include "py/bc.h"
  31. #include "py/objmodule.h"
  32. #include "py/runtime.h"
  33. #include "py/builtin.h"
  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. const char *module_name = "";
  38. mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_MAP_LOOKUP);
  39. if (elem != NULL) {
  40. module_name = mp_obj_str_get_str(elem->value);
  41. }
  42. #if MICROPY_PY___FILE__
  43. // If we store __file__ to imported modules then try to lookup this
  44. // symbol to give more information about the module.
  45. elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP);
  46. if (elem != NULL) {
  47. mp_printf(print, "<module '%s' from '%s'>", module_name, mp_obj_str_get_str(elem->value));
  48. return;
  49. }
  50. #endif
  51. mp_printf(print, "<module '%s'>", module_name);
  52. }
  53. static void module_attr_try_delegation(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
  54. static void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  55. mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
  56. if (dest[0] == MP_OBJ_NULL) {
  57. // load attribute
  58. mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
  59. if (elem != NULL) {
  60. dest[0] = elem->value;
  61. #if MICROPY_CPYTHON_COMPAT
  62. } else if (attr == MP_QSTR___dict__) {
  63. dest[0] = MP_OBJ_FROM_PTR(self->globals);
  64. #endif
  65. #if MICROPY_MODULE_GETATTR
  66. } else if (attr != MP_QSTR___getattr__) {
  67. elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___getattr__), MP_MAP_LOOKUP);
  68. if (elem != NULL) {
  69. dest[0] = mp_call_function_1(elem->value, MP_OBJ_NEW_QSTR(attr));
  70. } else {
  71. module_attr_try_delegation(self_in, attr, dest);
  72. }
  73. #endif
  74. } else {
  75. module_attr_try_delegation(self_in, attr, dest);
  76. }
  77. } else {
  78. // delete/store attribute
  79. mp_obj_dict_t *dict = self->globals;
  80. if (dict->map.is_fixed) {
  81. #if MICROPY_CAN_OVERRIDE_BUILTINS
  82. if (dict == &mp_module_builtins_globals) {
  83. if (MP_STATE_VM(mp_module_builtins_override_dict) == NULL) {
  84. MP_STATE_VM(mp_module_builtins_override_dict) = MP_OBJ_TO_PTR(mp_obj_new_dict(1));
  85. }
  86. dict = MP_STATE_VM(mp_module_builtins_override_dict);
  87. } else
  88. #endif
  89. {
  90. // can't delete or store to fixed map
  91. module_attr_try_delegation(self_in, attr, dest);
  92. return;
  93. }
  94. }
  95. if (dest[1] == MP_OBJ_NULL) {
  96. // delete attribute
  97. mp_obj_dict_delete(MP_OBJ_FROM_PTR(dict), MP_OBJ_NEW_QSTR(attr));
  98. } else {
  99. // store attribute
  100. mp_obj_dict_store(MP_OBJ_FROM_PTR(dict), MP_OBJ_NEW_QSTR(attr), dest[1]);
  101. }
  102. dest[0] = MP_OBJ_NULL; // indicate success
  103. }
  104. }
  105. MP_DEFINE_CONST_OBJ_TYPE(
  106. mp_type_module,
  107. MP_QSTR_module,
  108. MP_TYPE_FLAG_NONE,
  109. print, module_print,
  110. attr, module_attr
  111. );
  112. mp_obj_t mp_obj_new_module(qstr module_name) {
  113. mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
  114. 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);
  115. // We could error out if module already exists, but let C extensions
  116. // add new members to existing modules.
  117. if (el->value != MP_OBJ_NULL) {
  118. return el->value;
  119. }
  120. // create new module object
  121. mp_module_context_t *o = m_new_obj(mp_module_context_t);
  122. o->module.base.type = &mp_type_module;
  123. o->module.globals = MP_OBJ_TO_PTR(mp_obj_new_dict(MICROPY_MODULE_DICT_SIZE));
  124. // store __name__ entry in the module
  125. mp_obj_dict_store(MP_OBJ_FROM_PTR(o->module.globals), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(module_name));
  126. // store the new module into the slot in the global dict holding all modules
  127. el->value = MP_OBJ_FROM_PTR(o);
  128. // return the new module
  129. return MP_OBJ_FROM_PTR(o);
  130. }
  131. /******************************************************************************/
  132. // Global module table and related functions
  133. static const mp_rom_map_elem_t mp_builtin_module_table[] = {
  134. // built-in modules declared with MP_REGISTER_MODULE()
  135. MICROPY_REGISTERED_MODULES
  136. };
  137. MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table);
  138. static const mp_rom_map_elem_t mp_builtin_extensible_module_table[] = {
  139. // built-in modules declared with MP_REGISTER_EXTENSIBLE_MODULE()
  140. MICROPY_REGISTERED_EXTENSIBLE_MODULES
  141. };
  142. MP_DEFINE_CONST_MAP(mp_builtin_extensible_module_map, mp_builtin_extensible_module_table);
  143. #if MICROPY_MODULE_ATTR_DELEGATION && defined(MICROPY_MODULE_DELEGATIONS)
  144. typedef struct _mp_module_delegation_entry_t {
  145. mp_rom_obj_t mod;
  146. mp_attr_fun_t fun;
  147. } mp_module_delegation_entry_t;
  148. static const mp_module_delegation_entry_t mp_builtin_module_delegation_table[] = {
  149. // delegation entries declared with MP_REGISTER_MODULE_DELEGATION()
  150. MICROPY_MODULE_DELEGATIONS
  151. };
  152. #endif
  153. // Attempts to find (and initialise) a built-in, otherwise returns
  154. // MP_OBJ_NULL.
  155. mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) {
  156. mp_map_elem_t *elem = mp_map_lookup((mp_map_t *)(extensible ? &mp_builtin_extensible_module_map : &mp_builtin_module_map), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
  157. if (!elem) {
  158. #if MICROPY_PY_SYS
  159. // Special case for sys, which isn't extensible but can always be
  160. // imported with the alias `usys`.
  161. if (module_name == MP_QSTR_usys) {
  162. return MP_OBJ_FROM_PTR(&mp_module_sys);
  163. }
  164. #endif
  165. if (extensible) {
  166. // At this point we've already tried non-extensible built-ins, the
  167. // filesystem, and now extensible built-ins. No match, so fail
  168. // the import.
  169. return MP_OBJ_NULL;
  170. }
  171. // We're trying to match a non-extensible built-in (i.e. before trying
  172. // the filesystem), but if the user is importing `ufoo`, _and_ `foo`
  173. // is an extensible module, then allow it as a way of forcing the
  174. // built-in. Essentially, this makes it as if all the extensible
  175. // built-ins also had non-extensible aliases named `ufoo`. Newer code
  176. // should be using sys.path to force the built-in, but this retains
  177. // the old behaviour of the u-prefix being used to force a built-in
  178. // import.
  179. size_t module_name_len;
  180. const char *module_name_str = (const char *)qstr_data(module_name, &module_name_len);
  181. if (module_name_str[0] != 'u') {
  182. return MP_OBJ_NULL;
  183. }
  184. elem = mp_map_lookup((mp_map_t *)&mp_builtin_extensible_module_map, MP_OBJ_NEW_QSTR(qstr_from_strn(module_name_str + 1, module_name_len - 1)), MP_MAP_LOOKUP);
  185. if (!elem) {
  186. return MP_OBJ_NULL;
  187. }
  188. }
  189. #if MICROPY_MODULE_BUILTIN_INIT
  190. // If found, it's a newly loaded built-in, so init it. This can run
  191. // multiple times, so the module must ensure that it handles being
  192. // initialised multiple times.
  193. mp_obj_t dest[2];
  194. mp_load_method_maybe(elem->value, MP_QSTR___init__, dest);
  195. if (dest[0] != MP_OBJ_NULL) {
  196. mp_call_method_n_kw(0, 0, dest);
  197. }
  198. #endif
  199. return elem->value;
  200. }
  201. static void module_attr_try_delegation(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  202. #if MICROPY_MODULE_ATTR_DELEGATION && defined(MICROPY_MODULE_DELEGATIONS)
  203. // Delegate lookup to a module's custom attr method.
  204. size_t n = MP_ARRAY_SIZE(mp_builtin_module_delegation_table);
  205. for (size_t i = 0; i < n; ++i) {
  206. if (*(mp_obj_t *)(&mp_builtin_module_delegation_table[i].mod) == self_in) {
  207. mp_builtin_module_delegation_table[i].fun(self_in, attr, dest);
  208. break;
  209. }
  210. }
  211. #else
  212. (void)self_in;
  213. (void)attr;
  214. (void)dest;
  215. #endif
  216. }
  217. void mp_module_generic_attr(qstr attr, mp_obj_t *dest, const uint16_t *keys, mp_obj_t *values) {
  218. for (size_t i = 0; keys[i] != MP_QSTRnull; ++i) {
  219. if (attr == keys[i]) {
  220. if (dest[0] == MP_OBJ_NULL) {
  221. // load attribute (MP_OBJ_NULL returned for deleted items)
  222. dest[0] = values[i];
  223. } else {
  224. // delete or store (delete stores MP_OBJ_NULL)
  225. values[i] = dest[1];
  226. dest[0] = MP_OBJ_NULL; // indicate success
  227. }
  228. return;
  229. }
  230. }
  231. }