Skip to content

Commit 814ac0d

Browse files
authored
[3.13] gh-128161: Remove redundant GET_ITER from list comprehension code (backport of GH-134778) (GH-134788)
1 parent 3593d0d commit 814ac0d

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

Lib/test/test_coroutines.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,31 @@ def c():
22522252
# before fixing, visible stack from throw would be shorter than from send.
22532253
self.assertEqual(len_send, len_throw)
22542254

2255+
def test_call_aiter_once_in_comprehension(self):
2256+
2257+
class Iterator:
2258+
2259+
def __init__(self):
2260+
self.val = 0
2261+
2262+
async def __anext__(self):
2263+
if self.val == 2:
2264+
raise StopAsyncIteration
2265+
self.val += 1
2266+
return self.val
2267+
2268+
# No __aiter__ method
2269+
2270+
class C:
2271+
2272+
def __aiter__(self):
2273+
return Iterator()
2274+
2275+
async def run():
2276+
return [i async for i in C()]
2277+
2278+
self.assertEqual(run_async(run()), ([], [1,2]))
2279+
22552280

22562281
@unittest.skipIf(
22572282
support.is_emscripten or support.is_wasi,

Lib/test/test_listcomps.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,28 @@ def iter_raises():
750750
self.assertEqual(f.line[f.colno - indent : f.end_colno - indent],
751751
expected)
752752

753+
def test_only_calls_dunder_iter_once(self):
754+
755+
class Iterator:
756+
757+
def __init__(self):
758+
self.val = 0
759+
760+
def __next__(self):
761+
if self.val == 2:
762+
raise StopIteration
763+
self.val += 1
764+
return self.val
765+
766+
# No __iter__ method
767+
768+
class C:
769+
770+
def __iter__(self):
771+
return Iterator()
772+
773+
self.assertEqual([1,2], [i for i in C()])
774+
753775
__test__ = {'doctests' : doctests}
754776

755777
def load_tests(loader, tests, pattern):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
No longer call ``__iter__`` twice in list comprehensions. This brings the
2+
behavior of list comprehensions in line with other forms of iteration

Python/compile.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5501,9 +5501,9 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
55015501
else {
55025502
/* Sub-iter - calculate on the fly */
55035503
VISIT(c, expr, gen->iter);
5504-
ADDOP(c, LOC(gen->iter), GET_AITER);
55055504
}
55065505
}
5506+
ADDOP(c, LOC(gen->iter), GET_AITER);
55075507

55085508
USE_LABEL(c, start);
55095509
/* Runtime will push a block here, so we need to account for that */
@@ -5790,19 +5790,6 @@ pop_inlined_comprehension_state(struct compiler *c, location loc,
57905790
return SUCCESS;
57915791
}
57925792

5793-
static inline int
5794-
compiler_comprehension_iter(struct compiler *c, comprehension_ty comp)
5795-
{
5796-
VISIT(c, expr, comp->iter);
5797-
if (comp->is_async) {
5798-
ADDOP(c, LOC(comp->iter), GET_AITER);
5799-
}
5800-
else {
5801-
ADDOP(c, LOC(comp->iter), GET_ITER);
5802-
}
5803-
return SUCCESS;
5804-
}
5805-
58065793
static int
58075794
compiler_comprehension(struct compiler *c, expr_ty e, int type,
58085795
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
@@ -5824,9 +5811,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
58245811

58255812
outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
58265813
if (is_inlined) {
5827-
if (compiler_comprehension_iter(c, outermost)) {
5828-
goto error;
5829-
}
5814+
VISIT(c, expr, outermost->iter);
58305815
if (push_inlined_comprehension_state(c, loc, entry, &inline_state)) {
58315816
goto error;
58325817
}

0 commit comments

Comments
 (0)