mirror of
https://github.com/micropython/micropython.git
synced 2025-08-13 06:01:56 +02:00
py/objmodule: Implement PEP 562's __getattr__ for modules.
Configurable via MICROPY_MODULE_GETATTR, disabled by default. Among other things __getattr__ for modules can help to build lazy loading / code unloading at runtime.
This commit is contained in:
committed by
Damien George
parent
a527313382
commit
454cca6016
@@ -61,6 +61,13 @@ STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
||||
mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
|
||||
if (elem != NULL) {
|
||||
dest[0] = elem->value;
|
||||
#if MICROPY_MODULE_GETATTR
|
||||
} else if (attr != MP_QSTR___getattr__) {
|
||||
elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___getattr__), MP_MAP_LOOKUP);
|
||||
if (elem != NULL) {
|
||||
dest[0] = mp_call_function_1(elem->value, MP_OBJ_NEW_QSTR(attr));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
// delete/store attribute
|
||||
|
Reference in New Issue
Block a user