Skip to content

Commit f66ea31

Browse files
committed
py/objmodule: Add builtin deinit functionality.
Signed-off-by: Andrew Leech <[email protected]>
1 parent 838f212 commit f66ea31

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

py/objmodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ static const mp_module_delegation_entry_t mp_builtin_module_delegation_table[] =
170170
};
171171
#endif
172172

173+
#if MICROPY_MODULE_BUILTIN_INIT
174+
void mp_module_builtin_deinit() {
175+
// Iterate through builtin modules. Run the __del__ function if defined.
176+
for (size_t i = 0; i < mp_builtin_module_map.alloc; i++) {
177+
if (mp_builtin_module_map.table[i].key != MP_OBJ_NULL) {
178+
mp_obj_t dest[2];
179+
mp_load_method_maybe(mp_builtin_module_map.table[i].value, MP_QSTR___del__, dest);
180+
if (dest[0] != MP_OBJ_NULL) {
181+
mp_call_method_n_kw(0, 0, dest);
182+
}
183+
}
184+
}
185+
}
186+
#endif
187+
173188
// Attempts to find (and initialise) a built-in, otherwise returns
174189
// MP_OBJ_NULL.
175190
mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) {

py/objmodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
extern const mp_map_t mp_builtin_module_map;
3838
extern const mp_map_t mp_builtin_extensible_module_map;
3939

40+
void mp_module_builtin_deinit();
4041
mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible);
4142

4243
void mp_module_generic_attr(qstr attr, mp_obj_t *dest, const uint16_t *keys, mp_obj_t *values);

0 commit comments

Comments
 (0)