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>
This commit is contained in:
Jeff Epler
2025-09-26 10:23:06 -05:00
committed by Damien George
parent 653f7784d7
commit 41284577ca
6 changed files with 37 additions and 8 deletions

View File

@@ -79,7 +79,9 @@ static void mp_help_print_modules(void) {
mp_obj_t list = mp_obj_new_list(0, NULL);
mp_help_add_from_map(list, &mp_builtin_module_map);
#if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
mp_help_add_from_map(list, &mp_builtin_extensible_module_map);
#endif
#if MICROPY_MODULE_FROZEN
extern const char mp_frozen_names[];

View File

@@ -403,6 +403,7 @@ static mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
// all the locations in sys.path.
stat = stat_top_level(level_mod_name, &path);
#if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
// If filesystem failed, now try and see if it matches an extensible
// built-in module.
if (stat == MP_IMPORT_STAT_NO_EXIST) {
@@ -411,6 +412,7 @@ static mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
return module_obj;
}
}
#endif
} else {
DEBUG_printf("Searching for sub-module\n");
@@ -646,11 +648,13 @@ mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) {
if (module_obj != MP_OBJ_NULL) {
return module_obj;
}
#if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
// Now try as an extensible built-in (e.g. `time`).
module_obj = mp_module_get_builtin(module_name_qstr, true);
if (module_obj != MP_OBJ_NULL) {
return module_obj;
}
#endif
// Couldn't find the module, so fail
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE

View File

@@ -85,19 +85,25 @@ def generate_module_table_header(modules):
)
)
# There should always be at least one module (__main__ in runtime.c)
assert mod_defs
print("\n#define MICROPY_REGISTERED_MODULES \\")
for mod_def in sorted(mod_defs):
print(" {mod_def} \\".format(mod_def=mod_def))
print("// MICROPY_REGISTERED_MODULES")
print("\n#define MICROPY_REGISTERED_EXTENSIBLE_MODULES \\")
# There are not necessarily any extensible modules (e.g., bare-arm or minimal x86)
print("\n#define MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES ", len(extensible_mod_defs))
for mod_def in sorted(extensible_mod_defs):
print(" {mod_def} \\".format(mod_def=mod_def))
if extensible_mod_defs:
print("\n#define MICROPY_REGISTERED_EXTENSIBLE_MODULES \\")
print("// MICROPY_REGISTERED_EXTENSIBLE_MODULES")
for mod_def in sorted(extensible_mod_defs):
print(" {mod_def} \\".format(mod_def=mod_def))
print("// MICROPY_REGISTERED_EXTENSIBLE_MODULES")
def generate_module_delegations(delegations):

View File

@@ -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;
}

View File

@@ -35,7 +35,10 @@
#endif
extern const mp_map_t mp_builtin_module_map;
#if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
extern const mp_map_t mp_builtin_extensible_module_map;
#endif
mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible);

View File

@@ -162,8 +162,11 @@ static bool test_qstr(mp_obj_t obj, qstr name) {
return dest[0] != MP_OBJ_NULL;
} else {
// try builtin module
return mp_map_lookup((mp_map_t *)&mp_builtin_module_map, MP_OBJ_NEW_QSTR(name), MP_MAP_LOOKUP) ||
mp_map_lookup((mp_map_t *)&mp_builtin_extensible_module_map, MP_OBJ_NEW_QSTR(name), MP_MAP_LOOKUP);
return mp_map_lookup((mp_map_t *)&mp_builtin_module_map, MP_OBJ_NEW_QSTR(name), MP_MAP_LOOKUP)
#if MICROPY_HAVE_REGISTERED_EXTENSIBLE_MODULES
|| mp_map_lookup((mp_map_t *)&mp_builtin_extensible_module_map, MP_OBJ_NEW_QSTR(name), MP_MAP_LOOKUP)
#endif
;
}
}