Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Delta Between Two Patch Sets: Lib/test/test_iter.py

Issue 25958: Implicit ABCs have no means of "anti-registration"
Left Patch Set: Created 4 years, 1 month ago
Right Patch Set: Created 3 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_enumerate.py ('k') | Lib/test/test_unicode.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Test iterators. 1 # Test iterators.
2 2
3 import sys 3 import sys
4 import unittest 4 import unittest
5 from test.support import run_unittest, TESTFN, unlink, cpython_only 5 from test.support import run_unittest, TESTFN, unlink, cpython_only
6 from test.support import check_free_after_iterating
6 import pickle 7 import pickle
7 import collections.abc 8 import collections.abc
8 9
9 # Test result of triple loop (too big to inline) 10 # Test result of triple loop (too big to inline)
10 TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2), 11 TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2),
11 (0, 1, 0), (0, 1, 1), (0, 1, 2), 12 (0, 1, 0), (0, 1, 1), (0, 1, 2),
12 (0, 2, 0), (0, 2, 1), (0, 2, 2), 13 (0, 2, 0), (0, 2, 1), (0, 2, 2),
13 14
14 (1, 0, 0), (1, 0, 1), (1, 0, 2), 15 (1, 0, 0), (1, 0, 1), (1, 0, 2),
15 (1, 1, 0), (1, 1, 1), (1, 1, 2), 16 (1, 1, 0), (1, 1, 1), (1, 1, 2),
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 def test_iter_class_iter(self): 154 def test_iter_class_iter(self):
154 self.check_iterator(iter(IteratingSequenceClass(10)), list(range(10))) 155 self.check_iterator(iter(IteratingSequenceClass(10)), list(range(10)))
155 156
156 # Test for loop on a sequence class without __iter__ 157 # Test for loop on a sequence class without __iter__
157 def test_seq_class_for(self): 158 def test_seq_class_for(self):
158 self.check_for_loop(SequenceClass(10), list(range(10))) 159 self.check_for_loop(SequenceClass(10), list(range(10)))
159 160
160 # Test iter() on a sequence class without __iter__ 161 # Test iter() on a sequence class without __iter__
161 def test_seq_class_iter(self): 162 def test_seq_class_iter(self):
162 self.check_iterator(iter(SequenceClass(10)), list(range(10))) 163 self.check_iterator(iter(SequenceClass(10)), list(range(10)))
164
165 def test_mutating_seq_class_iter_pickle(self):
166 orig = SequenceClass(5)
167 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
168 # initial iterator
169 itorig = iter(orig)
170 d = pickle.dumps((itorig, orig), proto)
171 it, seq = pickle.loads(d)
172 seq.n = 7
173 self.assertIs(type(it), type(itorig))
174 self.assertEqual(list(it), list(range(7)))
175
176 # running iterator
177 next(itorig)
178 d = pickle.dumps((itorig, orig), proto)
179 it, seq = pickle.loads(d)
180 seq.n = 7
181 self.assertIs(type(it), type(itorig))
182 self.assertEqual(list(it), list(range(1, 7)))
183
184 # empty iterator
185 for i in range(1, 5):
186 next(itorig)
187 d = pickle.dumps((itorig, orig), proto)
188 it, seq = pickle.loads(d)
189 seq.n = 7
190 self.assertIs(type(it), type(itorig))
191 self.assertEqual(list(it), list(range(5, 7)))
192
193 # exhausted iterator
194 self.assertRaises(StopIteration, next, itorig)
195 d = pickle.dumps((itorig, orig), proto)
196 it, seq = pickle.loads(d)
197 seq.n = 7
198 self.assertTrue(isinstance(it, collections.abc.Iterator))
199 self.assertEqual(list(it), [])
200
201 def test_mutating_seq_class_exhausted_iter(self):
202 a = SequenceClass(5)
203 exhit = iter(a)
204 empit = iter(a)
205 for x in exhit: # exhaust the iterator
206 next(empit) # not exhausted
207 a.n = 7
208 self.assertEqual(list(exhit), [])
209 self.assertEqual(list(empit), [5, 6])
210 self.assertEqual(list(a), [0, 1, 2, 3, 4, 5, 6])
163 211
164 # Test a new_style class with __iter__ but no next() method 212 # Test a new_style class with __iter__ but no next() method
165 def test_new_style_iter_class(self): 213 def test_new_style_iter_class(self):
166 class IterClass(object): 214 class IterClass(object):
167 def __iter__(self): 215 def __iter__(self):
168 return self 216 return self
169 self.assertRaises(TypeError, iter, IterClass()) 217 self.assertRaises(TypeError, iter, IterClass())
170 218
171 # Test two-argument iter() with callable instance 219 # Test two-argument iter() with callable instance
172 def test_iter_callable(self): 220 def test_iter_callable(self):
(...skipping 772 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 # Check that Overflow error is always raised 993 # Check that Overflow error is always raised
946 with self.assertRaises(OverflowError): 994 with self.assertRaises(OverflowError):
947 next(it) 995 next(it)
948 996
949 def test_iter_neg_setstate(self): 997 def test_iter_neg_setstate(self):
950 it = iter(UnlimitedSequenceClass()) 998 it = iter(UnlimitedSequenceClass())
951 it.__setstate__(-42) 999 it.__setstate__(-42)
952 self.assertEqual(next(it), 0) 1000 self.assertEqual(next(it), 0)
953 self.assertEqual(next(it), 1) 1001 self.assertEqual(next(it), 1)
954 1002
1003 def test_free_after_iterating(self):
1004 check_free_after_iterating(self, iter, SequenceClass, (0,))
1005
955 def test_error_iter(self): 1006 def test_error_iter(self):
956 for typ in (DefaultIterClass, NoIterClass): 1007 for typ in (DefaultIterClass, NoIterClass):
957 self.assertRaises(TypeError, iter, typ()) 1008 self.assertRaises(TypeError, iter, typ())
958 1009
959 1010
960 def test_main(): 1011 def test_main():
961 run_unittest(TestCase) 1012 run_unittest(TestCase)
962 1013
963 1014
964 if __name__ == "__main__": 1015 if __name__ == "__main__":
965 test_main() 1016 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+