all: Use MP_REGISTER_EXTENSIBLE_MODULE for overrideable built-ins.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared 2023-06-02 12:33:25 +10:00
parent 24c02c4eb5
commit 2eba98f1e0
47 changed files with 68 additions and 48 deletions

View File

@ -203,6 +203,6 @@ const mp_obj_module_t mp_module_binascii = {
.globals = (mp_obj_dict_t *)&mp_module_binascii_globals,
};
MP_REGISTER_MODULE(MP_QSTR_binascii, mp_module_binascii);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_binascii, mp_module_binascii);
#endif // MICROPY_PY_BINASCII

View File

@ -1004,7 +1004,11 @@ const mp_obj_module_t mp_module_bluetooth = {
.globals = (mp_obj_dict_t *)&mp_module_bluetooth_globals,
};
MP_REGISTER_MODULE(MP_QSTR_bluetooth, mp_module_bluetooth);
// This module should not be extensible (as it is not a CPython standard
// library nor is it necessary to override from the filesystem), however it
// has previously been known as `ubluetooth`, so by making it extensible the
// `ubluetooth` alias will continue to work.
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_bluetooth, mp_module_bluetooth);
// Helpers

View File

@ -375,6 +375,10 @@ const mp_obj_module_t mp_module_cryptolib = {
.globals = (mp_obj_dict_t *)&mp_module_cryptolib_globals,
};
MP_REGISTER_MODULE(MP_QSTR_cryptolib, mp_module_cryptolib);
// This module should not be extensible (as it is not a CPython standard
// library nor is it necessary to override from the filesystem), however it
// has previously been known as `ucryptolib`, so by making it extensible the
// `ucryptolib` alias will continue to work.
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_cryptolib, mp_module_cryptolib);
#endif // MICROPY_PY_CRYPTOLIB

View File

@ -374,6 +374,6 @@ const mp_obj_module_t mp_module_hashlib = {
.globals = (mp_obj_dict_t *)&mp_module_hashlib_globals,
};
MP_REGISTER_MODULE(MP_QSTR_hashlib, mp_module_hashlib);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_hashlib, mp_module_hashlib);
#endif // MICROPY_PY_HASHLIB

View File

@ -118,7 +118,7 @@ const mp_obj_module_t mp_module_heapq = {
.globals = (mp_obj_dict_t *)&mp_module_heapq_globals,
};
MP_REGISTER_MODULE(MP_QSTR_heapq, mp_module_heapq);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_heapq, mp_module_heapq);
#endif
#endif // MICROPY_PY_HEAPQ

View File

@ -381,6 +381,6 @@ const mp_obj_module_t mp_module_json = {
.globals = (mp_obj_dict_t *)&mp_module_json_globals,
};
MP_REGISTER_MODULE(MP_QSTR_json, mp_module_json);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_json, mp_module_json);
#endif // MICROPY_PY_JSON

View File

@ -1802,7 +1802,7 @@ const mp_obj_module_t mp_module_lwip = {
MP_REGISTER_MODULE(MP_QSTR_lwip, mp_module_lwip);
// On LWIP-ports, this is the socket module (replaces extmod/modsocket.c).
MP_REGISTER_MODULE(MP_QSTR_socket, mp_module_lwip);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_socket, mp_module_lwip);
MP_REGISTER_ROOT_POINTER(mp_obj_t lwip_slip_stream);

View File

@ -195,6 +195,6 @@ const mp_obj_module_t mp_module_os = {
.globals = (mp_obj_dict_t *)&os_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_os, mp_module_os);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_os, mp_module_os);
#endif // MICROPY_PY_OS

View File

@ -75,6 +75,6 @@ const mp_obj_module_t mp_module_platform = {
.globals = (mp_obj_dict_t *)&modplatform_globals,
};
MP_REGISTER_MODULE(MP_QSTR_platform, mp_module_platform);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_platform, mp_module_platform);
#endif // MICROPY_PY_PLATFORM

View File

@ -255,7 +255,7 @@ const mp_obj_module_t mp_module_random = {
.globals = (mp_obj_dict_t *)&mp_module_random_globals,
};
MP_REGISTER_MODULE(MP_QSTR_random, mp_module_random);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_random, mp_module_random);
#endif
#endif // MICROPY_PY_RANDOM

View File

@ -470,7 +470,7 @@ const mp_obj_module_t mp_module_re = {
.globals = (mp_obj_dict_t *)&mp_module_re_globals,
};
MP_REGISTER_MODULE(MP_QSTR_re, mp_module_re);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_re, mp_module_re);
#endif
// Source files #include'd here to make sure they're compiled in

View File

