diff --git a/core/embed/upymod/modtrezorutils/modtrezorutils.c b/core/embed/upymod/modtrezorutils/modtrezorutils.c index 82bf6b7b6f..3edf17af84 100644 --- a/core/embed/upymod/modtrezorutils/modtrezorutils.c +++ b/core/embed/upymod/modtrezorutils/modtrezorutils.c @@ -254,6 +254,26 @@ STATIC mp_obj_t mod_trezorutils_sd_hotswap_enabled(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_sd_hotswap_enabled_obj, mod_trezorutils_sd_hotswap_enabled); +/// def presize_module(mod: module, n: int): +/// """ +/// Ensure the module's dict is preallocated to an expected size. +/// +/// This is used in modules like `trezor`, whose dict size depends not only +/// on the symbols defined in the file itself, but also on the number of +/// submodules that will be inserted into the module's namespace. +/// """ +STATIC mp_obj_t mod_trezorutils_presize_module(mp_obj_t mod, mp_obj_t n) { + if (!mp_obj_is_type(mod, &mp_type_module)) { + mp_raise_TypeError("expected module type"); + } + mp_uint_t size = trezor_obj_get_uint(n); + mp_obj_dict_t *globals = mp_obj_module_get_globals(mod); + mp_obj_dict_presize(globals, size); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorutils_presize_module_obj, + mod_trezorutils_presize_module); + #if !PYOPT #if LOG_STACK_USAGE /// def zero_unused_stack() -> None: @@ -601,6 +621,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { #endif {MP_ROM_QSTR(MP_QSTR_sd_hotswap_enabled), MP_ROM_PTR(&mod_trezorutils_sd_hotswap_enabled_obj)}, + {MP_ROM_QSTR(MP_QSTR_presize_module), + MP_ROM_PTR(&mod_trezorutils_presize_module_obj)}, // various built-in constants {MP_ROM_QSTR(MP_QSTR_SCM_REVISION), MP_ROM_PTR(&mod_trezorutils_revision_obj)}, diff --git a/core/embed/upymod/qstrdefsport.h b/core/embed/upymod/qstrdefsport.h index 23980524c5..55ff95f900 100644 --- a/core/embed/upymod/qstrdefsport.h +++ b/core/embed/upymod/qstrdefsport.h @@ -798,38 +798,6 @@ Q(Y) Q(z) Q(Z) -// generate module presizing identifiers -Q(___PRESIZE_MODULE_0) -Q(___PRESIZE_MODULE_1) -Q(___PRESIZE_MODULE_2) -Q(___PRESIZE_MODULE_3) -Q(___PRESIZE_MODULE_4) -Q(___PRESIZE_MODULE_5) -Q(___PRESIZE_MODULE_6) -Q(___PRESIZE_MODULE_7) -Q(___PRESIZE_MODULE_8) -Q(___PRESIZE_MODULE_9) -Q(___PRESIZE_MODULE_10) -Q(___PRESIZE_MODULE_11) -Q(___PRESIZE_MODULE_12) -Q(___PRESIZE_MODULE_13) -Q(___PRESIZE_MODULE_14) -Q(___PRESIZE_MODULE_15) -Q(___PRESIZE_MODULE_16) -Q(___PRESIZE_MODULE_17) -Q(___PRESIZE_MODULE_18) -Q(___PRESIZE_MODULE_19) -Q(___PRESIZE_MODULE_20) -Q(___PRESIZE_MODULE_21) -Q(___PRESIZE_MODULE_22) -Q(___PRESIZE_MODULE_23) -Q(___PRESIZE_MODULE_24) -Q(___PRESIZE_MODULE_25) -Q(___PRESIZE_MODULE_26) -Q(___PRESIZE_MODULE_27) -Q(___PRESIZE_MODULE_28) -Q(___PRESIZE_MODULE_29) - Q()) Q(() Q(<) diff --git a/core/embed/upymod/qstrdefsport.h.mako b/core/embed/upymod/qstrdefsport.h.mako index 1ba066c426..e12bafa9d6 100644 --- a/core/embed/upymod/qstrdefsport.h.mako +++ b/core/embed/upymod/qstrdefsport.h.mako @@ -108,11 +108,6 @@ Q(${letter}) Q(${letter.upper()}) % endfor -// generate module presizing identifiers -% for i in range(30): -Q(___PRESIZE_MODULE_${i}) -% endfor - Q()) Q(() Q(<) diff --git a/core/mocks/generated/trezorutils.pyi b/core/mocks/generated/trezorutils.pyi index 7da33c4078..a023f2e07c 100644 --- a/core/mocks/generated/trezorutils.pyi +++ b/core/mocks/generated/trezorutils.pyi @@ -87,6 +87,17 @@ def sd_hotswap_enabled() -> bool: """ +# upymod/modtrezorutils/modtrezorutils.c +def presize_module(mod: module, n: int): + """ + Ensure the module's dict is preallocated to an expected size. + + This is used in modules like `trezor`, whose dict size depends not only + on the symbols defined in the file itself, but also on the number of + submodules that will be inserted into the module's namespace. + """ + + # upymod/modtrezorutils/modtrezorutils.c def zero_unused_stack() -> None: """ diff --git a/core/src/main.py b/core/src/main.py index e70aab913f..5c33ef32e6 100644 --- a/core/src/main.py +++ b/core/src/main.py @@ -11,13 +11,13 @@ import trezor # trezor.utils import only C modules from trezor import utils # we need space for 30 items in the trezor module -utils.presize_module("trezor", 30) +utils.presize_module(trezor, 30) # storage imports storage.common, storage.cache and storage.device. # These import trezor, trezor.config (which is a C module), trezor.utils, and each other. import storage # we will need space for 12 items in the storage module -utils.presize_module("storage", 12) +utils.presize_module(storage, 12) if not utils.BITCOIN_ONLY: # storage.fido2 only imports C modules diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index 7571702031..c4953166f1 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -28,6 +28,7 @@ from trezorutils import ( # noqa: F401 firmware_vendor, halt, memcpy, + presize_module, reboot_to_bootloader, sd_hotswap_enabled, unit_btconly, @@ -114,20 +115,6 @@ class unimport: self.free_heap = check_free_heap(self.free_heap) -def presize_module(modname: str, size: int) -> None: - """Ensure the module's dict is preallocated to an expected size. - - This is used in modules like `trezor`, whose dict size depends not only on the - symbols defined in the file itself, but also on the number of submodules that will - be inserted into the module's namespace. - """ - module = sys.modules[modname] - for i in range(size): - setattr(module, f"___PRESIZE_MODULE_{i}", None) - for i in range(size): - delattr(module, f"___PRESIZE_MODULE_{i}") - - if __debug__: from ubinascii import hexlify diff --git a/vendor/micropython b/vendor/micropython index 476c569f35..b989de647c 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit 476c569f3512f634b7f2e356d754bc17e5348d10 +Subproject commit b989de647c772e2bf8d604fdc47eb604f5a7ad50