8000 py/compile: Don't await __aiter__ special method in async-for. · codemee/micropython@37e1b5c · GitHub
[go: up one dir, main page]

Skip to content

Commit 37e1b5c

Browse files
jonathanhoggdpgeorge
authored andcommitted
py/compile: Don't await __aiter__ special method in async-for.
MicroPython's original implementation of __aiter__ was correct for an earlier (provisional) version of PEP492 (CPython 3.5), where __aiter__ was an async-def function. But that changed in the final version of PEP492 (in CPython 3.5.2) where the function was changed to a normal one. See https://www.python.org/dev/peps/pep-0492/#why-aiter-does-not-return-an-awaitable See also the note at the end of this subsection in the docs: https://docs.python.org/3.5/reference/datamodel.html#asynchronous-iterators And for completeness the BPO: https://bugs.python.org/issue27243 To be consistent with the Python spec as it stands today (and now that PEP492 is final) this commit changes MicroPython's behaviour to match CPython: __aiter__ should return an async-iterable object, but is not itself awaitable. The relevant tests are updated to match. See micropython#6267.
1 parent fe7d479 commit 37e1b5c

File tree

4 files changed

+5
-9
lines changed

4 files changed

+5
-9
lines changed

py/compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,8 @@ STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns
17981798
uint try_finally_label = comp_next_label(comp);
17991799

18001800
compile_node(comp, pns->nodes[1]); // iterator
1801-
compile_await_object_method(comp, MP_QSTR___aiter__);
1801+
EMIT_ARG(load_method, MP_QSTR___aiter__, false);
1802+
EMIT_ARG(call_method, 0, 0, 0);
18021803
compile_store_id(comp, context);
18031804

18041805
START_BREAK_CONTINUE_BLOCK

tests/basics/async_for.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def __init__(self, obj):
66
print('init')
77
self._it = iter(obj)
88

9-
async def __aiter__(self):
9+
def __aiter__(self):
1010
print('aiter')
1111
return self
1212

tests/basics/async_for2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# test waiting within "async for" aiter/anext functions
1+
# test waiting within "async for" __anext__ function
22

33
import sys
44
if sys.implementation.name == 'micropython':
@@ -21,9 +21,8 @@ def __init__(self, high):
2121
self.cur = 0
2222
self.high = high
2323

24-
async def __aiter__(self):
24+
def __aiter__(self):
2525
print('aiter')
26-
print('f returned:', await f(10))
2726
return self
2827

2928
async def __anext__(self):

tests/basics/async_for2.py.exp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
init
22
aiter
3-
f start: 10
4-
coro yielded: 11
5-
coro yielded: 12
6-
f returned: 13
73
anext
84
f start: 20
95
coro yielded: 21

0 commit comments

Comments
 (0)
0