1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-04 21:48:17 +00:00
trezor-firmware/src/trezor/utils.py

55 lines
1.1 KiB
Python
Raw Normal View History

2016-04-26 23:32:57 +00:00
import sys
import gc
2017-06-14 16:47:38 +00:00
from trezorutils import halt, memcpy
2016-04-27 19:07:37 +00:00
def _gf():
yield
type_gen = type(_gf())
def _unimport_func(func):
def inner(*args, **kwargs):
mods = set(sys.modules)
try:
ret = func(*args, **kwargs)
finally:
2016-12-12 14:20:20 +00:00
for mod in sys.modules:
if mod not in mods:
del sys.modules[mod]
gc.collect()
return ret
return inner
def _unimport_gen(genfunc):
async def inner(*args, **kwargs):
2016-04-26 23:32:57 +00:00
mods = set(sys.modules)
try:
ret = await genfunc(*args, **kwargs)
finally:
2016-12-12 14:20:20 +00:00
for mod in sys.modules:
if mod not in mods:
del sys.modules[mod]
gc.collect()
2016-04-26 23:32:57 +00:00
return ret
return inner
2016-06-09 14:31:53 +00:00
def unimport(func):
if isinstance(func, type_gen):
return _unimport_gen(func)
else:
return _unimport_func(func)
def chunks(items, size):
for i in range(0, len(items), size):
yield items[i:i + size]
2016-11-23 13:46:55 +00:00
def ensure(cond):
if not cond:
raise AssertionError()