uasyncio.core: Don't pass any callback args to add_reader/add_writer.

Instead, capture all needed param in closure. Due do bug in MicroPython's
handling of default args to lambdas, helper function is used.
pull/63/head
Paul Sokolovsky 2015-12-09 00:38:55 +02:00
rodzic ff27e3c8ba
commit 460dd59b16
1 zmienionych plików z 10 dodań i 2 usunięć

Wyświetl plik

@ -44,6 +44,12 @@ class EventLoop:
log.debug("Sleeping for: %s", delay)
time.sleep(delay)
# Workaround bug for in uPy - default args for lambda's are not supported
def lambda_helper(self, cb):
def lmb(cb=cb):
self.call_soon(cb)
return lmb
def run_forever(self):
while True:
if self.q:
@ -78,10 +84,12 @@ class EventLoop:
elif isinstance(ret, IORead):
# self.add_reader(ret.obj.fileno(), lambda self, c, f: self.call_soon(c, f), self, cb, ret.obj)
# self.add_reader(ret.obj.fileno(), lambda c, f: self.call_soon(c, f), cb, ret.obj)
self.add_reader(arg.fileno(), lambda cb, f: self.call_soon(cb, f), cb, arg)
# self.add_reader(arg.fileno(), lambda cb: self.call_soon(cb), cb)
self.add_reader(arg.fileno(), self.lambda_helper(cb))
continue
elif isinstance(ret, IOWrite):
self.add_writer(arg.fileno(), lambda cb, f: self.call_soon(cb, f), cb, arg)
# self.add_writer(arg.fileno(), lambda cb: self.call_soon(cb), cb)
self.add_writer(arg.fileno(), self.lambda_helper(cb))
continue
elif isinstance(ret, IOReadDone):
self.remove_reader(arg.fileno())