From 8dc2ec1513e90a8d23394f1c4ec3a07c4e057610 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 30 Mar 2016 21:01:26 +0300 Subject: [PATCH] Issue #26492: Added additional tests for exhausted iterators of mutable sequences. --- Lib/test/list_tests.py | 11 +++++++++++ Lib/test/test_bytes.py | 2 ++ Lib/test/test_iter.py | 11 +++++++++++ 3 files changed, 24 insertions(+) diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index 1adfc75b77a..f20fdc0a5f0 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -593,3 +593,14 @@ class CommonTest(seq_tests.CommonTest): def __iter__(self): raise KeyboardInterrupt 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])) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 1bd3a1ed9c4..01ba5e51c82 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -17,6 +17,7 @@ import unittest import test.support import test.string_tests import test.buffer_tests +import test.list_tests from test.support import bigaddrspacetest, MAX_Py_ssize_t @@ -1323,6 +1324,7 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase): b[:] = data self.assertEqual(list(it), []) + test_exhausted_iterator = test.list_tests.CommonTest.test_exhausted_iterator class AssortedBytesTest(unittest.TestCase): # diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index 54ddbaa5fdf..a91670b4a11 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -190,6 +190,17 @@ class TestCase(unittest.TestCase): self.assertTrue(isinstance(it, collections.abc.Iterator)) self.assertEqual(list(it), []) + 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 def test_new_style_iter_class(self): class IterClass(object):