@ -373,6 +373,6 @@ const mp_obj_module_t mp_module_select = {
.globals = (mp_obj_dict_t *)&mp_module_select_globals,
};
MP_REGISTER_MODULE(MP_QSTR_select, mp_module_select);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_select, mp_module_select);
#endif // MICROPY_PY_SELECT

View File

@ -653,6 +653,6 @@ const mp_obj_module_t mp_module_socket = {
.globals = (mp_obj_dict_t *)&mp_module_socket_globals,
};
MP_REGISTER_MODULE(MP_QSTR_socket, mp_module_socket);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_socket, mp_module_socket);
#endif // MICROPY_PY_NETWORK && MICROPY_PY_SOCKET && !MICROPY_PY_LWIP

View File

@ -357,6 +357,6 @@ const mp_obj_module_t mp_module_ssl = {
.globals = (mp_obj_dict_t *)&mp_module_ssl_globals,
};
MP_REGISTER_MODULE(MP_QSTR_ssl, mp_module_ssl);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_ssl, mp_module_ssl);
#endif // MICROPY_PY_SSL && MICROPY_SSL_AXTLS

View File

@ -507,6 +507,6 @@ const mp_obj_module_t mp_module_ssl = {
.globals = (mp_obj_dict_t *)&mp_module_ssl_globals,
};
MP_REGISTER_MODULE(MP_QSTR_ssl, mp_module_ssl);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_ssl, mp_module_ssl);
#endif // MICROPY_PY_SSL && MICROPY_SSL_MBEDTLS

View File

@ -231,6 +231,6 @@ const mp_obj_module_t mp_module_time = {
.globals = (mp_obj_dict_t *)&mp_module_time_globals,
};
MP_REGISTER_MODULE(MP_QSTR_time, mp_module_time);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_time, mp_module_time);
#endif // MICROPY_PY_TIME

View File

@ -230,6 +230,6 @@ const mp_obj_module_t mp_module_timeq = {
.globals = (mp_obj_dict_t *)&mp_module_timeq_globals,
};
MP_REGISTER_MODULE(MP_QSTR_timeq, mp_module_timeq);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_timeq, mp_module_timeq);
#endif // MICROPY_PY_TIMEQ

View File

@ -718,6 +718,8 @@ const mp_obj_module_t mp_module_uctypes = {
.globals = (mp_obj_dict_t *)&mp_module_uctypes_globals,
};
// uctypes is not a Python standard library module (hence "uctypes"
// not "ctypes") and therefore shouldn't be extensible.
MP_REGISTER_MODULE(MP_QSTR_uctypes, mp_module_uctypes);
#endif

View File

@ -311,6 +311,10 @@ const mp_obj_module_t mp_module_websocket = {
.globals = (mp_obj_dict_t *)&websocket_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_websocket, mp_module_websocket);
// This module should not be extensible (as it is not a CPython standard
// library nor is it necessary to override from the filesystem), however it
// has previously been known as `uwebsocket`, so by making it extensible the
// `uwebsocket` alias will continue to work.
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_websocket, mp_module_websocket);
#endif // MICROPY_PY_WEBSOCKET

View File

@ -224,7 +224,7 @@ const mp_obj_module_t mp_module_zlib = {
};
MP_REGISTER_MODULE(MP_QSTR_zlib, mp_module_zlib);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_zlib, mp_module_zlib);
#endif
// Source files #include'd here to make sure they're compiled in

View File

@ -214,5 +214,5 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t*)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_ROOT_POINTER(mp_obj_t machine_config_main);

View File

@ -180,4 +180,4 @@ const mp_obj_module_t mp_module_os = {
.globals = (mp_obj_dict_t*)&os_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_os, mp_module_os);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_os, mp_module_os);

View File

@ -818,4 +818,4 @@ const mp_obj_module_t mp_module_socket = {
.globals = (mp_obj_dict_t*)&mp_module_socket_globals,
};
MP_REGISTER_MODULE(MP_QSTR_socket, mp_module_socket);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_socket, mp_module_socket);

View File

@ -160,4 +160,4 @@ const mp_obj_module_t mp_module_ssl = {
.globals = (mp_obj_dict_t*)&mp_module_ssl_globals,
};
MP_REGISTER_MODULE(MP_QSTR_ssl, mp_module_ssl);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_ssl, mp_module_ssl);

View File

@ -353,6 +353,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -872,4 +872,4 @@ const mp_obj_module_t mp_module_socket = {
// Note: This port doesn't define MICROPY_PY_SOCKET or MICROPY_PY_LWIP so
// this will not conflict with the common implementation provided by
// extmod/mod{lwip,socket}.c.
MP_REGISTER_MODULE(MP_QSTR_socket, mp_module_socket);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_socket, mp_module_socket);

View File

