| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2252,6 +2252,31 @@ def c(): | |||
| 2252 | 2252 | # before fixing, visible stack from throw would be shorter than from send. | |
| 2253 | 2253 | self.assertEqual(len_send, len_throw) | |
| 2254 | 2254 | ||
| 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 | + | ||
| 2255 | 2280 | ||
| 2256 | 2281 | @unittest.skipIf( | |
| 2257 | 2282 | support.is_emscripten or support.is_wasi, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -750,6 +750,28 @@ def iter_raises(): | |||
| 750 | 750 | self.assertEqual(f.line[f.colno - indent : f.end_colno - indent], | |
| 751 | 751 | expected) | |
| 752 | 752 | ||
| 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 | + | ||
| 753 | 775 | __test__ = {'doctests' : doctests} | |
| 754 | 776 | ||
| 755 | 777 | def load_tests(loader, tests, pattern): | |
| Original file line number | Diff line number | Diff 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5501,9 +5501,9 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, | |||
| 5501 | 5501 | else { | |
| 5502 | 5502 | /* Sub-iter - calculate on the fly */ | |
| 5503 | 5503 | VISIT(c, expr, gen->iter); | |
| 5504 | - ADDOP(c, LOC(gen->iter), GET_AITER); | ||
| 5505 | 5504 | } | |
| 5506 | 5505 | } | |
| 5506 | + ADDOP(c, LOC(gen->iter), GET_AITER); | ||
| 5507 | 5507 | ||
| 5508 | 5508 | USE_LABEL(c, start); | |
| 5509 | 5509 | /* 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, | |||
| 5790 | 5790 | return SUCCESS; | |
| 5791 | 5791 | } | |
| 5792 | 5792 | ||
| 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 | - | ||
| 5806 | 5793 | static int | |
| 5807 | 5794 | compiler_comprehension(struct compiler *c, expr_ty e, int type, | |
| 5808 | 5795 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, | |
@@ -5824,9 +5811,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, | |||
| 5824 | 5811 | ||
| 5825 | 5812 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); | |
| 5826 | 5813 | if (is_inlined) { | |
| 5827 | - if (compiler_comprehension_iter(c, outermost)) { | ||
| 5828 | - goto error; | ||
| 5829 | - } | ||
| 5814 | + VISIT(c, expr, outermost->iter); | ||
| 5830 | 5815 | if (push_inlined_comprehension_state(c, loc, entry, &inline_state)) { | |
| 5831 | 5816 | goto error; | |
| 5832 | 5817 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments