1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-16 11:28:14 +00:00

src/trezor/utils: replace unimport with explicit fns, remove parent references

This commit is contained in:
Jan Pochyla 2018-04-03 20:38:12 +02:00
parent 9b9183c4f7
commit b38d6870eb

View File

@ -4,18 +4,24 @@ import gc
from trezorutils import halt, memcpy, set_mode_unprivileged, symbol, model # noqa: F401
def unimport(genfunc):
async def inner(*args, **kwargs):
mods = set(sys.modules)
try:
ret = await genfunc(*args, **kwargs)
finally:
for mod in sys.modules:
if mod not in mods:
del sys.modules[mod]
gc.collect()
return ret
return inner
def unimport_begin():
return set(sys.modules)
def unimport_end(mods):
for mod in sys.modules:
if mod not in mods:
# remove reference from sys.modules
del sys.modules[mod]
# remove reference from the parent module
i = mod.rfind('.')
if i < 0:
continue
path = mod[:i]
name = mod[i + 1:]
delattr(sys.modules[path], name)
# collect removed modules
gc.collect()
def ensure(cond, msg=None):