1# test waiting within "async for" __anext__ function
2
3try:
4    import usys as sys
5except ImportError:
6    import sys
7if sys.implementation.name == 'micropython':
8    # uPy allows normal generators to be awaitables
9    coroutine = lambda f: f
10else:
11    import types
12    coroutine = types.coroutine
13
14@coroutine
15def f(x):
16    print('f start:', x)
17    yield x + 1
18    yield x + 2
19    return x + 3
20
21class ARange:
22    def __init__(self, high):
23        print('init')
24        self.cur = 0
25        self.high = high
26
27    def __aiter__(self):
28        print('aiter')
29        return self
30
31    async def __anext__(self):
32        print('anext')
33        print('f returned:', await f(20))
34        if self.cur < self.high:
35            val = self.cur
36            self.cur += 1
37            return val
38        else:
39            raise StopAsyncIteration
40
41async def coro():
42    async for x in ARange(4):
43        print('x', x)
44
45o = coro()
46try:
47    while True:
48        print('coro yielded:', o.send(None))
49except StopIteration:
50    print('finished')
51