Skip to content

Commit 1a1d11f

Browse files
committed
py/modsys: Implement sys.modules.
This for example will allow people to reload modules which didn't load successfully (e.g. due to syntax error).
1 parent 5ae3ddc commit 1a1d11f

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

py/modsys.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
185185
{ MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
186186
#endif
187187

188+
#if MICROPY_PY_SYS_MODULES
189+
{ MP_OBJ_NEW_QSTR(MP_QSTR_modules), MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
190+
#endif
188191
#if MICROPY_PY_SYS_EXC_INFO
189192
{ MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
190193
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,11 @@ typedef double mp_float_t;
704704
#define MICROPY_PY_SYS_MAXSIZE (0)
705705
#endif
706706

707+
// Whether to provide "sys.modules" dictionary
708+
#ifndef MICROPY_PY_SYS_MODULES
709+
#define MICROPY_PY_SYS_MODULES (1)
710+
#endif
711+
707712
// Whether to provide "sys.exc_info" function
708713
// Avoid enabling this, this function is Python2 heritage
709714
#ifndef MICROPY_PY_SYS_EXC_INFO

py/mpstate.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ typedef struct _mp_state_vm_t {
100100
#endif
101101
#endif
102102

103-
// map with loaded modules
104-
// TODO: expose as sys.modules
105-
mp_map_t mp_loaded_modules_map;
103+
// dictionary with loaded modules (may be exposed as sys.modules)
104+
mp_obj_dict_t mp_loaded_modules_dict;
106105

107106
// pending exception object (MP_OBJ_NULL if not pending)
108107
volatile mp_obj_t mp_pending_exception;

py/objmodule.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ const mp_obj_type_t mp_type_module = {
9595
};
9696

9797
mp_obj_t mp_obj_new_module(qstr module_name) {
98-
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);
98+
mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
99+
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);
99100
// We could error out if module already exists, but let C extensions
100101
// add new members to existing modules.
101102
if (el->value != MP_OBJ_NULL) {
@@ -200,17 +201,18 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
200201
STATIC MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table);
201202

202203
void mp_module_init(void) {
203-
mp_map_init(&MP_STATE_VM(mp_loaded_modules_map), 3);
204+
mp_obj_dict_init(&MP_STATE_VM(mp_loaded_modules_dict), 3);
204205
}
205206

206207
void mp_module_deinit(void) {
207-
mp_map_deinit(&MP_STATE_VM(mp_loaded_modules_map));
208+
//mp_map_deinit(&MP_STATE_VM(mp_loaded_modules_map));
208209
}
209210

210211
// returns MP_OBJ_NULL if not found
211212
mp_obj_t mp_module_get(qstr module_name) {
213+
mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
212214
// lookup module
213-
mp_map_elem_t *el = mp_map_lookup(&MP_STATE_VM(mp_loaded_modules_map), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
215+
mp_map_elem_t *el = mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
214216

215217
if (el == NULL) {
216218
// module not found, look for builtin module names
@@ -236,5 +238,6 @@ mp_obj_t mp_module_get(qstr module_name) {
236238
}
237239

238240
void 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;
241+
mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
242+
mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
240243
}

py/qstrdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ Q(implementation)
474474
#if MICROPY_PY_SYS_MAXSIZE
475475
Q(maxsize)
476476
#endif
477+
#if MICROPY_PY_SYS_MODULES
478+
Q(modules)
479+
#endif
477480
#if MICROPY_PY_SYS_EXC_INFO
478481
Q(exc_info)
479482
#endif

0 commit comments

Comments
 (0)