summaryrefslogtreecommitdiff
path: root/py/objmodule.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@unpythonic.net>2025-09-26 10:23:06 -0500
committerDamien George <damien@micropython.org>2025-10-04 16:22:32 +1000
commit41284577ca389c463a3e69302de8ad0edaf97f24 (patch)
treee588263e8bc49c14c5c9cbb04e00e69b6a558492 /py/objmodule.c
parent653f7784d7e566531ed1678486631c6c89aeedb7 (diff)
py/makemoduledefs.py: Avoid empty extensible module lists.
An empty array is a C extension supported by clang & GCC but not MSVC. This also saves a bit of code size if there are no extensible modules. Fixes issue #18141. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
Diffstat (limited to 'py/objmodule.c')
-rw-r--r--py/objmodule.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/py/objmodule.c b/py/objmodule.c
index 5ee2f7dc8..8c26cc55a 100644
--- a/py/objmodule.c
+++ b/py/objmodule.c
@@ -152,11 +152,13 @@ static const mp_rom_map_elem_t mp_builtin_module_table[] = {
};
MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table);
+#if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
static const mp_rom_map_elem_t mp_builtin_extensible_module_table[] = {
// built-in modules declared with MP_REGISTER_EXTENSIBLE_MODULE()
MICROPY_REGISTERED_EXTENSIBLE_MODULES
};
MP_DEFINE_CONST_MAP(mp_builtin_extensible_module_map, mp_builtin_extensible_module_table);
+#endif
#if MICROPY_MODULE_ATTR_DELEGATION && defined(MICROPY_MODULE_DELEGATIONS)
typedef struct _mp_module_delegation_entry_t {
@@ -173,7 +175,13 @@ static const mp_module_delegation_entry_t mp_builtin_module_delegation_table[] =
// Attempts to find (and initialise) a built-in, otherwise returns
// MP_OBJ_NULL.
mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) {
- mp_map_elem_t *elem = mp_map_lookup((mp_map_t *)(extensible ? &mp_builtin_extensible_module_map : &mp_builtin_module_map), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
+ #if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
+ const mp_map_t *map = extensible ? &mp_builtin_extensible_module_map : &mp_builtin_module_map;
+ #else
+ const mp_map_t *map = &mp_builtin_module_map;
+ #endif
+ mp_map_elem_t *elem = mp_map_lookup((mp_map_t *)map, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
+
if (!elem) {
#if MICROPY_PY_SYS
// Special case for sys, which isn't extensible but can always be
@@ -183,6 +191,7 @@ mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) {
}
#endif
+ #if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
if (extensible) {
// At this point we've already tried non-extensible built-ins, the
// filesystem, and now extensible built-ins. No match, so fail
@@ -204,6 +213,8 @@ mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) {
return MP_OBJ_NULL;
}
elem = mp_map_lookup((mp_map_t *)&mp_builtin_extensible_module_map, MP_OBJ_NEW_QSTR(qstr_from_strn(module_name_str + 1, module_name_len - 1)), MP_MAP_LOOKUP);
+ #endif
+
if (!elem) {
return MP_OBJ_NULL;
}