FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.13] gh-128161: Remove redundant GET_ITER from list comprehension code (GH-134778) by markshannon · Pull Request #134788 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
25 changes: 25 additions & 0 deletions Lib/test/test_coroutines.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,31 @@ def c():
# before fixing, visible stack from throw would be shorter than from send.
self.assertEqual(len_send, len_throw)

def test_call_aiter_once_in_comprehension(self):

class Iterator:

def __init__(self):
self.val = 0

async def __anext__(self):
if self.val == 2:
raise StopAsyncIteration
self.val += 1
return self.val

# No __aiter__ method

class C:

def __aiter__(self):
return Iterator()

async def run():
return [i async for i in C()]

self.assertEqual(run_async(run()), ([], [1,2]))


@unittest.skipIf(
support.is_emscripten or support.is_wasi,
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_listcomps.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,28 @@ def iter_raises():
self.assertEqual(f.line[f.colno - indent : f.end_colno - indent],
expected)

def test_only_calls_dunder_iter_once(self):

class Iterator:

def __init__(self):
self.val = 0

def __next__(self):
if self.val == 2:
raise StopIteration
self.val += 1
return self.val

# No __iter__ method

class C:

def __iter__(self):
return Iterator()

self.assertEqual([1,2], [i for i in C()])

__test__ = {'doctests' : doctests}

def load_tests(loader, tests, pattern):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
No longer call ``__iter__`` twice in list comprehensions. This brings the
behavior of list comprehensions in line with other forms of iteration
19 changes: 2 additions & 17 deletions Python/compile.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5501,9 +5501,9 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
else {
/* Sub-iter - calculate on the fly */
VISIT(c, expr, gen->iter);
ADDOP(c, LOC(gen->iter), GET_AITER);
}
}
ADDOP(c, LOC(gen->iter), GET_AITER);

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

static inline int
compiler_comprehension_iter(struct compiler *c, comprehension_ty comp)
{
VISIT(c, expr, comp->iter);
if (comp->is_async) {
ADDOP(c, LOC(comp->iter), GET_AITER);
}
else {
ADDOP(c, LOC(comp->iter), GET_ITER);
}
return SUCCESS;
}

static int
compiler_comprehension(struct compiler *c, expr_ty e, int type,
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Expand All @@ -5824,9 +5811,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,

outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
if (is_inlined) {
if (compiler_comprehension_iter(c, outermost)) {
goto error;
}
VISIT(c, expr, outermost->iter);
if (push_inlined_comprehension_state(c, loc, entry, &inline_state)) {
goto error;
}
Expand Down

Back | FazBrowse Home | New Git URL