Skip to content

Commit d04629b

Browse files
[3.14] GH-135171: Revert async generator expressions behavior
1 parent 081421a commit d04629b

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

Lib/test/test_asyncgen.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,13 @@ async def run():
18351835
res = self.loop.run_until_complete(run())
18361836
self.assertEqual(res, [i * 2 for i in range(1, 10)])
18371837

1838+
def test_async_gen_expression_incorrect(self):
1839+
err_msg_async = "'async for' requires an object with " \
1840+
"__aiter__ method, got .*"
1841+
1842+
with self.assertRaisesRegex(TypeError, err_msg_async):
1843+
(x async for x in None)
1844+
18381845
def test_asyncgen_nonstarted_hooks_are_cancellable(self):
18391846
# See https://bugs.python.org/issue38013
18401847
messages = []
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reverts the behavior of async generator expressions when created with object
2+
w/o __aiter__ method to the pre-3.13 behavior of raising a TypeError.

Python/codegen.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4544,9 +4544,9 @@ codegen_async_comprehension_generator(compiler *c, location loc,
45444544
else {
45454545
/* Sub-iter - calculate on the fly */
45464546
VISIT(c, expr, gen->iter);
4547+
ADDOP(c, LOC(gen->iter), GET_AITER);
45474548
}
45484549
}
4549-
ADDOP(c, LOC(gen->iter), GET_AITER);
45504550

45514551
USE_LABEL(c, start);
45524552
/* Runtime will push a block here, so we need to account for that */
@@ -4776,7 +4776,6 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
47764776
location loc = LOC(e);
47774777

47784778
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
4779-
int is_sync_genexpr = type == COMP_GENEXP && !outermost->is_async;
47804779
if (is_inlined) {
47814780
VISIT(c, expr, outermost->iter);
47824781
if (push_inlined_comprehension_state(c, loc, entry, &inline_state)) {
@@ -4853,8 +4852,13 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
48534852
Py_CLEAR(co);
48544853

48554854
VISIT(c, expr, outermost->iter);
4856-
if (is_sync_genexpr) {
4857-
ADDOP(c, loc, GET_ITER);
4855+
if (type == COMP_GENEXP) {
4856+
if (outermost->is_async) {
4857+
ADDOP(c, loc, GET_AITER);
4858+
}
4859+
else {
4860+
ADDOP(c, loc, GET_ITER);
4861+
}
48584862
}
48594863
ADDOP_I(c, loc, CALL, 0);
48604864

0 commit comments

Comments
 (0)