Issue #26492: Added additional tests for exhausted iterators of mutable sequences.

This commit is contained in:
Serhiy Storchaka 2016-03-30 21:02:00 +03:00
parent 14a7d6389f
commit 0ed3891915
3 changed files with 32 additions and 0 deletions

View File

@ -532,3 +532,14 @@ class CommonTest(seq_tests.CommonTest):
def __iter__(self): def __iter__(self):
raise KeyboardInterrupt raise KeyboardInterrupt
self.assertRaises(KeyboardInterrupt, list, F()) self.assertRaises(KeyboardInterrupt, list, F())
def test_exhausted_iterator(self):
a = self.type2test([1, 2, 3])
exhit = iter(a)
empit = iter(a)
for x in exhit: # exhaust the iterator
next(empit) # not exhausted
a.append(9)
self.assertEqual(list(exhit), [])
self.assertEqual(list(empit), [9])
self.assertEqual(a, self.type2test([1, 2, 3, 9]))

View File

@ -900,6 +900,16 @@ class ByteArrayTest(BaseBytesTest):
# PyByteArray_AS_STRING() C macro. # PyByteArray_AS_STRING() C macro.
self.assertRaises(ValueError, int, bytearray(b'')) self.assertRaises(ValueError, int, bytearray(b''))
def test_exhausted_iterator(self):
a = self.type2test([1, 2, 3])
exhit = iter(a)
empit = iter(a)
for x in exhit: # exhaust the iterator
next(empit) # not exhausted
a.append(9)
self.assertEqual(list(exhit), [])
self.assertEqual(list(empit), [9])
self.assertEqual(a, self.type2test([1, 2, 3, 9]))
class AssortedBytesTest(unittest.TestCase): class AssortedBytesTest(unittest.TestCase):
# #

View File

@ -122,6 +122,17 @@ class TestCase(unittest.TestCase):
def test_seq_class_iter(self): def test_seq_class_iter(self):
self.check_iterator(iter(SequenceClass(10)), range(10)) self.check_iterator(iter(SequenceClass(10)), range(10))
def test_mutating_seq_class_exhausted_iter(self):
a = SequenceClass(5)
exhit = iter(a)
empit = iter(a)
for x in exhit: # exhaust the iterator
next(empit) # not exhausted
a.n = 7
self.assertEqual(list(exhit), [])
self.assertEqual(list(empit), [5, 6])
self.assertEqual(list(a), [0, 1, 2, 3, 4, 5, 6])
# Test a new_style class with __iter__ but no next() method # Test a new_style class with __iter__ but no next() method
def test_new_style_iter_class(self): def test_new_style_iter_class(self):
class IterClass(object): class IterClass(object):