2016-04-26 23:32:57 +00:00
|
|
|
import sys
|
2016-09-26 14:23:48 +00:00
|
|
|
import gc
|
|
|
|
|
2016-11-19 14:21:39 +00:00
|
|
|
from TrezorUtils import halt, memcpy
|
2016-09-26 14:23:48 +00:00
|
|
|
from trezor import log
|
2016-04-27 19:07:37 +00:00
|
|
|
|
2016-04-28 05:44:15 +00:00
|
|
|
type_gen = type((lambda: (yield))())
|
2016-09-21 12:24:12 +00:00
|
|
|
type_genfunc = type((lambda: (yield)))
|
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:
|
|
|
|
for to_remove in set(sys.modules) - mods:
|
2016-10-06 10:29:57 +00:00
|
|
|
log.debug(__name__, 'unimport %s', to_remove)
|
2016-04-28 05:44:15 +00:00
|
|
|
del sys.modules[to_remove]
|
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
|
|
|
|
|
|
|
def _unimport_genfunc(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:
|
|
|
|
for to_remove in set(sys.modules) - mods:
|
2016-10-06 10:29:57 +00:00
|
|
|
log.debug(__name__, 'unimport %s', to_remove)
|
2016-04-28 05:44:15 +00:00
|
|
|
del sys.modules[to_remove]
|
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):
|
|
|
|
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]
|