Skip to content

Commit d1239f7

Browse files
committed
Add test and news entry
1 parent f41324e commit d1239f7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Lib/test/test_generators.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,28 @@ def loop():
268268
#This should not raise
269269
loop()
270270

271+
def test_genexpr_only_calls_dunder_iter_once(self):
272+
273+
class Iterator:
274+
275+
def __init__(self):
276+
self.val = 0
277+
278+
def __next__(self):
279+
if self.val == 2:
280+
raise StopIteration
281+
self.val += 1
282+
return self.val
283+
284+
# No __iter__ method
285+
286+
class C:
287+
288+
def __iter__(self):
289+
return Iterator()
290+
291+
self.assertEqual([1,2], list(i for i in C()))
292+
271293

272294
class ModifyUnderlyingIterableTest(unittest.TestCase):
273295
iterables = [
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
No longer call ``__iter__`` twice when creating and executing a generator
2+
expression. Creating a generator expression with non-interable will only
3+
raise when the generator expression is exeuted. This brings the behavior of
4+
generator expressions in line with other generators

0 commit comments

Comments
 (0)