diff options
Diffstat (limited to 'tests/basics/async_for2.py')
| -rw-r--r-- | tests/basics/async_for2.py | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/tests/basics/async_for2.py b/tests/basics/async_for2.py index add74dd38..d7b97cb38 100644 --- a/tests/basics/async_for2.py +++ b/tests/basics/async_for2.py @@ -1,19 +1,19 @@ # test waiting within "async for" __anext__ function -import sys -if sys.implementation.name in ('micropython', 'circuitpython'): - # uPy allows normal generators to be awaitables - coroutine = lambda f: f -else: - import types - coroutine = types.coroutine - -@coroutine -def f(x): - print('f start:', x) - yield x + 1 - yield x + 2 - return x + 3 +# uPy allows normal generators to be awaitables. +# CircuitPython does not. +# In CircuitPython you need to have an __await__ method on an awaitable like in CPython; +# and like in CPython, generators do not have __await__. + +class Awaitable: + def __init__(self, x): + self.x = x + + def __await__(self): + print('f start:', self.x) + yield self.x + 1 + yield self.x + 2 + return self.x + 3 class ARange: def __init__(self, high): @@ -27,7 +27,7 @@ class ARange: async def __anext__(self): print('anext') - print('f returned:', await f(20)) + print('f returned:', await Awaitable(20)) if self.cur < self.high: val = self.cur self.cur += 1 |
