Skip to content

Commit 89d4524

Browse files
committed
Add mp_obj_module_register
* Add function to load static modules. * Use module_register to pyb module.
1 parent 4b2b7ce commit 89d4524

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ typedef struct _mp_obj_module_t {
456456
extern const mp_obj_type_t mp_type_module;
457457
mp_obj_t mp_obj_new_module(qstr module_name);
458458
mp_obj_t mp_obj_module_get(qstr module_name);
459+
void mp_obj_module_register(qstr qstr, mp_obj_t module); //use for loading statically allocated modules
459460
struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in);
460461

461462
// staticmethod and classmethod types; defined here so we can make const versions

py/objmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ mp_obj_t mp_obj_module_get(qstr module_name) {
8282
return MP_OBJ_NULL;
8383
}
8484

85+
void mp_obj_module_register(qstr qstr, mp_obj_t module)
86+
{
87+
mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
88+
}
89+
8590
mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in) {
8691
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_module));
8792
mp_obj_module_t *self = self_in;

stm/main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ int main(void) {
272272
rt_store_name(MP_QSTR_help, rt_make_function_n(0, pyb_help));
273273
rt_store_name(MP_QSTR_open, rt_make_function_n(2, pyb_io_open));
274274

275-
// we pre-import the pyb module
276-
// probably shouldn't do this, so we are compatible with CPython
277-
rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
275+
// load the pyb module
276+
mp_obj_module_register(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
278277

279278
// check if user switch held (initiates reset of filesystem)
280279
bool reset_filesystem = false;

0 commit comments

Comments
 (0)