@ -456,6 +456,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -185,6 +185,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -266,6 +266,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t*)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -197,4 +197,4 @@ const mp_obj_module_t mp_module_os = {
.globals = (mp_obj_dict_t*)&os_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_os, mp_module_os);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_os, mp_module_os);

View File

@ -48,6 +48,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -307,6 +307,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -278,6 +278,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -297,6 +297,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -471,6 +471,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -110,6 +110,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -351,6 +351,6 @@ const mp_obj_module_t mp_module_select = {
.globals = (mp_obj_dict_t *)&mp_module_select_globals,
};
MP_REGISTER_MODULE(MP_QSTR_select, mp_module_select);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_select, mp_module_select);
#endif // MICROPY_PY_SELECT_POSIX

View File

@ -707,6 +707,6 @@ const mp_obj_module_t mp_module_socket = {
.globals = (mp_obj_dict_t *)&mp_module_socket_globals,
};
MP_REGISTER_MODULE(MP_QSTR_socket, mp_module_socket);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_socket, mp_module_socket);
#endif // MICROPY_PY_SOCKET

View File

@ -89,6 +89,6 @@ const mp_obj_module_t mp_module_machine = {
.globals = (mp_obj_dict_t *)&machine_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
#endif // MICROPY_PY_MACHINE

View File

@ -473,6 +473,6 @@ const mp_obj_module_t mp_module_socket = {
.globals = (mp_obj_dict_t *)&mp_module_socket_globals,
};
MP_REGISTER_MODULE(MP_QSTR_socket, mp_module_socket);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_socket, mp_module_socket);
#endif // MICROPY_PY_SOCKET

View File

@ -380,20 +380,23 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
mp_obj_t module_obj;
if (outer_module_obj == MP_OBJ_NULL) {
// First module in the dotted-name path.
DEBUG_printf("Searching for top-level module\n");
// An import of a non-extensible built-in will always bypass the
// filesystem. e.g. `import micropython` or `import pyb`.
// filesystem. e.g. `import micropython` or `import pyb`. So try and
// match a non-extensible built-ins first.
module_obj = mp_module_get_builtin(level_mod_name, false);
if (module_obj != MP_OBJ_NULL) {
return module_obj;
}
// First module in the dotted-name; search for a directory or file
// relative to all the locations in sys.path.
// Next try the filesystem. Search for a directory or file relative to
// all the locations in sys.path.
stat = stat_top_level(level_mod_name, &path);
// TODO: If stat failed, now try extensible built-in modules.
// If filesystem failed, now try and see if it matches an extensible
// built-in module.
if (stat == MP_IMPORT_STAT_NO_EXIST) {
module_obj = mp_module_get_builtin(level_mod_name, true);
if (module_obj != MP_OBJ_NULL) {

View File

@ -40,6 +40,6 @@ const mp_obj_module_t mp_module_array = {
.globals = (mp_obj_dict_t *)&mp_module_array_globals,
};
MP_REGISTER_MODULE(MP_QSTR_array, mp_module_array);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_array, mp_module_array);
#endif

View File

@ -46,6 +46,6 @@ const mp_obj_module_t mp_module_collections = {
.globals = (mp_obj_dict_t *)&mp_module_collections_globals,
};
MP_REGISTER_MODULE(MP_QSTR_collections, mp_module_collections);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_collections, mp_module_collections);
#endif // MICROPY_PY_COLLECTIONS

View File

@ -99,7 +99,7 @@ const mp_obj_module_t mp_module_errno = {
.globals = (mp_obj_dict_t *)&mp_module_errno_globals,
};
MP_REGISTER_MODULE(MP_QSTR_errno, mp_module_errno);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_errno, mp_module_errno);
qstr mp_errno_to_str(mp_obj_t errno_val) {
#if MICROPY_PY_ERRNO_ERRORCODE

View File

@ -226,6 +226,6 @@ const mp_obj_module_t mp_module_io = {
.globals = (mp_obj_dict_t *)&mp_module_io_globals,
};
MP_REGISTER_MODULE(MP_QSTR_io, mp_module_io);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_io, mp_module_io);
#endif

View File

@ -266,6 +266,6 @@ const mp_obj_module_t mp_module_struct = {
.globals = (mp_obj_dict_t *)&mp_module_struct_globals,
};
MP_REGISTER_MODULE(MP_QSTR_struct, mp_module_struct);
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_struct, mp_module_struct);
#endif

View File

@ -294,6 +294,9 @@ const mp_obj_module_t mp_module_sys = {
.globals = (mp_obj_dict_t *)&mp_module_sys_globals,
};
// Unlike the other CPython-compatible modules, sys is not extensible from the
// filesystem. We rely on it to work so that things like sys.path are always
// available.
MP_REGISTER_MODULE(MP_QSTR_sys, mp_module_sys);
// If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is not enabled then these two lists