mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
GH-127682: Only call __iter__
once in generator expressions. (GH-132351)
This commit is contained in:
parent
bc0b94b30c
commit
d87e7f3529
5 changed files with 27 additions and 15 deletions
|
@ -268,6 +268,28 @@ class GeneratorTest(unittest.TestCase):
|
|||
#This should not raise
|
||||
loop()
|
||||
|
||||
def test_genexpr_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], list(i for i in C()))
|
||||
|
||||
|
||||
class ModifyUnderlyingIterableTest(unittest.TestCase):
|
||||
iterables = [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue