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

adopt wire dispatching, log removed modules

This commit is contained in:
Jan Pochyla 2016-09-26 16:23:48 +02:00 committed by Pavol Rusnak
parent e9c7a67c73
commit 883b392fdb
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
3 changed files with 23 additions and 22 deletions

View File

@ -9,5 +9,5 @@ def get_protobuf_type_name(wire_type):
def get_protobuf_type(wire_type):
name = get_protobuf_type_name(wire_type)
module = __import__('.%s' % name, globals(), locals(), (name,), 1)
module = __import__('.%s' % name, None, None, (name,), 1)
return getattr(module, name)

View File

@ -1,4 +1,7 @@
import sys
import gc
from trezor import log
type_gen = type((lambda: (yield))())
type_genfunc = type((lambda: (yield)))
@ -11,19 +14,25 @@ def _unimport_func(func):
ret = func(*args, **kwargs)
finally:
for to_remove in set(sys.modules) - mods:
print('removing module', to_remove)
# log.info(__name__, 'removing module %s', to_remove)
del sys.modules[to_remove]
gc.collect()
return ret
return inner
def _unimport_genfunc(genfunc):
def inner(*args, **kwargs):
async def inner(*args, **kwargs):
mods = set(sys.modules)
try:
ret = yield from genfunc(*args, **kwargs)
ret = await genfunc(*args, **kwargs)
finally:
for to_remove in set(sys.modules) - mods:
print('removing module', to_remove)
# log.info(__name__, 'removing module %s', to_remove)
del sys.modules[to_remove]
gc.collect()
return ret
return inner
@ -35,14 +44,6 @@ def unimport(func):
return _unimport_func(func)
def coroutine(func):
def inner(*args, **kwargs):
gen = func(*args, **kwargs)
gen.send(None)
return gen
return inner
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]

View File

@ -3,9 +3,9 @@ from .wire_codec import parse_report, REP_MARKER_OPEN, REP_MARKER_CLOSE
def dispatch_reports_by_session(handlers,
open_handler,
close_handler,
fallback_handler):
open_callback,
close_callback,
unknown_callback):
'''
Consumes reports adhering to the wire codec and dispatches the report
payloads by between the passed handlers.
@ -16,21 +16,21 @@ def dispatch_reports_by_session(handlers,
if marker == REP_MARKER_OPEN:
log.debug(__name__, 'request for new session')
open_handler.send(session_id)
open_callback()
continue
elif marker == REP_MARKER_CLOSE:
log.debug(__name__, 'request for closing session %d', session_id)
close_handler.send(session_id)
close_callback(session_id)
continue
elif session_id in handlers:
log.debug(__name__, 'report on session %d', session_id)
handler = handlers[session_id]
else:
elif session_id not in handlers:
log.debug(__name__, 'report on unknown session %d', session_id)
handler = fallback_handler
unknown_callback(session_id, report_data)
continue
log.debug(__name__, 'report on session %d', session_id)
handler = handlers[session_id]
try:
handler.send(report_data)