Skip to content

Commit 52c4e7c

Browse files
committed
Issue python#28008: Implement PEP 530 -- asynchronous comprehensions.
1 parent 93b2dee commit 52c4e7c

20 files changed

+613
-114
lines changed

Grammar/Grammar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ argument: ( test [comp_for] |
146146
'*' test )
147147

148148
comp_iter: comp_for | comp_if
149-
comp_for: 'for' exprlist 'in' or_test [comp_iter]
149+
comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
150150
comp_if: 'if' test_nocond [comp_iter]
151151

152152
# not used in grammar, but may appear in "node" passed from Parser to Compiler

Include/Python-ast.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ struct _comprehension {
389389
expr_ty target;
390390
expr_ty iter;
391391
asdl_seq *ifs;
392+
int is_async;
392393
};
393394

394395
enum _excepthandler_kind {ExceptHandler_kind=1};
@@ -609,9 +610,9 @@ slice_ty _Py_Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena);
609610
slice_ty _Py_ExtSlice(asdl_seq * dims, PyArena *arena);
610611
#define Index(a0, a1) _Py_Index(a0, a1)
611612
slice_ty _Py_Index(expr_ty value, PyArena *arena);
612-
#define comprehension(a0, a1, a2, a3) _Py_comprehension(a0, a1, a2, a3)
613+
#define comprehension(a0, a1, a2, a3, a4) _Py_comprehension(a0, a1, a2, a3, a4)
613614
comprehension_ty _Py_comprehension(expr_ty target, expr_ty iter, asdl_seq *
614-
ifs, PyArena *arena);
615+
ifs, int is_async, PyArena *arena);
615616
#define ExceptHandler(a0, a1, a2, a3, a4, a5) _Py_ExceptHandler(a0, a1, a2, a3, a4, a5)
616617
excepthandler_ty _Py_ExceptHandler(expr_ty type, identifier name, asdl_seq *
617618
body, int lineno, int col_offset, PyArena

Lib/lib2to3/Grammar.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ arglist: (argument ',')* (argument [',']
150150
argument: test [comp_for] | test '=' test # Really [keyword '='] test
151151

152152
comp_iter: comp_for | comp_if
153-
comp_for: 'for' exprlist 'in' testlist_safe [comp_iter]
153+
comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
154154
comp_if: 'if' old_test [comp_iter]
155155

156156
testlist1: test (',' test)*

Lib/lib2to3/tests/test_parser.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ def test_await_expr(self):
133133
await x
134134
""")
135135

136+
self.validate("""async def foo():
137+
[i async for i in b]
138+
""")
139+
140+
self.validate("""async def foo():
141+
{i for i in b
142+
async for i in a if await i
143+
for b in i}
144+
""")
145+
146+
self.validate("""async def foo():
147+
[await i for i in b if await c]
148+
""")
149+
150+
self.validate("""async def foo():
151+
[ i for i in b if c]
152+
""")
153+
136154
self.validate("""async def foo():
137155
138156
def foo(): pass

Lib/test/badsyntax_async1.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

Lib/test/badsyntax_async2.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

Lib/test/badsyntax_async3.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

Lib/test/badsyntax_async4.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

Lib/test/badsyntax_async5.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

Lib/test/badsyntax_async7.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)