1 | n/a | # Test iterators. |
---|
2 | n/a | |
---|
3 | n/a | import sys |
---|
4 | n/a | import unittest |
---|
5 | n/a | from test.support import run_unittest, TESTFN, unlink, cpython_only |
---|
6 | n/a | from test.support import check_free_after_iterating |
---|
7 | n/a | import pickle |
---|
8 | n/a | import collections.abc |
---|
9 | n/a | |
---|
10 | n/a | # Test result of triple loop (too big to inline) |
---|
11 | n/a | TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2), |
---|
12 | n/a | (0, 1, 0), (0, 1, 1), (0, 1, 2), |
---|
13 | n/a | (0, 2, 0), (0, 2, 1), (0, 2, 2), |
---|
14 | n/a | |
---|
15 | n/a | (1, 0, 0), (1, 0, 1), (1, 0, 2), |
---|
16 | n/a | (1, 1, 0), (1, 1, 1), (1, 1, 2), |
---|
17 | n/a | (1, 2, 0), (1, 2, 1), (1, 2, 2), |
---|
18 | n/a | |
---|
19 | n/a | (2, 0, 0), (2, 0, 1), (2, 0, 2), |
---|
20 | n/a | (2, 1, 0), (2, 1, 1), (2, 1, 2), |
---|
21 | n/a | (2, 2, 0), (2, 2, 1), (2, 2, 2)] |
---|
22 | n/a | |
---|
23 | n/a | # Helper classes |
---|
24 | n/a | |
---|
25 | n/a | class BasicIterClass: |
---|
26 | n/a | def __init__(self, n): |
---|
27 | n/a | self.n = n |
---|
28 | n/a | self.i = 0 |
---|
29 | n/a | def __next__(self): |
---|
30 | n/a | res = self.i |
---|
31 | n/a | if res >= self.n: |
---|
32 | n/a | raise StopIteration |
---|
33 | n/a | self.i = res + 1 |
---|
34 | n/a | return res |
---|
35 | n/a | def __iter__(self): |
---|
36 | n/a | return self |
---|
37 | n/a | |
---|
38 | n/a | class IteratingSequenceClass: |
---|
39 | n/a | def __init__(self, n): |
---|
40 | n/a | self.n = n |
---|
41 | n/a | def __iter__(self): |
---|
42 | n/a | return BasicIterClass(self.n) |
---|
43 | n/a | |
---|
44 | n/a | class SequenceClass: |
---|
45 | n/a | def __init__(self, n): |
---|
46 | n/a | self.n = n |
---|
47 | n/a | def __getitem__(self, i): |
---|
48 | n/a | if 0 <= i < self.n: |
---|
49 | n/a | return i |
---|
50 | n/a | else: |
---|
51 | n/a | raise IndexError |
---|
52 | n/a | |
---|
53 | n/a | class UnlimitedSequenceClass: |
---|
54 | n/a | def __getitem__(self, i): |
---|
55 | n/a | return i |
---|
56 | n/a | |
---|
57 | n/a | class DefaultIterClass: |
---|
58 | n/a | pass |
---|
59 | n/a | |
---|
60 | n/a | class NoIterClass: |
---|
61 | n/a | def __getitem__(self, i): |
---|
62 | n/a | return i |
---|
63 | n/a | __iter__ = None |
---|
64 | n/a | |
---|
65 | n/a | # Main test suite |
---|
66 | n/a | |
---|
67 | n/a | class TestCase(unittest.TestCase): |
---|
68 | n/a | |
---|
69 | n/a | # Helper to check that an iterator returns a given sequence |
---|
70 | n/a | def check_iterator(self, it, seq, pickle=True): |
---|
71 | n/a | if pickle: |
---|
72 | n/a | self.check_pickle(it, seq) |
---|
73 | n/a | res = [] |
---|
74 | n/a | while 1: |
---|
75 | n/a | try: |
---|
76 | n/a | val = next(it) |
---|
77 | n/a | except StopIteration: |
---|
78 | n/a | break |
---|
79 | n/a | res.append(val) |
---|
80 | n/a | self.assertEqual(res, seq) |
---|
81 | n/a | |
---|
82 | n/a | # Helper to check that a for loop generates a given sequence |
---|
83 | n/a | def check_for_loop(self, expr, seq, pickle=True): |
---|
84 | n/a | if pickle: |
---|
85 | n/a | self.check_pickle(iter(expr), seq) |
---|
86 | n/a | res = [] |
---|
87 | n/a | for val in expr: |
---|
88 | n/a | res.append(val) |
---|
89 | n/a | self.assertEqual(res, seq) |
---|
90 | n/a | |
---|
91 | n/a | # Helper to check picklability |
---|
92 | n/a | def check_pickle(self, itorg, seq): |
---|
93 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
94 | n/a | d = pickle.dumps(itorg, proto) |
---|
95 | n/a | it = pickle.loads(d) |
---|
96 | n/a | # Cannot assert type equality because dict iterators unpickle as list |
---|
97 | n/a | # iterators. |
---|
98 | n/a | # self.assertEqual(type(itorg), type(it)) |
---|
99 | n/a | self.assertTrue(isinstance(it, collections.abc.Iterator)) |
---|
100 | n/a | self.assertEqual(list(it), seq) |
---|
101 | n/a | |
---|
102 | n/a | it = pickle.loads(d) |
---|
103 | n/a | try: |
---|
104 | n/a | next(it) |
---|
105 | n/a | except StopIteration: |
---|
106 | n/a | continue |
---|
107 | n/a | d = pickle.dumps(it, proto) |
---|
108 | n/a | it = pickle.loads(d) |
---|
109 | n/a | self.assertEqual(list(it), seq[1:]) |
---|
110 | n/a | |
---|
111 | n/a | # Test basic use of iter() function |
---|
112 | n/a | def test_iter_basic(self): |
---|
113 | n/a | self.check_iterator(iter(range(10)), list(range(10))) |
---|
114 | n/a | |
---|
115 | n/a | # Test that iter(iter(x)) is the same as iter(x) |
---|
116 | n/a | def test_iter_idempotency(self): |
---|
117 | n/a | seq = list(range(10)) |
---|
118 | n/a | it = iter(seq) |
---|
119 | n/a | it2 = iter(it) |
---|
120 | n/a | self.assertTrue(it is it2) |
---|
121 | n/a | |
---|
122 | n/a | # Test that for loops over iterators work |
---|
123 | n/a | def test_iter_for_loop(self): |
---|
124 | n/a | self.check_for_loop(iter(range(10)), list(range(10))) |
---|
125 | n/a | |
---|
126 | n/a | # Test several independent iterators over the same list |
---|
127 | n/a | def test_iter_independence(self): |
---|
128 | n/a | seq = range(3) |
---|
129 | n/a | res = [] |
---|
130 | n/a | for i in iter(seq): |
---|
131 | n/a | for j in iter(seq): |
---|
132 | n/a | for k in iter(seq): |
---|
133 | n/a | res.append((i, j, k)) |
---|
134 | n/a | self.assertEqual(res, TRIPLETS) |
---|
135 | n/a | |
---|
136 | n/a | # Test triple list comprehension using iterators |
---|
137 | n/a | def test_nested_comprehensions_iter(self): |
---|
138 | n/a | seq = range(3) |
---|
139 | n/a | res = [(i, j, k) |
---|
140 | n/a | for i in iter(seq) for j in iter(seq) for k in iter(seq)] |
---|
141 | n/a | self.assertEqual(res, TRIPLETS) |
---|
142 | n/a | |
---|
143 | n/a | # Test triple list comprehension without iterators |
---|
144 | n/a | def test_nested_comprehensions_for(self): |
---|
145 | n/a | seq = range(3) |
---|
146 | n/a | res = [(i, j, k) for i in seq for j in seq for k in seq] |
---|
147 | n/a | self.assertEqual(res, TRIPLETS) |
---|
148 | n/a | |
---|
149 | n/a | # Test a class with __iter__ in a for loop |
---|
150 | n/a | def test_iter_class_for(self): |
---|
151 | n/a | self.check_for_loop(IteratingSequenceClass(10), list(range(10))) |
---|
152 | n/a | |
---|
153 | n/a | # Test a class with __iter__ with explicit iter() |
---|
154 | n/a | def test_iter_class_iter(self): |
---|
155 | n/a | self.check_iterator(iter(IteratingSequenceClass(10)), list(range(10))) |
---|
156 | n/a | |
---|
157 | n/a | # Test for loop on a sequence class without __iter__ |
---|
158 | n/a | def test_seq_class_for(self): |
---|
159 | n/a | self.check_for_loop(SequenceClass(10), list(range(10))) |
---|
160 | n/a | |
---|
161 | n/a | # Test iter() on a sequence class without __iter__ |
---|
162 | n/a | def test_seq_class_iter(self): |
---|
163 | n/a | self.check_iterator(iter(SequenceClass(10)), list(range(10))) |
---|
164 | n/a | |
---|
165 | n/a | def test_mutating_seq_class_iter_pickle(self): |
---|
166 | n/a | orig = SequenceClass(5) |
---|
167 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
168 | n/a | # initial iterator |
---|
169 | n/a | itorig = iter(orig) |
---|
170 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
171 | n/a | it, seq = pickle.loads(d) |
---|
172 | n/a | seq.n = 7 |
---|
173 | n/a | self.assertIs(type(it), type(itorig)) |
---|
174 | n/a | self.assertEqual(list(it), list(range(7))) |
---|
175 | n/a | |
---|
176 | n/a | # running iterator |
---|
177 | n/a | next(itorig) |
---|
178 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
179 | n/a | it, seq = pickle.loads(d) |
---|
180 | n/a | seq.n = 7 |
---|
181 | n/a | self.assertIs(type(it), type(itorig)) |
---|
182 | n/a | self.assertEqual(list(it), list(range(1, 7))) |
---|
183 | n/a | |
---|
184 | n/a | # empty iterator |
---|
185 | n/a | for i in range(1, 5): |
---|
186 | n/a | next(itorig) |
---|
187 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
188 | n/a | it, seq = pickle.loads(d) |
---|
189 | n/a | seq.n = 7 |
---|
190 | n/a | self.assertIs(type(it), type(itorig)) |
---|
191 | n/a | self.assertEqual(list(it), list(range(5, 7))) |
---|
192 | n/a | |
---|
193 | n/a | # exhausted iterator |
---|
194 | n/a | self.assertRaises(StopIteration, next, itorig) |
---|
195 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
196 | n/a | it, seq = pickle.loads(d) |
---|
197 | n/a | seq.n = 7 |
---|
198 | n/a | self.assertTrue(isinstance(it, collections.abc.Iterator)) |
---|
199 | n/a | self.assertEqual(list(it), []) |
---|
200 | n/a | |
---|
201 | n/a | def test_mutating_seq_class_exhausted_iter(self): |
---|
202 | n/a | a = SequenceClass(5) |
---|
203 | n/a | exhit = iter(a) |
---|
204 | n/a | empit = iter(a) |
---|
205 | n/a | for x in exhit: # exhaust the iterator |
---|
206 | n/a | next(empit) # not exhausted |
---|
207 | n/a | a.n = 7 |
---|
208 | n/a | self.assertEqual(list(exhit), []) |
---|
209 | n/a | self.assertEqual(list(empit), [5, 6]) |
---|
210 | n/a | self.assertEqual(list(a), [0, 1, 2, 3, 4, 5, 6]) |
---|
211 | n/a | |
---|
212 | n/a | # Test a new_style class with __iter__ but no next() method |
---|
213 | n/a | def test_new_style_iter_class(self): |
---|
214 | n/a | class IterClass(object): |
---|
215 | n/a | def __iter__(self): |
---|
216 | n/a | return self |
---|
217 | n/a | self.assertRaises(TypeError, iter, IterClass()) |
---|
218 | n/a | |
---|
219 | n/a | # Test two-argument iter() with callable instance |
---|
220 | n/a | def test_iter_callable(self): |
---|
221 | n/a | class C: |
---|
222 | n/a | def __init__(self): |
---|
223 | n/a | self.i = 0 |
---|
224 | n/a | def __call__(self): |
---|
225 | n/a | i = self.i |
---|
226 | n/a | self.i = i + 1 |
---|
227 | n/a | if i > 100: |
---|
228 | n/a | raise IndexError # Emergency stop |
---|
229 | n/a | return i |
---|
230 | n/a | self.check_iterator(iter(C(), 10), list(range(10)), pickle=False) |
---|
231 | n/a | |
---|
232 | n/a | # Test two-argument iter() with function |
---|
233 | n/a | def test_iter_function(self): |
---|
234 | n/a | def spam(state=[0]): |
---|
235 | n/a | i = state[0] |
---|
236 | n/a | state[0] = i+1 |
---|
237 | n/a | return i |
---|
238 | n/a | self.check_iterator(iter(spam, 10), list(range(10)), pickle=False) |
---|
239 | n/a | |
---|
240 | n/a | # Test two-argument iter() with function that raises StopIteration |
---|
241 | n/a | def test_iter_function_stop(self): |
---|
242 | n/a | def spam(state=[0]): |
---|
243 | n/a | i = state[0] |
---|
244 | n/a | if i == 10: |
---|
245 | n/a | raise StopIteration |
---|
246 | n/a | state[0] = i+1 |
---|
247 | n/a | return i |
---|
248 | n/a | self.check_iterator(iter(spam, 20), list(range(10)), pickle=False) |
---|
249 | n/a | |
---|
250 | n/a | # Test exception propagation through function iterator |
---|
251 | n/a | def test_exception_function(self): |
---|
252 | n/a | def spam(state=[0]): |
---|
253 | n/a | i = state[0] |
---|
254 | n/a | state[0] = i+1 |
---|
255 | n/a | if i == 10: |
---|
256 | n/a | raise RuntimeError |
---|
257 | n/a | return i |
---|
258 | n/a | res = [] |
---|
259 | n/a | try: |
---|
260 | n/a | for x in iter(spam, 20): |
---|
261 | n/a | res.append(x) |
---|
262 | n/a | except RuntimeError: |
---|
263 | n/a | self.assertEqual(res, list(range(10))) |
---|
264 | n/a | else: |
---|
265 | n/a | self.fail("should have raised RuntimeError") |
---|
266 | n/a | |
---|
267 | n/a | # Test exception propagation through sequence iterator |
---|
268 | n/a | def test_exception_sequence(self): |
---|
269 | n/a | class MySequenceClass(SequenceClass): |
---|
270 | n/a | def __getitem__(self, i): |
---|
271 | n/a | if i == 10: |
---|
272 | n/a | raise RuntimeError |
---|
273 | n/a | return SequenceClass.__getitem__(self, i) |
---|
274 | n/a | res = [] |
---|
275 | n/a | try: |
---|
276 | n/a | for x in MySequenceClass(20): |
---|
277 | n/a | res.append(x) |
---|
278 | n/a | except RuntimeError: |
---|
279 | n/a | self.assertEqual(res, list(range(10))) |
---|
280 | n/a | else: |
---|
281 | n/a | self.fail("should have raised RuntimeError") |
---|
282 | n/a | |
---|
283 | n/a | # Test for StopIteration from __getitem__ |
---|
284 | n/a | def test_stop_sequence(self): |
---|
285 | n/a | class MySequenceClass(SequenceClass): |
---|
286 | n/a | def __getitem__(self, i): |
---|
287 | n/a | if i == 10: |
---|
288 | n/a | raise StopIteration |
---|
289 | n/a | return SequenceClass.__getitem__(self, i) |
---|
290 | n/a | self.check_for_loop(MySequenceClass(20), list(range(10)), pickle=False) |
---|
291 | n/a | |
---|
292 | n/a | # Test a big range |
---|
293 | n/a | def test_iter_big_range(self): |
---|
294 | n/a | self.check_for_loop(iter(range(10000)), list(range(10000))) |
---|
295 | n/a | |
---|
296 | n/a | # Test an empty list |
---|
297 | n/a | def test_iter_empty(self): |
---|
298 | n/a | self.check_for_loop(iter([]), []) |
---|
299 | n/a | |
---|
300 | n/a | # Test a tuple |
---|
301 | n/a | def test_iter_tuple(self): |
---|
302 | n/a | self.check_for_loop(iter((0,1,2,3,4,5,6,7,8,9)), list(range(10))) |
---|
303 | n/a | |
---|
304 | n/a | # Test a range |
---|
305 | n/a | def test_iter_range(self): |
---|
306 | n/a | self.check_for_loop(iter(range(10)), list(range(10))) |
---|
307 | n/a | |
---|
308 | n/a | # Test a string |
---|
309 | n/a | def test_iter_string(self): |
---|
310 | n/a | self.check_for_loop(iter("abcde"), ["a", "b", "c", "d", "e"]) |
---|
311 | n/a | |
---|
312 | n/a | # Test a directory |
---|
313 | n/a | def test_iter_dict(self): |
---|
314 | n/a | dict = {} |
---|
315 | n/a | for i in range(10): |
---|
316 | n/a | dict[i] = None |
---|
317 | n/a | self.check_for_loop(dict, list(dict.keys())) |
---|
318 | n/a | |
---|
319 | n/a | # Test a file |
---|
320 | n/a | def test_iter_file(self): |
---|
321 | n/a | f = open(TESTFN, "w") |
---|
322 | n/a | try: |
---|
323 | n/a | for i in range(5): |
---|
324 | n/a | f.write("%d\n" % i) |
---|
325 | n/a | finally: |
---|
326 | n/a | f.close() |
---|
327 | n/a | f = open(TESTFN, "r") |
---|
328 | n/a | try: |
---|
329 | n/a | self.check_for_loop(f, ["0\n", "1\n", "2\n", "3\n", "4\n"], pickle=False) |
---|
330 | n/a | self.check_for_loop(f, [], pickle=False) |
---|
331 | n/a | finally: |
---|
332 | n/a | f.close() |
---|
333 | n/a | try: |
---|
334 | n/a | unlink(TESTFN) |
---|
335 | n/a | except OSError: |
---|
336 | n/a | pass |
---|
337 | n/a | |
---|
338 | n/a | # Test list()'s use of iterators. |
---|
339 | n/a | def test_builtin_list(self): |
---|
340 | n/a | self.assertEqual(list(SequenceClass(5)), list(range(5))) |
---|
341 | n/a | self.assertEqual(list(SequenceClass(0)), []) |
---|
342 | n/a | self.assertEqual(list(()), []) |
---|
343 | n/a | |
---|
344 | n/a | d = {"one": 1, "two": 2, "three": 3} |
---|
345 | n/a | self.assertEqual(list(d), list(d.keys())) |
---|
346 | n/a | |
---|
347 | n/a | self.assertRaises(TypeError, list, list) |
---|
348 | n/a | self.assertRaises(TypeError, list, 42) |
---|
349 | n/a | |
---|
350 | n/a | f = open(TESTFN, "w") |
---|
351 | n/a | try: |
---|
352 | n/a | for i in range(5): |
---|
353 | n/a | f.write("%d\n" % i) |
---|
354 | n/a | finally: |
---|
355 | n/a | f.close() |
---|
356 | n/a | f = open(TESTFN, "r") |
---|
357 | n/a | try: |
---|
358 | n/a | self.assertEqual(list(f), ["0\n", "1\n", "2\n", "3\n", "4\n"]) |
---|
359 | n/a | f.seek(0, 0) |
---|
360 | n/a | self.assertEqual(list(f), |
---|
361 | n/a | ["0\n", "1\n", "2\n", "3\n", "4\n"]) |
---|
362 | n/a | finally: |
---|
363 | n/a | f.close() |
---|
364 | n/a | try: |
---|
365 | n/a | unlink(TESTFN) |
---|
366 | n/a | except OSError: |
---|
367 | n/a | pass |
---|
368 | n/a | |
---|
369 | n/a | # Test tuples()'s use of iterators. |
---|
370 | n/a | def test_builtin_tuple(self): |
---|
371 | n/a | self.assertEqual(tuple(SequenceClass(5)), (0, 1, 2, 3, 4)) |
---|
372 | n/a | self.assertEqual(tuple(SequenceClass(0)), ()) |
---|
373 | n/a | self.assertEqual(tuple([]), ()) |
---|
374 | n/a | self.assertEqual(tuple(()), ()) |
---|
375 | n/a | self.assertEqual(tuple("abc"), ("a", "b", "c")) |
---|
376 | n/a | |
---|
377 | n/a | d = {"one": 1, "two": 2, "three": 3} |
---|
378 | n/a | self.assertEqual(tuple(d), tuple(d.keys())) |
---|
379 | n/a | |
---|
380 | n/a | self.assertRaises(TypeError, tuple, list) |
---|
381 | n/a | self.assertRaises(TypeError, tuple, 42) |
---|
382 | n/a | |
---|
383 | n/a | f = open(TESTFN, "w") |
---|
384 | n/a | try: |
---|
385 | n/a | for i in range(5): |
---|
386 | n/a | f.write("%d\n" % i) |
---|
387 | n/a | finally: |
---|
388 | n/a | f.close() |
---|
389 | n/a | f = open(TESTFN, "r") |
---|
390 | n/a | try: |
---|
391 | n/a | self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n")) |
---|
392 | n/a | f.seek(0, 0) |
---|
393 | n/a | self.assertEqual(tuple(f), |
---|
394 | n/a | ("0\n", "1\n", "2\n", "3\n", "4\n")) |
---|
395 | n/a | finally: |
---|
396 | n/a | f.close() |
---|
397 | n/a | try: |
---|
398 | n/a | unlink(TESTFN) |
---|
399 | n/a | except OSError: |
---|
400 | n/a | pass |
---|
401 | n/a | |
---|
402 | n/a | # Test filter()'s use of iterators. |
---|
403 | n/a | def test_builtin_filter(self): |
---|
404 | n/a | self.assertEqual(list(filter(None, SequenceClass(5))), |
---|
405 | n/a | list(range(1, 5))) |
---|
406 | n/a | self.assertEqual(list(filter(None, SequenceClass(0))), []) |
---|
407 | n/a | self.assertEqual(list(filter(None, ())), []) |
---|
408 | n/a | self.assertEqual(list(filter(None, "abc")), ["a", "b", "c"]) |
---|
409 | n/a | |
---|
410 | n/a | d = {"one": 1, "two": 2, "three": 3} |
---|
411 | n/a | self.assertEqual(list(filter(None, d)), list(d.keys())) |
---|
412 | n/a | |
---|
413 | n/a | self.assertRaises(TypeError, filter, None, list) |
---|
414 | n/a | self.assertRaises(TypeError, filter, None, 42) |
---|
415 | n/a | |
---|
416 | n/a | class Boolean: |
---|
417 | n/a | def __init__(self, truth): |
---|
418 | n/a | self.truth = truth |
---|
419 | n/a | def __bool__(self): |
---|
420 | n/a | return self.truth |
---|
421 | n/a | bTrue = Boolean(True) |
---|
422 | n/a | bFalse = Boolean(False) |
---|
423 | n/a | |
---|
424 | n/a | class Seq: |
---|
425 | n/a | def __init__(self, *args): |
---|
426 | n/a | self.vals = args |
---|
427 | n/a | def __iter__(self): |
---|
428 | n/a | class SeqIter: |
---|
429 | n/a | def __init__(self, vals): |
---|
430 | n/a | self.vals = vals |
---|
431 | n/a | self.i = 0 |
---|
432 | n/a | def __iter__(self): |
---|
433 | n/a | return self |
---|
434 | n/a | def __next__(self): |
---|
435 | n/a | i = self.i |
---|
436 | n/a | self.i = i + 1 |
---|
437 | n/a | if i < len(self.vals): |
---|
438 | n/a | return self.vals[i] |
---|
439 | n/a | else: |
---|
440 | n/a | raise StopIteration |
---|
441 | n/a | return SeqIter(self.vals) |
---|
442 | n/a | |
---|
443 | n/a | seq = Seq(*([bTrue, bFalse] * 25)) |
---|
444 | n/a | self.assertEqual(list(filter(lambda x: not x, seq)), [bFalse]*25) |
---|
445 | n/a | self.assertEqual(list(filter(lambda x: not x, iter(seq))), [bFalse]*25) |
---|
446 | n/a | |
---|
447 | n/a | # Test max() and min()'s use of iterators. |
---|
448 | n/a | def test_builtin_max_min(self): |
---|
449 | n/a | self.assertEqual(max(SequenceClass(5)), 4) |
---|
450 | n/a | self.assertEqual(min(SequenceClass(5)), 0) |
---|
451 | n/a | self.assertEqual(max(8, -1), 8) |
---|
452 | n/a | self.assertEqual(min(8, -1), -1) |
---|
453 | n/a | |
---|
454 | n/a | d = {"one": 1, "two": 2, "three": 3} |
---|
455 | n/a | self.assertEqual(max(d), "two") |
---|
456 | n/a | self.assertEqual(min(d), "one") |
---|
457 | n/a | self.assertEqual(max(d.values()), 3) |
---|
458 | n/a | self.assertEqual(min(iter(d.values())), 1) |
---|
459 | n/a | |
---|
460 | n/a | f = open(TESTFN, "w") |
---|
461 | n/a | try: |
---|
462 | n/a | f.write("medium line\n") |
---|
463 | n/a | f.write("xtra large line\n") |
---|
464 | n/a | f.write("itty-bitty line\n") |
---|
465 | n/a | finally: |
---|
466 | n/a | f.close() |
---|
467 | n/a | f = open(TESTFN, "r") |
---|
468 | n/a | try: |
---|
469 | n/a | self.assertEqual(min(f), "itty-bitty line\n") |
---|
470 | n/a | f.seek(0, 0) |
---|
471 | n/a | self.assertEqual(max(f), "xtra large line\n") |
---|
472 | n/a | finally: |
---|
473 | n/a | f.close() |
---|
474 | n/a | try: |
---|
475 | n/a | unlink(TESTFN) |
---|
476 | n/a | except OSError: |
---|
477 | n/a | pass |
---|
478 | n/a | |
---|
479 | n/a | # Test map()'s use of iterators. |
---|
480 | n/a | def test_builtin_map(self): |
---|
481 | n/a | self.assertEqual(list(map(lambda x: x+1, SequenceClass(5))), |
---|
482 | n/a | list(range(1, 6))) |
---|
483 | n/a | |
---|
484 | n/a | d = {"one": 1, "two": 2, "three": 3} |
---|
485 | n/a | self.assertEqual(list(map(lambda k, d=d: (k, d[k]), d)), |
---|
486 | n/a | list(d.items())) |
---|
487 | n/a | dkeys = list(d.keys()) |
---|
488 | n/a | expected = [(i < len(d) and dkeys[i] or None, |
---|
489 | n/a | i, |
---|
490 | n/a | i < len(d) and dkeys[i] or None) |
---|
491 | n/a | for i in range(3)] |
---|
492 | n/a | |
---|
493 | n/a | f = open(TESTFN, "w") |
---|
494 | n/a | try: |
---|
495 | n/a | for i in range(10): |
---|
496 | n/a | f.write("xy" * i + "\n") # line i has len 2*i+1 |
---|
497 | n/a | finally: |
---|
498 | n/a | f.close() |
---|
499 | n/a | f = open(TESTFN, "r") |
---|
500 | n/a | try: |
---|
501 | n/a | self.assertEqual(list(map(len, f)), list(range(1, 21, 2))) |
---|
502 | n/a | finally: |
---|
503 | n/a | f.close() |
---|
504 | n/a | try: |
---|
505 | n/a | unlink(TESTFN) |
---|
506 | n/a | except OSError: |
---|
507 | n/a | pass |
---|
508 | n/a | |
---|
509 | n/a | # Test zip()'s use of iterators. |
---|
510 | n/a | def test_builtin_zip(self): |
---|
511 | n/a | self.assertEqual(list(zip()), []) |
---|
512 | n/a | self.assertEqual(list(zip(*[])), []) |
---|
513 | n/a | self.assertEqual(list(zip(*[(1, 2), 'ab'])), [(1, 'a'), (2, 'b')]) |
---|
514 | n/a | |
---|
515 | n/a | self.assertRaises(TypeError, zip, None) |
---|
516 | n/a | self.assertRaises(TypeError, zip, range(10), 42) |
---|
517 | n/a | self.assertRaises(TypeError, zip, range(10), zip) |
---|
518 | n/a | |
---|
519 | n/a | self.assertEqual(list(zip(IteratingSequenceClass(3))), |
---|
520 | n/a | [(0,), (1,), (2,)]) |
---|
521 | n/a | self.assertEqual(list(zip(SequenceClass(3))), |
---|
522 | n/a | [(0,), (1,), (2,)]) |
---|
523 | n/a | |
---|
524 | n/a | d = {"one": 1, "two": 2, "three": 3} |
---|
525 | n/a | self.assertEqual(list(d.items()), list(zip(d, d.values()))) |
---|
526 | n/a | |
---|
527 | n/a | # Generate all ints starting at constructor arg. |
---|
528 | n/a | class IntsFrom: |
---|
529 | n/a | def __init__(self, start): |
---|
530 | n/a | self.i = start |
---|
531 | n/a | |
---|
532 | n/a | def __iter__(self): |
---|
533 | n/a | return self |
---|
534 | n/a | |
---|
535 | n/a | def __next__(self): |
---|
536 | n/a | i = self.i |
---|
537 | n/a | self.i = i+1 |
---|
538 | n/a | return i |
---|
539 | n/a | |
---|
540 | n/a | f = open(TESTFN, "w") |
---|
541 | n/a | try: |
---|
542 | n/a | f.write("a\n" "bbb\n" "cc\n") |
---|
543 | n/a | finally: |
---|
544 | n/a | f.close() |
---|
545 | n/a | f = open(TESTFN, "r") |
---|
546 | n/a | try: |
---|
547 | n/a | self.assertEqual(list(zip(IntsFrom(0), f, IntsFrom(-100))), |
---|
548 | n/a | [(0, "a\n", -100), |
---|
549 | n/a | (1, "bbb\n", -99), |
---|
550 | n/a | (2, "cc\n", -98)]) |
---|
551 | n/a | finally: |
---|
552 | n/a | f.close() |
---|
553 | n/a | try: |
---|
554 | n/a | unlink(TESTFN) |
---|
555 | n/a | except OSError: |
---|
556 | n/a | pass |
---|
557 | n/a | |
---|
558 | n/a | self.assertEqual(list(zip(range(5))), [(i,) for i in range(5)]) |
---|
559 | n/a | |
---|
560 | n/a | # Classes that lie about their lengths. |
---|
561 | n/a | class NoGuessLen5: |
---|
562 | n/a | def __getitem__(self, i): |
---|
563 | n/a | if i >= 5: |
---|
564 | n/a | raise IndexError |
---|
565 | n/a | return i |
---|
566 | n/a | |
---|
567 | n/a | class Guess3Len5(NoGuessLen5): |
---|
568 | n/a | def __len__(self): |
---|
569 | n/a | return 3 |
---|
570 | n/a | |
---|
571 | n/a | class Guess30Len5(NoGuessLen5): |
---|
572 | n/a | def __len__(self): |
---|
573 | n/a | return 30 |
---|
574 | n/a | |
---|
575 | n/a | def lzip(*args): |
---|
576 | n/a | return list(zip(*args)) |
---|
577 | n/a | |
---|
578 | n/a | self.assertEqual(len(Guess3Len5()), 3) |
---|
579 | n/a | self.assertEqual(len(Guess30Len5()), 30) |
---|
580 | n/a | self.assertEqual(lzip(NoGuessLen5()), lzip(range(5))) |
---|
581 | n/a | self.assertEqual(lzip(Guess3Len5()), lzip(range(5))) |
---|
582 | n/a | self.assertEqual(lzip(Guess30Len5()), lzip(range(5))) |
---|
583 | n/a | |
---|
584 | n/a | expected = [(i, i) for i in range(5)] |
---|
585 | n/a | for x in NoGuessLen5(), Guess3Len5(), Guess30Len5(): |
---|
586 | n/a | for y in NoGuessLen5(), Guess3Len5(), Guess30Len5(): |
---|
587 | n/a | self.assertEqual(lzip(x, y), expected) |
---|
588 | n/a | |
---|
589 | n/a | def test_unicode_join_endcase(self): |
---|
590 | n/a | |
---|
591 | n/a | # This class inserts a Unicode object into its argument's natural |
---|
592 | n/a | # iteration, in the 3rd position. |
---|
593 | n/a | class OhPhooey: |
---|
594 | n/a | def __init__(self, seq): |
---|
595 | n/a | self.it = iter(seq) |
---|
596 | n/a | self.i = 0 |
---|
597 | n/a | |
---|
598 | n/a | def __iter__(self): |
---|
599 | n/a | return self |
---|
600 | n/a | |
---|
601 | n/a | def __next__(self): |
---|
602 | n/a | i = self.i |
---|
603 | n/a | self.i = i+1 |
---|
604 | n/a | if i == 2: |
---|
605 | n/a | return "fooled you!" |
---|
606 | n/a | return next(self.it) |
---|
607 | n/a | |
---|
608 | n/a | f = open(TESTFN, "w") |
---|
609 | n/a | try: |
---|
610 | n/a | f.write("a\n" + "b\n" + "c\n") |
---|
611 | n/a | finally: |
---|
612 | n/a | f.close() |
---|
613 | n/a | |
---|
614 | n/a | f = open(TESTFN, "r") |
---|
615 | n/a | # Nasty: string.join(s) can't know whether unicode.join() is needed |
---|
616 | n/a | # until it's seen all of s's elements. But in this case, f's |
---|
617 | n/a | # iterator cannot be restarted. So what we're testing here is |
---|
618 | n/a | # whether string.join() can manage to remember everything it's seen |
---|
619 | n/a | # and pass that on to unicode.join(). |
---|
620 | n/a | try: |
---|
621 | n/a | got = " - ".join(OhPhooey(f)) |
---|
622 | n/a | self.assertEqual(got, "a\n - b\n - fooled you! - c\n") |
---|
623 | n/a | finally: |
---|
624 | n/a | f.close() |
---|
625 | n/a | try: |
---|
626 | n/a | unlink(TESTFN) |
---|
627 | n/a | except OSError: |
---|
628 | n/a | pass |
---|
629 | n/a | |
---|
630 | n/a | # Test iterators with 'x in y' and 'x not in y'. |
---|
631 | n/a | def test_in_and_not_in(self): |
---|
632 | n/a | for sc5 in IteratingSequenceClass(5), SequenceClass(5): |
---|
633 | n/a | for i in range(5): |
---|
634 | n/a | self.assertIn(i, sc5) |
---|
635 | n/a | for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5: |
---|
636 | n/a | self.assertNotIn(i, sc5) |
---|
637 | n/a | |
---|
638 | n/a | self.assertRaises(TypeError, lambda: 3 in 12) |
---|
639 | n/a | self.assertRaises(TypeError, lambda: 3 not in map) |
---|
640 | n/a | |
---|
641 | n/a | d = {"one": 1, "two": 2, "three": 3, 1j: 2j} |
---|
642 | n/a | for k in d: |
---|
643 | n/a | self.assertIn(k, d) |
---|
644 | n/a | self.assertNotIn(k, d.values()) |
---|
645 | n/a | for v in d.values(): |
---|
646 | n/a | self.assertIn(v, d.values()) |
---|
647 | n/a | self.assertNotIn(v, d) |
---|
648 | n/a | for k, v in d.items(): |
---|
649 | n/a | self.assertIn((k, v), d.items()) |
---|
650 | n/a | self.assertNotIn((v, k), d.items()) |
---|
651 | n/a | |
---|
652 | n/a | f = open(TESTFN, "w") |
---|
653 | n/a | try: |
---|
654 | n/a | f.write("a\n" "b\n" "c\n") |
---|
655 | n/a | finally: |
---|
656 | n/a | f.close() |
---|
657 | n/a | f = open(TESTFN, "r") |
---|
658 | n/a | try: |
---|
659 | n/a | for chunk in "abc": |
---|
660 | n/a | f.seek(0, 0) |
---|
661 | n/a | self.assertNotIn(chunk, f) |
---|
662 | n/a | f.seek(0, 0) |
---|
663 | n/a | self.assertIn((chunk + "\n"), f) |
---|
664 | n/a | finally: |
---|
665 | n/a | f.close() |
---|
666 | n/a | try: |
---|
667 | n/a | unlink(TESTFN) |
---|
668 | n/a | except OSError: |
---|
669 | n/a | pass |
---|
670 | n/a | |
---|
671 | n/a | # Test iterators with operator.countOf (PySequence_Count). |
---|
672 | n/a | def test_countOf(self): |
---|
673 | n/a | from operator import countOf |
---|
674 | n/a | self.assertEqual(countOf([1,2,2,3,2,5], 2), 3) |
---|
675 | n/a | self.assertEqual(countOf((1,2,2,3,2,5), 2), 3) |
---|
676 | n/a | self.assertEqual(countOf("122325", "2"), 3) |
---|
677 | n/a | self.assertEqual(countOf("122325", "6"), 0) |
---|
678 | n/a | |
---|
679 | n/a | self.assertRaises(TypeError, countOf, 42, 1) |
---|
680 | n/a | self.assertRaises(TypeError, countOf, countOf, countOf) |
---|
681 | n/a | |
---|
682 | n/a | d = {"one": 3, "two": 3, "three": 3, 1j: 2j} |
---|
683 | n/a | for k in d: |
---|
684 | n/a | self.assertEqual(countOf(d, k), 1) |
---|
685 | n/a | self.assertEqual(countOf(d.values(), 3), 3) |
---|
686 | n/a | self.assertEqual(countOf(d.values(), 2j), 1) |
---|
687 | n/a | self.assertEqual(countOf(d.values(), 1j), 0) |
---|
688 | n/a | |
---|
689 | n/a | f = open(TESTFN, "w") |
---|
690 | n/a | try: |
---|
691 | n/a | f.write("a\n" "b\n" "c\n" "b\n") |
---|
692 | n/a | finally: |
---|
693 | n/a | f.close() |
---|
694 | n/a | f = open(TESTFN, "r") |
---|
695 | n/a | try: |
---|
696 | n/a | for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0): |
---|
697 | n/a | f.seek(0, 0) |
---|
698 | n/a | self.assertEqual(countOf(f, letter + "\n"), count) |
---|
699 | n/a | finally: |
---|
700 | n/a | f.close() |
---|
701 | n/a | try: |
---|
702 | n/a | unlink(TESTFN) |
---|
703 | n/a | except OSError: |
---|
704 | n/a | pass |
---|
705 | n/a | |
---|
706 | n/a | # Test iterators with operator.indexOf (PySequence_Index). |
---|
707 | n/a | def test_indexOf(self): |
---|
708 | n/a | from operator import indexOf |
---|
709 | n/a | self.assertEqual(indexOf([1,2,2,3,2,5], 1), 0) |
---|
710 | n/a | self.assertEqual(indexOf((1,2,2,3,2,5), 2), 1) |
---|
711 | n/a | self.assertEqual(indexOf((1,2,2,3,2,5), 3), 3) |
---|
712 | n/a | self.assertEqual(indexOf((1,2,2,3,2,5), 5), 5) |
---|
713 | n/a | self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 0) |
---|
714 | n/a | self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 6) |
---|
715 | n/a | |
---|
716 | n/a | self.assertEqual(indexOf("122325", "2"), 1) |
---|
717 | n/a | self.assertEqual(indexOf("122325", "5"), 5) |
---|
718 | n/a | self.assertRaises(ValueError, indexOf, "122325", "6") |
---|
719 | n/a | |
---|
720 | n/a | self.assertRaises(TypeError, indexOf, 42, 1) |
---|
721 | n/a | self.assertRaises(TypeError, indexOf, indexOf, indexOf) |
---|
722 | n/a | |
---|
723 | n/a | f = open(TESTFN, "w") |
---|
724 | n/a | try: |
---|
725 | n/a | f.write("a\n" "b\n" "c\n" "d\n" "e\n") |
---|
726 | n/a | finally: |
---|
727 | n/a | f.close() |
---|
728 | n/a | f = open(TESTFN, "r") |
---|
729 | n/a | try: |
---|
730 | n/a | fiter = iter(f) |
---|
731 | n/a | self.assertEqual(indexOf(fiter, "b\n"), 1) |
---|
732 | n/a | self.assertEqual(indexOf(fiter, "d\n"), 1) |
---|
733 | n/a | self.assertEqual(indexOf(fiter, "e\n"), 0) |
---|
734 | n/a | self.assertRaises(ValueError, indexOf, fiter, "a\n") |
---|
735 | n/a | finally: |
---|
736 | n/a | f.close() |
---|
737 | n/a | try: |
---|
738 | n/a | unlink(TESTFN) |
---|
739 | n/a | except OSError: |
---|
740 | n/a | pass |
---|
741 | n/a | |
---|
742 | n/a | iclass = IteratingSequenceClass(3) |
---|
743 | n/a | for i in range(3): |
---|
744 | n/a | self.assertEqual(indexOf(iclass, i), i) |
---|
745 | n/a | self.assertRaises(ValueError, indexOf, iclass, -1) |
---|
746 | n/a | |
---|
747 | n/a | # Test iterators with file.writelines(). |
---|
748 | n/a | def test_writelines(self): |
---|
749 | n/a | f = open(TESTFN, "w") |
---|
750 | n/a | |
---|
751 | n/a | try: |
---|
752 | n/a | self.assertRaises(TypeError, f.writelines, None) |
---|
753 | n/a | self.assertRaises(TypeError, f.writelines, 42) |
---|
754 | n/a | |
---|
755 | n/a | f.writelines(["1\n", "2\n"]) |
---|
756 | n/a | f.writelines(("3\n", "4\n")) |
---|
757 | n/a | f.writelines({'5\n': None}) |
---|
758 | n/a | f.writelines({}) |
---|
759 | n/a | |
---|
760 | n/a | # Try a big chunk too. |
---|
761 | n/a | class Iterator: |
---|
762 | n/a | def __init__(self, start, finish): |
---|
763 | n/a | self.start = start |
---|
764 | n/a | self.finish = finish |
---|
765 | n/a | self.i = self.start |
---|
766 | n/a | |
---|
767 | n/a | def __next__(self): |
---|
768 | n/a | if self.i >= self.finish: |
---|
769 | n/a | raise StopIteration |
---|
770 | n/a | result = str(self.i) + '\n' |
---|
771 | n/a | self.i += 1 |
---|
772 | n/a | return result |
---|
773 | n/a | |
---|
774 | n/a | def __iter__(self): |
---|
775 | n/a | return self |
---|
776 | n/a | |
---|
777 | n/a | class Whatever: |
---|
778 | n/a | def __init__(self, start, finish): |
---|
779 | n/a | self.start = start |
---|
780 | n/a | self.finish = finish |
---|
781 | n/a | |
---|
782 | n/a | def __iter__(self): |
---|
783 | n/a | return Iterator(self.start, self.finish) |
---|
784 | n/a | |
---|
785 | n/a | f.writelines(Whatever(6, 6+2000)) |
---|
786 | n/a | f.close() |
---|
787 | n/a | |
---|
788 | n/a | f = open(TESTFN) |
---|
789 | n/a | expected = [str(i) + "\n" for i in range(1, 2006)] |
---|
790 | n/a | self.assertEqual(list(f), expected) |
---|
791 | n/a | |
---|
792 | n/a | finally: |
---|
793 | n/a | f.close() |
---|
794 | n/a | try: |
---|
795 | n/a | unlink(TESTFN) |
---|
796 | n/a | except OSError: |
---|
797 | n/a | pass |
---|
798 | n/a | |
---|
799 | n/a | |
---|
800 | n/a | # Test iterators on RHS of unpacking assignments. |
---|
801 | n/a | def test_unpack_iter(self): |
---|
802 | n/a | a, b = 1, 2 |
---|
803 | n/a | self.assertEqual((a, b), (1, 2)) |
---|
804 | n/a | |
---|
805 | n/a | a, b, c = IteratingSequenceClass(3) |
---|
806 | n/a | self.assertEqual((a, b, c), (0, 1, 2)) |
---|
807 | n/a | |
---|
808 | n/a | try: # too many values |
---|
809 | n/a | a, b = IteratingSequenceClass(3) |
---|
810 | n/a | except ValueError: |
---|
811 | n/a | pass |
---|
812 | n/a | else: |
---|
813 | n/a | self.fail("should have raised ValueError") |
---|
814 | n/a | |
---|
815 | n/a | try: # not enough values |
---|
816 | n/a | a, b, c = IteratingSequenceClass(2) |
---|
817 | n/a | except ValueError: |
---|
818 | n/a | pass |
---|
819 | n/a | else: |
---|
820 | n/a | self.fail("should have raised ValueError") |
---|
821 | n/a | |
---|
822 | n/a | try: # not iterable |
---|
823 | n/a | a, b, c = len |
---|
824 | n/a | except TypeError: |
---|
825 | n/a | pass |
---|
826 | n/a | else: |
---|
827 | n/a | self.fail("should have raised TypeError") |
---|
828 | n/a | |
---|
829 | n/a | a, b, c = {1: 42, 2: 42, 3: 42}.values() |
---|
830 | n/a | self.assertEqual((a, b, c), (42, 42, 42)) |
---|
831 | n/a | |
---|
832 | n/a | f = open(TESTFN, "w") |
---|
833 | n/a | lines = ("a\n", "bb\n", "ccc\n") |
---|
834 | n/a | try: |
---|
835 | n/a | for line in lines: |
---|
836 | n/a | f.write(line) |
---|
837 | n/a | finally: |
---|
838 | n/a | f.close() |
---|
839 | n/a | f = open(TESTFN, "r") |
---|
840 | n/a | try: |
---|
841 | n/a | a, b, c = f |
---|
842 | n/a | self.assertEqual((a, b, c), lines) |
---|
843 | n/a | finally: |
---|
844 | n/a | f.close() |
---|
845 | n/a | try: |
---|
846 | n/a | unlink(TESTFN) |
---|
847 | n/a | except OSError: |
---|
848 | n/a | pass |
---|
849 | n/a | |
---|
850 | n/a | (a, b), (c,) = IteratingSequenceClass(2), {42: 24} |
---|
851 | n/a | self.assertEqual((a, b, c), (0, 1, 42)) |
---|
852 | n/a | |
---|
853 | n/a | |
---|
854 | n/a | @cpython_only |
---|
855 | n/a | def test_ref_counting_behavior(self): |
---|
856 | n/a | class C(object): |
---|
857 | n/a | count = 0 |
---|
858 | n/a | def __new__(cls): |
---|
859 | n/a | cls.count += 1 |
---|
860 | n/a | return object.__new__(cls) |
---|
861 | n/a | def __del__(self): |
---|
862 | n/a | cls = self.__class__ |
---|
863 | n/a | assert cls.count > 0 |
---|
864 | n/a | cls.count -= 1 |
---|
865 | n/a | x = C() |
---|
866 | n/a | self.assertEqual(C.count, 1) |
---|
867 | n/a | del x |
---|
868 | n/a | self.assertEqual(C.count, 0) |
---|
869 | n/a | l = [C(), C(), C()] |
---|
870 | n/a | self.assertEqual(C.count, 3) |
---|
871 | n/a | try: |
---|
872 | n/a | a, b = iter(l) |
---|
873 | n/a | except ValueError: |
---|
874 | n/a | pass |
---|
875 | n/a | del l |
---|
876 | n/a | self.assertEqual(C.count, 0) |
---|
877 | n/a | |
---|
878 | n/a | |
---|
879 | n/a | # Make sure StopIteration is a "sink state". |
---|
880 | n/a | # This tests various things that weren't sink states in Python 2.2.1, |
---|
881 | n/a | # plus various things that always were fine. |
---|
882 | n/a | |
---|
883 | n/a | def test_sinkstate_list(self): |
---|
884 | n/a | # This used to fail |
---|
885 | n/a | a = list(range(5)) |
---|
886 | n/a | b = iter(a) |
---|
887 | n/a | self.assertEqual(list(b), list(range(5))) |
---|
888 | n/a | a.extend(range(5, 10)) |
---|
889 | n/a | self.assertEqual(list(b), []) |
---|
890 | n/a | |
---|
891 | n/a | def test_sinkstate_tuple(self): |
---|
892 | n/a | a = (0, 1, 2, 3, 4) |
---|
893 | n/a | b = iter(a) |
---|
894 | n/a | self.assertEqual(list(b), list(range(5))) |
---|
895 | n/a | self.assertEqual(list(b), []) |
---|
896 | n/a | |
---|
897 | n/a | def test_sinkstate_string(self): |
---|
898 | n/a | a = "abcde" |
---|
899 | n/a | b = iter(a) |
---|
900 | n/a | self.assertEqual(list(b), ['a', 'b', 'c', 'd', 'e']) |
---|
901 | n/a | self.assertEqual(list(b), []) |
---|
902 | n/a | |
---|
903 | n/a | def test_sinkstate_sequence(self): |
---|
904 | n/a | # This used to fail |
---|
905 | n/a | a = SequenceClass(5) |
---|
906 | n/a | b = iter(a) |
---|
907 | n/a | self.assertEqual(list(b), list(range(5))) |
---|
908 | n/a | a.n = 10 |
---|
909 | n/a | self.assertEqual(list(b), []) |
---|
910 | n/a | |
---|
911 | n/a | def test_sinkstate_callable(self): |
---|
912 | n/a | # This used to fail |
---|
913 | n/a | def spam(state=[0]): |
---|
914 | n/a | i = state[0] |
---|
915 | n/a | state[0] = i+1 |
---|
916 | n/a | if i == 10: |
---|
917 | n/a | raise AssertionError("shouldn't have gotten this far") |
---|
918 | n/a | return i |
---|
919 | n/a | b = iter(spam, 5) |
---|
920 | n/a | self.assertEqual(list(b), list(range(5))) |
---|
921 | n/a | self.assertEqual(list(b), []) |
---|
922 | n/a | |
---|
923 | n/a | def test_sinkstate_dict(self): |
---|
924 | n/a | # XXX For a more thorough test, see towards the end of: |
---|
925 | n/a | # http://mail.python.org/pipermail/python-dev/2002-July/026512.html |
---|
926 | n/a | a = {1:1, 2:2, 0:0, 4:4, 3:3} |
---|
927 | n/a | for b in iter(a), a.keys(), a.items(), a.values(): |
---|
928 | n/a | b = iter(a) |
---|
929 | n/a | self.assertEqual(len(list(b)), 5) |
---|
930 | n/a | self.assertEqual(list(b), []) |
---|
931 | n/a | |
---|
932 | n/a | def test_sinkstate_yield(self): |
---|
933 | n/a | def gen(): |
---|
934 | n/a | for i in range(5): |
---|
935 | n/a | yield i |
---|
936 | n/a | b = gen() |
---|
937 | n/a | self.assertEqual(list(b), list(range(5))) |
---|
938 | n/a | self.assertEqual(list(b), []) |
---|
939 | n/a | |
---|
940 | n/a | def test_sinkstate_range(self): |
---|
941 | n/a | a = range(5) |
---|
942 | n/a | b = iter(a) |
---|
943 | n/a | self.assertEqual(list(b), list(range(5))) |
---|
944 | n/a | self.assertEqual(list(b), []) |
---|
945 | n/a | |
---|
946 | n/a | def test_sinkstate_enumerate(self): |
---|
947 | n/a | a = range(5) |
---|
948 | n/a | e = enumerate(a) |
---|
949 | n/a | b = iter(e) |
---|
950 | n/a | self.assertEqual(list(b), list(zip(range(5), range(5)))) |
---|
951 | n/a | self.assertEqual(list(b), []) |
---|
952 | n/a | |
---|
953 | n/a | def test_3720(self): |
---|
954 | n/a | # Avoid a crash, when an iterator deletes its next() method. |
---|
955 | n/a | class BadIterator(object): |
---|
956 | n/a | def __iter__(self): |
---|
957 | n/a | return self |
---|
958 | n/a | def __next__(self): |
---|
959 | n/a | del BadIterator.__next__ |
---|
960 | n/a | return 1 |
---|
961 | n/a | |
---|
962 | n/a | try: |
---|
963 | n/a | for i in BadIterator() : |
---|
964 | n/a | pass |
---|
965 | n/a | except TypeError: |
---|
966 | n/a | pass |
---|
967 | n/a | |
---|
968 | n/a | def test_extending_list_with_iterator_does_not_segfault(self): |
---|
969 | n/a | # The code to extend a list with an iterator has a fair |
---|
970 | n/a | # amount of nontrivial logic in terms of guessing how |
---|
971 | n/a | # much memory to allocate in advance, "stealing" refs, |
---|
972 | n/a | # and then shrinking at the end. This is a basic smoke |
---|
973 | n/a | # test for that scenario. |
---|
974 | n/a | def gen(): |
---|
975 | n/a | for i in range(500): |
---|
976 | n/a | yield i |
---|
977 | n/a | lst = [0] * 500 |
---|
978 | n/a | for i in range(240): |
---|
979 | n/a | lst.pop(0) |
---|
980 | n/a | lst.extend(gen()) |
---|
981 | n/a | self.assertEqual(len(lst), 760) |
---|
982 | n/a | |
---|
983 | n/a | @cpython_only |
---|
984 | n/a | def test_iter_overflow(self): |
---|
985 | n/a | # Test for the issue 22939 |
---|
986 | n/a | it = iter(UnlimitedSequenceClass()) |
---|
987 | n/a | # Manually set `it_index` to PY_SSIZE_T_MAX-2 without a loop |
---|
988 | n/a | it.__setstate__(sys.maxsize - 2) |
---|
989 | n/a | self.assertEqual(next(it), sys.maxsize - 2) |
---|
990 | n/a | self.assertEqual(next(it), sys.maxsize - 1) |
---|
991 | n/a | with self.assertRaises(OverflowError): |
---|
992 | n/a | next(it) |
---|
993 | n/a | # Check that Overflow error is always raised |
---|
994 | n/a | with self.assertRaises(OverflowError): |
---|
995 | n/a | next(it) |
---|
996 | n/a | |
---|
997 | n/a | def test_iter_neg_setstate(self): |
---|
998 | n/a | it = iter(UnlimitedSequenceClass()) |
---|
999 | n/a | it.__setstate__(-42) |
---|
1000 | n/a | self.assertEqual(next(it), 0) |
---|
1001 | n/a | self.assertEqual(next(it), 1) |
---|
1002 | n/a | |
---|
1003 | n/a | def test_free_after_iterating(self): |
---|
1004 | n/a | check_free_after_iterating(self, iter, SequenceClass, (0,)) |
---|
1005 | n/a | |
---|
1006 | n/a | def test_error_iter(self): |
---|
1007 | n/a | for typ in (DefaultIterClass, NoIterClass): |
---|
1008 | n/a | self.assertRaises(TypeError, iter, typ()) |
---|
1009 | n/a | |
---|
1010 | n/a | |
---|
1011 | n/a | def test_main(): |
---|
1012 | n/a | run_unittest(TestCase) |
---|
1013 | n/a | |
---|
1014 | n/a | |
---|
1015 | n/a | if __name__ == "__main__": |
---|
1016 | n/a | test_main() |
---|