annotate Lib/test/test_iter.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents e9748db47635
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Test iterators.
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 24265
diff changeset
4 from test.test_support import run_unittest, TESTFN, unlink, have_unicode
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # Test result of triple loop (too big to inline)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 (0, 1, 0), (0, 1, 1), (0, 1, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 (0, 2, 0), (0, 2, 1), (0, 2, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 (1, 0, 0), (1, 0, 1), (1, 0, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 (1, 1, 0), (1, 1, 1), (1, 1, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 (1, 2, 0), (1, 2, 1), (1, 2, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 (2, 0, 0), (2, 0, 1), (2, 0, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 (2, 1, 0), (2, 1, 1), (2, 1, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 (2, 2, 0), (2, 2, 1), (2, 2, 2)]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # Helper classes
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 class BasicIterClass:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 def __init__(self, n):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 self.n = n
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 self.i = 0
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def next(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 res = self.i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 if res >= self.n:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 raise StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 self.i = res + 1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 return res
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 class IteratingSequenceClass:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def __init__(self, n):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 self.n = n
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def __iter__(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 return BasicIterClass(self.n)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 class SequenceClass:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 def __init__(self, n):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self.n = n
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 def __getitem__(self, i):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 if 0 <= i < self.n:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 else:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 raise IndexError
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # Main test suite
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 class TestCase(unittest.TestCase):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # Helper to check that an iterator returns a given sequence
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 def check_iterator(self, it, seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 while 1:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 val = it.next()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 except StopIteration:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 break
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 res.append(val)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 self.assertEqual(res, seq)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # Helper to check that a for loop generates a given sequence
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 def check_for_loop(self, expr, seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 for val in expr:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 res.append(val)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 self.assertEqual(res, seq)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # Test basic use of iter() function
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 def test_iter_basic(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 self.check_iterator(iter(range(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # Test that iter(iter(x)) is the same as iter(x)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 def test_iter_idempotency(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 seq = range(10)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 it = iter(seq)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 it2 = iter(it)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 self.assert_(it is it2)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # Test that for loops over iterators work
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def test_iter_for_loop(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 self.check_for_loop(iter(range(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 # Test several independent iterators over the same list
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 def test_iter_independence(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 seq = range(3)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 for i in iter(seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 for j in iter(seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 for k in iter(seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 res.append((i, j, k))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 self.assertEqual(res, TRIPLETS)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 # Test triple list comprehension using iterators
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 def test_nested_comprehensions_iter(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 seq = range(3)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 res = [(i, j, k)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 for i in iter(seq) for j in iter(seq) for k in iter(seq)]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 self.assertEqual(res, TRIPLETS)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 # Test triple list comprehension without iterators
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 def test_nested_comprehensions_for(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 seq = range(3)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 res = [(i, j, k) for i in seq for j in seq for k in seq]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.assertEqual(res, TRIPLETS)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 # Test a class with __iter__ in a for loop
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 def test_iter_class_for(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 self.check_for_loop(IteratingSequenceClass(10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 # Test a class with __iter__ with explicit iter()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 def test_iter_class_iter(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 self.check_iterator(iter(IteratingSequenceClass(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 # Test for loop on a sequence class without __iter__
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 def test_seq_class_for(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 self.check_for_loop(SequenceClass(10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 # Test iter() on a sequence class without __iter__
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 def test_seq_class_iter(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 self.check_iterator(iter(SequenceClass(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
50754
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
123 # Test a new_style class with __iter__ but no next() method
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
124 def test_new_style_iter_class(self):
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
125 class IterClass(object):
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
126 def __iter__(self):
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
127 return self
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
128 self.assertRaises(TypeError, iter, IterClass())
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
129
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 # Test two-argument iter() with callable instance
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 def test_iter_callable(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 class C:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 def __init__(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 self.i = 0
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def __call__(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 i = self.i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 self.i = i + 1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if i > 100:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 raise IndexError # Emergency stop
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 self.check_iterator(iter(C(), 10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 # Test two-argument iter() with function
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 def test_iter_function(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 def spam(state=[0]):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 i = state[0]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 state[0] = i+1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 self.check_iterator(iter(spam, 10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 # Test two-argument iter() with function that raises StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 def test_iter_function_stop(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 def spam(state=[0]):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 i = state[0]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 raise StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 state[0] = i+1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 self.check_iterator(iter(spam, 20), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 # Test exception propagation through function iterator
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 def test_exception_function(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def spam(state=[0]):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 i = state[0]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 state[0] = i+1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 raise RuntimeError
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 for x in iter(spam, 20):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 res.append(x)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 except RuntimeError:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 self.assertEqual(res, range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 else:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 self.fail("should have raised RuntimeError")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 # Test exception propagation through sequence iterator
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 def test_exception_sequence(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 class MySequenceClass(SequenceClass):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 def __getitem__(self, i):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 raise RuntimeError
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 return SequenceClass.__getitem__(self, i)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 for x in MySequenceClass(20):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 res.append(x)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 except RuntimeError:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 self.assertEqual(res, range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 else:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 self.fail("should have raised RuntimeError")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 # Test for StopIteration from __getitem__
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 def test_stop_sequence(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 class MySequenceClass(SequenceClass):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 def __getitem__(self, i):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 raise StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 return SequenceClass.__getitem__(self, i)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 self.check_for_loop(MySequenceClass(20), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 # Test a big range
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 def test_iter_big_range(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 self.check_for_loop(iter(range(10000)), range(10000))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 # Test an empty list
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 def test_iter_empty(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 self.check_for_loop(iter([]), [])
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 # Test a tuple
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 def test_iter_tuple(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 self.check_for_loop(iter((0,1,2,3,4,5,6,7,8,9)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 # Test an xrange
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 def test_iter_xrange(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 self.check_for_loop(iter(xrange(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 # Test a string
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def test_iter_string(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 self.check_for_loop(iter("abcde"), ["a", "b", "c", "d", "e"])
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 # Test a Unicode string
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
224 if have_unicode:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
225 def test_iter_unicode(self):
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
226 self.check_for_loop(iter(unicode("abcde")),
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
227 [unicode("a"), unicode("b"), unicode("c"),
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
228 unicode("d"), unicode("e")])
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 # Test a directory
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 def test_iter_dict(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 dict = {}
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 for i in range(10):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 dict[i] = None
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 self.check_for_loop(dict, dict.keys())
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 # Test a file
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 def test_iter_file(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 f = open(TESTFN, "w")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 for i in range(5):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 f.write("%d\n" % i)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 finally:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 f.close()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 f = open(TESTFN, "r")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 self.check_for_loop(f, ["0\n", "1\n", "2\n", "3\n", "4\n"])
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 self.check_for_loop(f, [])
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 finally:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 f.close()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 unlink(TESTFN)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 except OSError:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 pass
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
256 # Test list()'s use of iterators.
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
257 def test_builtin_list(self):
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
258 self.assertEqual(list(SequenceClass(5)), range(5))
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
259 self.assertEqual(list(SequenceClass(0)), [])
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
260 self.assertEqual(list(()), [])
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
261 self.assertEqual(list(range(10, -1, -1)), range(10, -1, -1))
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
262
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
263 d = {"one": 1, "two": 2, "three": 3}
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
264 self.assertEqual(list(d), d.keys())
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
265
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
266 self.assertRaises(TypeError, list, list)
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
267 self.assertRaises(TypeError, list, 42)
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
268
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
269 f = open(TESTFN, "w")
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
270 try:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
271 for i in range(5):
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
272 f.write("%d\n" % i)
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
273 finally:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
274 f.close()
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
275 f = open(TESTFN, "r")
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
276 try:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
277 self.assertEqual(list(f), ["0\n", "1\n", "2\n", "3\n", "4\n"])
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
278 f.seek(0, 0)
24602
f5e29c7336dd Mark xreadlines deprecated. Don't use f.xreadlines() in test_iter.py.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
279 self.assertEqual(list(f),
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
280 ["0\n", "1\n", "2\n", "3\n", "4\n"])
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
281 finally:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
282 f.close()
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
283 try:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
284 unlink(TESTFN)
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
285 except OSError:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
286 pass
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
287
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
288 # Test tuples()'s use of iterators.
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
289 def test_builtin_tuple(self):
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
290 self.assertEqual(tuple(SequenceClass(5)), (0, 1, 2, 3, 4))
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
291 self.assertEqual(tuple(SequenceClass(0)), ())
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
292 self.assertEqual(tuple([]), ())
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
293 self.assertEqual(tuple(()), ())
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
294 self.assertEqual(tuple("abc"), ("a", "b", "c"))
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
295
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
296 d = {"one": 1, "two": 2, "three": 3}
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
297 self.assertEqual(tuple(d), tuple(d.keys()))
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
298
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
299 self.assertRaises(TypeError, tuple, list)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
300 self.assertRaises(TypeError, tuple, 42)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
301
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
302 f = open(TESTFN, "w")
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
303 try:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
304 for i in range(5):
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
305 f.write("%d\n" % i)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
306 finally:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
307 f.close()
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
308 f = open(TESTFN, "r")
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
309 try:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
310 self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n"))
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
311 f.seek(0, 0)
24602
f5e29c7336dd Mark xreadlines deprecated. Don't use f.xreadlines() in test_iter.py.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
312 self.assertEqual(tuple(f),
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
313 ("0\n", "1\n", "2\n", "3\n", "4\n"))
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
314 finally:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
315 f.close()
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
316 try:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
317 unlink(TESTFN)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
318 except OSError:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
319 pass
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
320
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
321 # Test filter()'s use of iterators.
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
322 def test_builtin_filter(self):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
323 self.assertEqual(filter(None, SequenceClass(5)), range(1, 5))
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
324 self.assertEqual(filter(None, SequenceClass(0)), [])
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
325 self.assertEqual(filter(None, ()), ())
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
326 self.assertEqual(filter(None, "abc"), "abc")
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
327
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
328 d = {"one": 1, "two": 2, "three": 3}
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
329 self.assertEqual(filter(None, d), d.keys())
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
330
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
331 self.assertRaises(TypeError, filter, None, list)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
332 self.assertRaises(TypeError, filter, None, 42)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
333
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
334 class Boolean:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
335 def __init__(self, truth):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
336 self.truth = truth
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
337 def __nonzero__(self):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
338 return self.truth
26755
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
339 bTrue = Boolean(1)
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
340 bFalse = Boolean(0)
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
341
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
342 class Seq:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
343 def __init__(self, *args):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
344 self.vals = args
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
345 def __iter__(self):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
346 class SeqIter:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
347 def __init__(self, vals):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
348 self.vals = vals
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
349 self.i = 0
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
350 def __iter__(self):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
351 return self
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
352 def next(self):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
353 i = self.i
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
354 self.i = i + 1
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
355 if i < len(self.vals):
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
356 return self.vals[i]
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
357 else:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
358 raise StopIteration
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
359 return SeqIter(self.vals)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
360
26755
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
361 seq = Seq(*([bTrue, bFalse] * 25))
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
362 self.assertEqual(filter(lambda x: not x, seq), [bFalse]*25)
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
363 self.assertEqual(filter(lambda x: not x, iter(seq)), [bFalse]*25)
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
364
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
365 # Test max() and min()'s use of iterators.
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
366 def test_builtin_max_min(self):
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
367 self.assertEqual(max(SequenceClass(5)), 4)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
368 self.assertEqual(min(SequenceClass(5)), 0)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
369 self.assertEqual(max(8, -1), 8)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
370 self.assertEqual(min(8, -1), -1)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
371
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
372 d = {"one": 1, "two": 2, "three": 3}
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
373 self.assertEqual(max(d), "two")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
374 self.assertEqual(min(d), "one")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
375 self.assertEqual(max(d.itervalues()), 3)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
376 self.assertEqual(min(iter(d.itervalues())), 1)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
377
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
378 f = open(TESTFN, "w")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
379 try:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
380 f.write("medium line\n")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
381 f.write("xtra large line\n")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
382 f.write("itty-bitty line\n")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
383 finally:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
384 f.close()
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
385 f = open(TESTFN, "r")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
386 try:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
387 self.assertEqual(min(f), "itty-bitty line\n")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
388 f.seek(0, 0)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
389 self.assertEqual(max(f), "xtra large line\n")
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
390 finally:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
391 f.close()
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
392 try:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
393 unlink(TESTFN)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
394 except OSError:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
395 pass
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
396
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
397 # Test map()'s use of iterators.
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
398 def test_builtin_map(self):
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
399 self.assertEqual(map(None, SequenceClass(5)), range(5))
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
400 self.assertEqual(map(lambda x: x+1, SequenceClass(5)), range(1, 6))
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
401
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
402 d = {"one": 1, "two": 2, "three": 3}
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
403 self.assertEqual(map(None, d), d.keys())
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
404 self.assertEqual(map(lambda k, d=d: (k, d[k]), d), d.items())
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
405 dkeys = d.keys()
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
406 expected = [(i < len(d) and dkeys[i] or None,
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
407 i,
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
408 i < len(d) and dkeys[i] or None)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
409 for i in range(5)]
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
410 self.assertEqual(map(None, d,
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
411 SequenceClass(5),
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
412 iter(d.iterkeys())),
17676
d80500076e9f Purge redundant cut&paste line.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
413 expected)
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
414
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
415 f = open(TESTFN, "w")
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
416 try:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
417 for i in range(10):
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
418 f.write("xy" * i + "\n") # line i has len 2*i+1
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
419 finally:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
420 f.close()
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
421 f = open(TESTFN, "r")
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
422 try:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
423 self.assertEqual(map(len, f), range(1, 21, 2))
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
424 finally:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
425 f.close()
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
426 try:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
427 unlink(TESTFN)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
428 except OSError:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
429 pass
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
430
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
431 # Test zip()'s use of iterators.
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
432 def test_builtin_zip(self):
30037
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 26755
diff changeset
433 self.assertEqual(zip(), [])
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 26755
diff changeset
434 self.assertEqual(zip(*[]), [])
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 26755
diff changeset
435 self.assertEqual(zip(*[(1, 2), 'ab']), [(1, 'a'), (2, 'b')])
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 26755
diff changeset
436
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
437 self.assertRaises(TypeError, zip, None)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
438 self.assertRaises(TypeError, zip, range(10), 42)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
439 self.assertRaises(TypeError, zip, range(10), zip)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
440
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
441 self.assertEqual(zip(IteratingSequenceClass(3)),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
442 [(0,), (1,), (2,)])
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
443 self.assertEqual(zip(SequenceClass(3)),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
444 [(0,), (1,), (2,)])
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
445
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
446 d = {"one": 1, "two": 2, "three": 3}
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
447 self.assertEqual(d.items(), zip(d, d.itervalues()))
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
448
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
449 # Generate all ints starting at constructor arg.
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
450 class IntsFrom:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
451 def __init__(self, start):
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
452 self.i = start
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
453
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
454 def __iter__(self):
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
455 return self
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
456
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
457 def next(self):
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
458 i = self.i
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
459 self.i = i+1
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
460 return i
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
461
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
462 f = open(TESTFN, "w")
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
463 try:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
464 f.write("a\n" "bbb\n" "cc\n")
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
465 finally:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
466 f.close()
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
467 f = open(TESTFN, "r")
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
468 try:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
469 self.assertEqual(zip(IntsFrom(0), f, IntsFrom(-100)),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
470 [(0, "a\n", -100),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
471 (1, "bbb\n", -99),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
472 (2, "cc\n", -98)])
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
473 finally:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
474 f.close()
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
475 try:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
476 unlink(TESTFN)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
477 except OSError:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
478 pass
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
479
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
480 self.assertEqual(zip(xrange(5)), [(i,) for i in range(5)])
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
481
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
482 # Classes that lie about their lengths.
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
483 class NoGuessLen5:
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
484 def __getitem__(self, i):
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
485 if i >= 5:
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
486 raise IndexError
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
487 return i
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
488
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
489 class Guess3Len5(NoGuessLen5):
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
490 def __len__(self):
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
491 return 3
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
492
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
493 class Guess30Len5(NoGuessLen5):
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
494 def __len__(self):
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
495 return 30
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
496
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
497 self.assertEqual(len(Guess3Len5()), 3)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
498 self.assertEqual(len(Guess30Len5()), 30)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
499 self.assertEqual(zip(NoGuessLen5()), zip(range(5)))
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
500 self.assertEqual(zip(Guess3Len5()), zip(range(5)))
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
501 self.assertEqual(zip(Guess30Len5()), zip(range(5)))
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
502
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
503 expected = [(i, i) for i in range(5)]
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
504 for x in NoGuessLen5(), Guess3Len5(), Guess30Len5():
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
505 for y in NoGuessLen5(), Guess3Len5(), Guess30Len5():
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
506 self.assertEqual(zip(x, y), expected)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 21170
diff changeset
507
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
508 # Test reduces()'s use of iterators.
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
509 def test_builtin_reduce(self):
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
510 from operator import add
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
511 self.assertEqual(reduce(add, SequenceClass(5)), 10)
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
512 self.assertEqual(reduce(add, SequenceClass(5), 42), 52)
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
513 self.assertRaises(TypeError, reduce, add, SequenceClass(0))
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
514 self.assertEqual(reduce(add, SequenceClass(0), 42), 42)
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
515 self.assertEqual(reduce(add, SequenceClass(1)), 0)
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
516 self.assertEqual(reduce(add, SequenceClass(1), 42), 42)
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
517
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
518 d = {"one": 1, "two": 2, "three": 3}
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
519 self.assertEqual(reduce(add, d), "".join(d.keys()))
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
520
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
521 # This test case will be removed if we don't have Unicode
17687
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
522 def test_unicode_join_endcase(self):
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
523
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
524 # This class inserts a Unicode object into its argument's natural
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
525 # iteration, in the 3rd position.
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
526 class OhPhooey:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
527 def __init__(self, seq):
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
528 self.it = iter(seq)
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
529 self.i = 0
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
530
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
531 def __iter__(self):
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
532 return self
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
533
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
534 def next(self):
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
535 i = self.i
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
536 self.i = i+1
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
537 if i == 2:
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
538 return unicode("fooled you!")
17687
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
539 return self.it.next()
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
540
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
541 f = open(TESTFN, "w")
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
542 try:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
543 f.write("a\n" + "b\n" + "c\n")
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
544 finally:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
545 f.close()
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
546
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
547 f = open(TESTFN, "r")
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
548 # Nasty: string.join(s) can't know whether unicode.join() is needed
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
549 # until it's seen all of s's elements. But in this case, f's
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
550 # iterator cannot be restarted. So what we're testing here is
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
551 # whether string.join() can manage to remember everything it's seen
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
552 # and pass that on to unicode.join().
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
553 try:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
554 got = " - ".join(OhPhooey(f))
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
555 self.assertEqual(got, unicode("a\n - b\n - fooled you! - c\n"))
17687
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
556 finally:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
557 f.close()
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
558 try:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
559 unlink(TESTFN)
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
560 except OSError:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
561 pass
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
562 if not have_unicode:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
563 def test_unicode_join_endcase(self): pass
17687
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
564
17688
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
565 # Test iterators with 'x in y' and 'x not in y'.
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
566 def test_in_and_not_in(self):
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
567 for sc5 in IteratingSequenceClass(5), SequenceClass(5):
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
568 for i in range(5):
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
569 self.assert_(i in sc5)
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
570 for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
571 self.assert_(i not in sc5)
17688
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
572
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
573 self.assertRaises(TypeError, lambda: 3 in 12)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
574 self.assertRaises(TypeError, lambda: 3 not in map)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
575
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
576 d = {"one": 1, "two": 2, "three": 3, 1j: 2j}
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
577 for k in d:
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
578 self.assert_(k in d)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
579 self.assert_(k not in d.itervalues())
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
580 for v in d.values():
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
581 self.assert_(v in d.itervalues())
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
582 self.assert_(v not in d)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
583 for k, v in d.iteritems():
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
584 self.assert_((k, v) in d.iteritems())
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
585 self.assert_((v, k) not in d.iteritems())
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
586
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
587 f = open(TESTFN, "w")
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
588 try:
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
589 f.write("a\n" "b\n" "c\n")
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
590 finally:
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
591 f.close()
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
592 f = open(TESTFN, "r")
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
593 try:
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
594 for chunk in "abc":
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
595 f.seek(0, 0)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
596 self.assert_(chunk not in f)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
597 f.seek(0, 0)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
598 self.assert_((chunk + "\n") in f)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
599 finally:
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
600 f.close()
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
601 try:
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
602 unlink(TESTFN)
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
603 except OSError:
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
604 pass
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17687
diff changeset
605
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
606 # Test iterators with operator.countOf (PySequence_Count).
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
607 def test_countOf(self):
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
608 from operator import countOf
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
609 self.assertEqual(countOf([1,2,2,3,2,5], 2), 3)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
610 self.assertEqual(countOf((1,2,2,3,2,5), 2), 3)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
611 self.assertEqual(countOf("122325", "2"), 3)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
612 self.assertEqual(countOf("122325", "6"), 0)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
613
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
614 self.assertRaises(TypeError, countOf, 42, 1)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
615 self.assertRaises(TypeError, countOf, countOf, countOf)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
616
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
617 d = {"one": 3, "two": 3, "three": 3, 1j: 2j}
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
618 for k in d:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
619 self.assertEqual(countOf(d, k), 1)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
620 self.assertEqual(countOf(d.itervalues(), 3), 3)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
621 self.assertEqual(countOf(d.itervalues(), 2j), 1)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
622 self.assertEqual(countOf(d.itervalues(), 1j), 0)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
623
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
624 f = open(TESTFN, "w")
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
625 try:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
626 f.write("a\n" "b\n" "c\n" "b\n")
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
627 finally:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
628 f.close()
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
629 f = open(TESTFN, "r")
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
630 try:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
631 for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0):
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
632 f.seek(0, 0)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
633 self.assertEqual(countOf(f, letter + "\n"), count)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
634 finally:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
635 f.close()
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
636 try:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
637 unlink(TESTFN)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
638 except OSError:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
639 pass
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
640
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
641 # Test iterators with operator.indexOf (PySequence_Index).
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
642 def test_indexOf(self):
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
643 from operator import indexOf
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
644 self.assertEqual(indexOf([1,2,2,3,2,5], 1), 0)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
645 self.assertEqual(indexOf((1,2,2,3,2,5), 2), 1)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
646 self.assertEqual(indexOf((1,2,2,3,2,5), 3), 3)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
647 self.assertEqual(indexOf((1,2,2,3,2,5), 5), 5)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
648 self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 0)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
649 self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 6)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
650
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
651 self.assertEqual(indexOf("122325", "2"), 1)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
652 self.assertEqual(indexOf("122325", "5"), 5)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
653 self.assertRaises(ValueError, indexOf, "122325", "6")
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
654
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
655 self.assertRaises(TypeError, indexOf, 42, 1)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
656 self.assertRaises(TypeError, indexOf, indexOf, indexOf)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
657
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
658 f = open(TESTFN, "w")
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
659 try:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
660 f.write("a\n" "b\n" "c\n" "d\n" "e\n")
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
661 finally:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
662 f.close()
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
663 f = open(TESTFN, "r")
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
664 try:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
665 fiter = iter(f)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
666 self.assertEqual(indexOf(fiter, "b\n"), 1)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
667 self.assertEqual(indexOf(fiter, "d\n"), 1)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
668 self.assertEqual(indexOf(fiter, "e\n"), 0)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
669 self.assertRaises(ValueError, indexOf, fiter, "a\n")
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
670 finally:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
671 f.close()
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
672 try:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
673 unlink(TESTFN)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
674 except OSError:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
675 pass
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
676
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
677 iclass = IteratingSequenceClass(3)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
678 for i in range(3):
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
679 self.assertEqual(indexOf(iclass, i), i)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
680 self.assertRaises(ValueError, indexOf, iclass, -1)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
681
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
682 # Test iterators with file.writelines().
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
683 def test_writelines(self):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
684 f = file(TESTFN, "w")
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
685
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
686 try:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
687 self.assertRaises(TypeError, f.writelines, None)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
688 self.assertRaises(TypeError, f.writelines, 42)
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
689
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
690 f.writelines(["1\n", "2\n"])
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
691 f.writelines(("3\n", "4\n"))
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
692 f.writelines({'5\n': None})
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
693 f.writelines({})
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
694
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
695 # Try a big chunk too.
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
696 class Iterator:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
697 def __init__(self, start, finish):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
698 self.start = start
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
699 self.finish = finish
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
700 self.i = self.start
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
701
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
702 def next(self):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
703 if self.i >= self.finish:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
704 raise StopIteration
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
705 result = str(self.i) + '\n'
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
706 self.i += 1
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
707 return result
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
708
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
709 def __iter__(self):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
710 return self
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
711
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
712 class Whatever:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
713 def __init__(self, start, finish):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
714 self.start = start
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
715 self.finish = finish
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
716
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
717 def __iter__(self):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
718 return Iterator(self.start, self.finish)
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
719
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
720 f.writelines(Whatever(6, 6+2000))
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
721 f.close()
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
722
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
723 f = file(TESTFN)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
724 expected = [str(i) + "\n" for i in range(1, 2006)]
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
725 self.assertEqual(list(f), expected)
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
726
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
727 finally:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
728 f.close()
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
729 try:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
730 unlink(TESTFN)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
731 except OSError:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
732 pass
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
733
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
734
18195
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
735 # Test iterators on RHS of unpacking assignments.
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
736 def test_unpack_iter(self):
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
737 a, b = 1, 2
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
738 self.assertEqual((a, b), (1, 2))
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
739
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
740 a, b, c = IteratingSequenceClass(3)
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
741 self.assertEqual((a, b, c), (0, 1, 2))
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
742
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
743 try: # too many values
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
744 a, b = IteratingSequenceClass(3)
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
745 except ValueError:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
746 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
747 else:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
748 self.fail("should have raised ValueError")
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
749
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
750 try: # not enough values
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
751 a, b, c = IteratingSequenceClass(2)
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
752 except ValueError:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
753 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
754 else:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
755 self.fail("should have raised ValueError")
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
756
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
757 try: # not iterable
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
758 a, b, c = len
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
759 except TypeError:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
760 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
761 else:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
762 self.fail("should have raised TypeError")
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
763
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
764 a, b, c = {1: 42, 2: 42, 3: 42}.itervalues()
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
765 self.assertEqual((a, b, c), (42, 42, 42))
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
766
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
767 f = open(TESTFN, "w")
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
768 lines = ("a\n", "bb\n", "ccc\n")
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
769 try:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
770 for line in lines:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
771 f.write(line)
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
772 finally:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
773 f.close()
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
774 f = open(TESTFN, "r")
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
775 try:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
776 a, b, c = f
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
777 self.assertEqual((a, b, c), lines)
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
778 finally:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
779 f.close()
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
780 try:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
781 unlink(TESTFN)
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
782 except OSError:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
783 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
784
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
785 (a, b), (c,) = IteratingSequenceClass(2), {42: 24}
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
786 self.assertEqual((a, b, c), (0, 1, 42))
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
787
21170
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
788 # Test reference count behavior
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
789
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
790 class C(object):
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
791 count = 0
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
792 def __new__(cls):
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
793 cls.count += 1
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
794 return object.__new__(cls)
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
795 def __del__(self):
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
796 cls = self.__class__
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
797 assert cls.count > 0
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
798 cls.count -= 1
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
799 x = C()
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
800 self.assertEqual(C.count, 1)
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
801 del x
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
802 self.assertEqual(C.count, 0)
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
803 l = [C(), C(), C()]
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
804 self.assertEqual(C.count, 3)
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
805 try:
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
806 a, b = iter(l)
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
807 except ValueError:
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
808 pass
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
809 del l
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
810 self.assertEqual(C.count, 0)
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
811
24265
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
812
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
813 # Make sure StopIteration is a "sink state".
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
814 # This tests various things that weren't sink states in Python 2.2.1,
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
815 # plus various things that always were fine.
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
816
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
817 def test_sinkstate_list(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
818 # This used to fail
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
819 a = range(5)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
820 b = iter(a)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
821 self.assertEqual(list(b), range(5))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
822 a.extend(range(5, 10))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
823 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
824
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
825 def test_sinkstate_tuple(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
826 a = (0, 1, 2, 3, 4)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
827 b = iter(a)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
828 self.assertEqual(list(b), range(5))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
829 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
830
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
831 def test_sinkstate_string(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
832 a = "abcde"
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
833 b = iter(a)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
834 self.assertEqual(list(b), ['a', 'b', 'c', 'd', 'e'])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
835 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
836
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
837 def test_sinkstate_sequence(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
838 # This used to fail
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
839 a = SequenceClass(5)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
840 b = iter(a)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
841 self.assertEqual(list(b), range(5))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
842 a.n = 10
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
843 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
844
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
845 def test_sinkstate_callable(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
846 # This used to fail
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
847 def spam(state=[0]):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
848 i = state[0]
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
849 state[0] = i+1
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
850 if i == 10:
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
851 raise AssertionError, "shouldn't have gotten this far"
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
852 return i
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
853 b = iter(spam, 5)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
854 self.assertEqual(list(b), range(5))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
855 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
856
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
857 def test_sinkstate_dict(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
858 # XXX For a more thorough test, see towards the end of:
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
859 # http://mail.python.org/pipermail/python-dev/2002-July/026512.html
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
860 a = {1:1, 2:2, 0:0, 4:4, 3:3}
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
861 for b in iter(a), a.iterkeys(), a.iteritems(), a.itervalues():
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
862 b = iter(a)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
863 self.assertEqual(len(list(b)), 5)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
864 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
865
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
866 def test_sinkstate_yield(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
867 def gen():
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
868 for i in range(5):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
869 yield i
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
870 b = gen()
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
871 self.assertEqual(list(b), range(5))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
872 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
873
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
874 def test_sinkstate_range(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
875 a = xrange(5)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
876 b = iter(a)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
877 self.assertEqual(list(b), range(5))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
878 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
879
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
880 def test_sinkstate_enumerate(self):
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
881 a = range(5)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
882 e = enumerate(a)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
883 b = iter(e)
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
884 self.assertEqual(list(b), zip(range(5), range(5)))
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
885 self.assertEqual(list(b), [])
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
886
50754
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
887 def test_3720(self):
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
888 # Avoid a crash, when an iterator deletes its next() method.
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
889 class BadIterator(object):
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
890 def __iter__(self):
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
891 return self
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
892 def next(self):
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
893 del BadIterator.next
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
894 return 1
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
895
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
896 try:
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
897 for i in BadIterator() :
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
898 pass
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
899 except TypeError:
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
900 pass
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
901
24265
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
902
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
903 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
904 run_unittest(TestCase)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
905
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
906
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
907 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
908 test_main()