GH-127682: Only call __iter__ once in generator expressions. (GH-132351)

This commit is contained in:
Mark Shannon 2025-04-11 09:37:22 +01:00 committed by GitHub
parent bc0b94b30c
commit d87e7f3529
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 27 additions and 15 deletions

View file

@ -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 = [