Skip to content

Commit fea3c87

Browse files
author
Adam Hrbac
committed
Implement async comprehensions
1 parent 2b9debd commit fea3c87

File tree

1 file changed

+14
-6
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler

1 file changed

+14
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/Compiler.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,10 +1517,17 @@ private Void visitComprehension(ExprTy node, String name, ComprehensionTy[] gene
15171517
addOp(GET_ITER);
15181518
}
15191519
addOp(CALL_COMPREHENSION);
1520-
if (generators[0].isAsync) {
1521-
addOp(GET_AWAITABLE);
1522-
addOp(LOAD_NONE);
1523-
addYieldFrom();
1520+
// a genexpr will create an asyncgen, which we cannot await
1521+
if (type != ComprehensionType.GENEXPR) {
1522+
for (ComprehensionTy gen: generators) {
1523+
// if we have a non-genexpr async comprehension, the call will produce a coroutine which we need to await
1524+
if (gen.isAsync) {
1525+
addOp(GET_AWAITABLE);
1526+
addOp(LOAD_NONE);
1527+
addYieldFrom();
1528+
break;
1529+
}
1530+
}
15241531
}
15251532
return null;
15261533
} finally {
@@ -1554,7 +1561,6 @@ private void visitComprehensionGenerator(ComprehensionTy[] generators, int i, Ex
15541561
unit.useNextBlock(asyncForTry);
15551562
unit.pushBlock(new BlockInfo.AsyncForLoopExit(asyncForTry, asyncForExcept));
15561563
addOp(GET_ANEXT);
1557-
addOp(GET_AWAITABLE);
15581564
addOp(LOAD_NONE);
15591565
addYieldFrom();
15601566
unit.popBlock();
@@ -1579,6 +1585,9 @@ private void visitComprehensionGenerator(ComprehensionTy[] generators, int i, Ex
15791585
collectionStackDepth++;
15801586
}
15811587
if (type == ComprehensionType.GENEXPR) {
1588+
if (generators[i].isAsync) {
1589+
addOp(ASYNCGEN_WRAP);
1590+
}
15821591
addOp(YIELD_VALUE);
15831592
addOp(RESUME_YIELD);
15841593
addOp(POP_TOP);
@@ -2137,7 +2146,6 @@ private void visitAsyncFor(StmtTy.AsyncFor node) {
21372146
unit.pushBlock(new BlockInfo.AsyncForLoop(head, end));
21382147
addOp(DUP_TOP);
21392148
addOp(GET_ANEXT);
2140-
addOp(GET_AWAITABLE);
21412149
unit.useNextBlock(loopTry);
21422150
unit.pushBlock(new BlockInfo.AsyncForLoopExit(loopTry, except));
21432151
addOp(LOAD_NONE);

0 commit comments

Comments
 (0)