Skip to content

Commit 3356b5e

Browse files
committed
py/objmodule: Support delegating failed attr lookups.
This commit adds generic support for mutable module attributes on built in modules, by adding support for an optional hook function for module attribute lookup. If a module wants to support additional attribute load/ store/delete (beyond what is in the constant, globals dict) then it should add at the very end of its globals dict MP_MODULE_ATTR_DELEGATION_ENTRY(). This should point to a custom function which will handle any additional attributes. The mp_module_generic_attr() function is provided as a helper function for additional attributes: it requires an array of qstrs (terminated in MP_QSTRnull) and a corresponding array of objects (with a 1-1 mapping between qstrs and objects). If the qstr is found in the array then the corresponding object is loaded/stored/deleted. Signed-off-by: Damien George <damien@micropython.org>
1 parent d470c5a commit 3356b5e

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎py/mpconfig.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,12 @@ typedef double mp_float_t;
822822
#define MICROPY_STREAMS_POSIX_API (0)
823823
#endif
824824

825+
// Whether modules can use MP_MODULE_ATTR_DELEGATION_ENTRY() to delegate failed
826+
// attribute lookups.
827+
#ifndef MICROPY_MODULE_ATTR_DELEGATION
828+
#define MICROPY_MODULE_ATTR_DELEGATION (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
829+
#endif
830+
825831
// Whether to call __init__ when importing builtin modules for the first time
826832
#ifndef MICROPY_MODULE_BUILTIN_INIT
827833
#define MICROPY_MODULE_BUILTIN_INIT (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)

‎py/objmodule.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ STATIC void module_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kin
6262
mp_printf(print, "<module '%s'>", module_name);
6363
}
6464

65+
STATIC void module_attr_try_delegation(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
66+
#if MICROPY_MODULE_ATTR_DELEGATION
67+
// Delegate lookup to a module's custom attr method (found in last lot of globals dict).
68+
mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
69+
mp_map_t *map = &self->globals->map;
70+
if (map->table[map->alloc - 1].key == MP_OBJ_NEW_QSTR(MP_QSTRnull)) {
71+
((mp_attr_fun_t)MP_OBJ_TO_PTR(map->table[map->alloc - 1].value))(self_in, attr, dest);
72+
}
73+
#else
74+
(void)self_in;
75+
(void)attr;
76+
(void)dest;
77+
#endif
78+
}
79+
6580
STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
6681
mp_obj_module_t *self = MP_OBJ_TO_PTR(self_in);
6782
if (dest[0] == MP_OBJ_NULL) {
@@ -74,8 +89,12 @@ STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
7489
elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___getattr__), MP_MAP_LOOKUP);
7590
if (elem != NULL) {
7691
dest[0] = mp_call_function_1(elem->value, MP_OBJ_NEW_QSTR(attr));
92+
} else {
93+
module_attr_try_delegation(self_in, attr, dest);
7794
}
7895
#endif
96+
} else {
97+
module_attr_try_delegation(self_in, attr, dest);
7998
}
8099
} else {
81100
// delete/store attribute
@@ -91,6 +110,7 @@ STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
91110
#endif
92111
{
93112
// can't delete or store to fixed map
113+
module_attr_try_delegation(self_in, attr, dest);
94114
return;
95115
}
96116
}
@@ -319,3 +339,19 @@ STATIC void mp_module_call_init(mp_obj_t module_name, mp_obj_t module_obj) {
319339
}
320340
}
321341
#endif
342+
343+
void mp_module_generic_attr(qstr attr, mp_obj_t *dest, const uint16_t *keys, mp_obj_t *values) {
344+
for (size_t i = 0; keys[i] != MP_QSTRnull; ++i) {
345+
if (attr == keys[i]) {
346+
if (dest[0] == MP_OBJ_NULL) {
347+
// load attribute (MP_OBJ_NULL returned for deleted items)
348+
dest[0] = values[i];
349+
} else {
350+
// delete or store (delete stores MP_OBJ_NULL)
351+
values[i] = dest[1];
352+
dest[0] = MP_OBJ_NULL; // indicate success
353+
}
354+
return;
355+
}
356+
}
357+
}

‎py/objmodule.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@
2828

2929
#include "py/obj.h"
3030

31+
// Place at the very end of a module's globals_table.
32+
#define MP_MODULE_ATTR_DELEGATION_ENTRY(ptr) { MP_ROM_QSTR(MP_QSTRnull), MP_ROM_PTR(ptr) }
33+
3134
extern const mp_map_t mp_builtin_module_map;
3235

3336
mp_obj_t mp_module_get_loaded_or_builtin(qstr module_name);
3437
#if MICROPY_MODULE_WEAK_LINKS
3538
mp_obj_t mp_module_get_builtin(qstr module_name);
3639
#endif
3740

41+
void mp_module_generic_attr(qstr attr, mp_obj_t *dest, const uint16_t *keys, mp_obj_t *values);
42+
3843
#endif // MICROPY_INCLUDED_PY_OBJMODULE_H

0 commit comments

Comments
 (0)