1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 20:19:23 +00:00
trezor-firmware/src/trezor/utils.py

49 lines
1.1 KiB
Python
Raw Normal View History

2016-04-26 23:32:57 +00:00
import sys
import gc
2016-11-19 14:21:39 +00:00
from TrezorUtils import halt, memcpy
from trezor import log
2016-04-27 19:07:37 +00:00
type_gen = type((lambda: (yield))())
type_genfunc = type((lambda: (yield)))
def _unimport_func(func):
def inner(*args, **kwargs):
mods = set(sys.modules)
try:
ret = func(*args, **kwargs)
finally:
for to_remove in set(sys.modules) - mods:
2016-10-06 10:29:57 +00:00
log.debug(__name__, 'unimport %s', to_remove)
del sys.modules[to_remove]
gc.collect()
return ret
return inner
def _unimport_genfunc(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:
for to_remove in set(sys.modules) - mods:
2016-10-06 10:29:57 +00:00
log.debug(__name__, 'unimport %s', to_remove)
del sys.modules[to_remove]
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_genfunc):
return _unimport_genfunc(func)
else:
return _unimport_func(func)
2016-06-09 14:31:53 +00:00
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]