Skip to content

Commit 53bd362

Browse files
[3.14] GH-135171: Bug fix, test improved
1 parent d04629b commit 53bd362

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

Lib/test/test_asyncgen.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,11 +1836,18 @@ async def run():
18361836
self.assertEqual(res, [i * 2 for i in range(1, 10)])
18371837

18381838
def test_async_gen_expression_incorrect(self):
1839+
async def ag():
1840+
yield 42
1841+
1842+
async def run(arg):
1843+
(x async for x in arg)
1844+
18391845
err_msg_async = "'async for' requires an object with " \
18401846
"__aiter__ method, got .*"
18411847

1848+
self.loop.run_until_complete(run(ag()))
18421849
with self.assertRaisesRegex(TypeError, err_msg_async):
1843-
(x async for x in None)
1850+
self.loop.run_until_complete(run(None))
18441851

18451852
def test_asyncgen_nonstarted_hooks_are_cancellable(self):
18461853
# See https://bugs.python.org/issue38013

Python/codegen.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,6 +4758,19 @@ pop_inlined_comprehension_state(compiler *c, location loc,
47584758
return SUCCESS;
47594759
}
47604760

4761+
static inline int
4762+
codegen_comprehension_iter(compiler *c, comprehension_ty comp)
4763+
{
4764+
VISIT(c, expr, comp->iter);
4765+
if (comp->is_async) {
4766+
ADDOP(c, LOC(comp->iter), GET_AITER);
4767+
}
4768+
else {
4769+
ADDOP(c, LOC(comp->iter), GET_ITER);
4770+
}
4771+
return SUCCESS;
4772+
}
4773+
47614774
static int
47624775
codegen_comprehension(compiler *c, expr_ty e, int type,
47634776
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
@@ -4777,7 +4790,9 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
47774790

47784791
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
47794792
if (is_inlined) {
4780-
VISIT(c, expr, outermost->iter);
4793+
if (codegen_comprehension_iter(c, outermost)) {
4794+
goto error;
4795+
}
47814796
if (push_inlined_comprehension_state(c, loc, entry, &inline_state)) {
47824797
goto error;
47834798
}
@@ -4851,15 +4866,10 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
48514866
}
48524867
Py_CLEAR(co);
48534868

4854-
VISIT(c, expr, outermost->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-
}
4869+
if (codegen_comprehension_iter(c, outermost)) {
4870+
goto error;
48624871
}
4872+
48634873
ADDOP_I(c, loc, CALL, 0);
48644874

48654875
if (is_async_comprehension && type != COMP_GENEXP) {

0 commit comments

Comments
 (0)