2016-04-26 23:32:57 +00:00
|
|
|
import sys
|
2016-09-26 14:23:48 +00:00
|
|
|
import gc
|
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
from trezorutils import halt, memcpy
|
2016-04-27 19:07:37 +00:00
|
|
|
|
2017-06-14 17:34:23 +00:00
|
|
|
|
|
|
|
def _gf():
|
|
|
|
yield
|
|
|
|
type_gen = type(_gf())
|
2016-04-28 05:44:15 +00:00
|
|
|
|
2016-09-21 12:24:12 +00:00
|
|
|
|
|
|
|
def _unimport_func(func):
|
2016-04-28 05:44:15 +00:00
|
|
|
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]
|
2016-09-26 14:23:48 +00:00
|
|
|
gc.collect()
|
2016-04-28 05:44:15 +00:00
|
|
|
return ret
|
|
|
|
return inner
|
|
|
|
|
2016-09-21 12:24:12 +00:00
|
|
|
|
2017-06-14 17:34:23 +00:00
|
|
|
def _unimport_gen(genfunc):
|
2016-09-26 14:23:48 +00:00
|
|
|
async def inner(*args, **kwargs):
|
2016-04-26 23:32:57 +00:00
|
|
|
mods = set(sys.modules)
|
2016-04-28 05:44:15 +00:00
|
|
|
try:
|
2016-09-26 14:23:48 +00:00
|
|
|
ret = await genfunc(*args, **kwargs)
|
2016-04-28 05:44:15 +00:00
|
|
|
finally:
|
2016-12-12 14:20:20 +00:00
|
|
|
for mod in sys.modules:
|
|
|
|
if mod not in mods:
|
|
|
|
del sys.modules[mod]
|
2016-09-26 14:23:48 +00:00
|
|
|
gc.collect()
|
2016-04-26 23:32:57 +00:00
|
|
|
return ret
|
|
|
|
return inner
|
2016-06-09 14:31:53 +00:00
|
|
|
|
2016-09-21 12:24:12 +00:00
|
|
|
|
|
|
|
def unimport(func):
|
2017-06-14 17:34:23 +00:00
|
|
|
if isinstance(func, type_gen):
|
|
|
|
return _unimport_gen(func)
|
2016-09-21 12:24:12 +00:00
|
|
|
else:
|
|
|
|
return _unimport_func(func)
|
|
|
|
|
|
|
|
|
2017-06-14 17:34:23 +00:00
|
|
|
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()
|