annotate Lib/test/test_iter.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children 4624570c1f19
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
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
4 from test.test_support import run_unittest, TESTFN, unlink, have_unicode, \
62451
4dd8cff6b7be mark ref counting as impl detail
Benjamin Peterson <benjamin@python.org>
parents: 62449
diff changeset
5 check_py3k_warnings, cpython_only
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # Test result of triple loop (too big to inline)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 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
9 (0, 1, 0), (0, 1, 1), (0, 1, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 (0, 2, 0), (0, 2, 1), (0, 2, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 (1, 0, 0), (1, 0, 1), (1, 0, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 (1, 1, 0), (1, 1, 1), (1, 1, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 (1, 2, 0), (1, 2, 1), (1, 2, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 (2, 0, 0), (2, 0, 1), (2, 0, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 (2, 1, 0), (2, 1, 1), (2, 1, 2),
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 (2, 2, 0), (2, 2, 1), (2, 2, 2)]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # Helper classes
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 class BasicIterClass:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 def __init__(self, n):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 self.n = n
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 self.i = 0
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 def next(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 res = self.i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 if res >= self.n:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 raise StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 self.i = res + 1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 return res
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 class IteratingSequenceClass:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def __init__(self, n):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 self.n = n
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 def __iter__(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 return BasicIterClass(self.n)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 class SequenceClass:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 def __init__(self, n):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 self.n = n
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def __getitem__(self, i):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 if 0 <= i < self.n:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 else:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 raise IndexError
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # Main test suite
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 class TestCase(unittest.TestCase):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # 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
53 def check_iterator(self, it, seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 while 1:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 val = it.next()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 except StopIteration:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 break
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 res.append(val)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 self.assertEqual(res, seq)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # 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
64 def check_for_loop(self, expr, seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 for val in expr:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 res.append(val)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 self.assertEqual(res, seq)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 # Test basic use of iter() function
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 def test_iter_basic(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 self.check_iterator(iter(range(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # 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
75 def test_iter_idempotency(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 seq = range(10)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 it = iter(seq)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 it2 = iter(it)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50754
diff changeset
79 self.assertTrue(it is it2)
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # Test that for loops over iterators work
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 def test_iter_for_loop(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 self.check_for_loop(iter(range(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 # Test several independent iterators over the same list
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def test_iter_independence(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 seq = range(3)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 for i in iter(seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 for j in iter(seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 for k in iter(seq):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 res.append((i, j, k))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 self.assertEqual(res, TRIPLETS)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 # Test triple list comprehension using iterators
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def test_nested_comprehensions_iter(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 seq = range(3)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 res = [(i, j, k)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 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
100 self.assertEqual(res, TRIPLETS)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 # Test triple list comprehension without iterators
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def test_nested_comprehensions_for(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 seq = range(3)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 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
106 self.assertEqual(res, TRIPLETS)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 # Test a class with __iter__ in a for loop
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 def test_iter_class_for(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self.check_for_loop(IteratingSequenceClass(10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # Test a class with __iter__ with explicit iter()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def test_iter_class_iter(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 self.check_iterator(iter(IteratingSequenceClass(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 # Test for loop on a sequence class without __iter__
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 def test_seq_class_for(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 self.check_for_loop(SequenceClass(10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 # Test iter() on a sequence class without __iter__
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def test_seq_class_iter(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 self.check_iterator(iter(SequenceClass(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
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
124 # 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
125 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
126 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
127 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
128 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
129 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
130
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 # Test two-argument iter() with callable instance
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 def test_iter_callable(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 class C:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 def __init__(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 self.i = 0
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 def __call__(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 i = self.i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 self.i = i + 1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if i > 100:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 raise IndexError # Emergency stop
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 self.check_iterator(iter(C(), 10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 # Test two-argument iter() with function
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 def test_iter_function(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 def spam(state=[0]):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 i = state[0]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 state[0] = i+1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 self.check_iterator(iter(spam, 10), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 # Test two-argument iter() with function that raises StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 def test_iter_function_stop(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 def spam(state=[0]):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 i = state[0]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 raise StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 state[0] = i+1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 self.check_iterator(iter(spam, 20), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 # Test exception propagation through function iterator
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def test_exception_function(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 def spam(state=[0]):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 i = state[0]
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 state[0] = i+1
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 raise RuntimeError
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 return i
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 for x in iter(spam, 20):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 res.append(x)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 except RuntimeError:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 self.assertEqual(res, range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 else:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 self.fail("should have raised RuntimeError")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 # Test exception propagation through sequence iterator
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 def test_exception_sequence(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 class MySequenceClass(SequenceClass):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 def __getitem__(self, i):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 raise RuntimeError
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 return SequenceClass.__getitem__(self, i)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 res = []
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 for x in MySequenceClass(20):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 res.append(x)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 except RuntimeError:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 self.assertEqual(res, range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 else:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 self.fail("should have raised RuntimeError")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 # Test for StopIteration from __getitem__
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 def test_stop_sequence(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 class MySequenceClass(SequenceClass):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 def __getitem__(self, i):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 if i == 10:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 raise StopIteration
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 return SequenceClass.__getitem__(self, i)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 self.check_for_loop(MySequenceClass(20), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 # Test a big range
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 def test_iter_big_range(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 self.check_for_loop(iter(range(10000)), range(10000))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 # Test an empty list
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 def test_iter_empty(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 self.check_for_loop(iter([]), [])
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 # Test a tuple
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 def test_iter_tuple(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 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
215
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 # Test an xrange
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 def test_iter_xrange(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 self.check_for_loop(iter(xrange(10)), range(10))
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 # Test a string
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 def test_iter_string(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 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
223
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 # Test a Unicode string
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
225 if have_unicode:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
226 def test_iter_unicode(self):
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
227 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
228 [unicode("a"), unicode("b"), unicode("c"),
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
229 unicode("d"), unicode("e")])
17610
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 # Test a directory
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 def test_iter_dict(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 dict = {}
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 for i in range(10):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 dict[i] = None
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 self.check_for_loop(dict, dict.keys())
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 # Test a file
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 def test_iter_file(self):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 f = open(TESTFN, "w")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 for i in range(5):
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 f.write("%d\n" % i)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 finally:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 f.close()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 f = open(TESTFN, "r")
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 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
249 self.check_for_loop(f, [])
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 finally:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 f.close()
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 try:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 unlink(TESTFN)
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 except OSError:
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 pass
624fc9713d18 Add test suite for iterators.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
17648
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
257 # 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
258 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
259 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
260 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
261 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
262 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
263
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
264 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
265 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
266
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, list)
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
268 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
269
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
270 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
271 try:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
272 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
273 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
274 finally:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
275 f.close()
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
276 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
277 try:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
278 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
279 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
280 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
281 ["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
282 finally:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
283 f.close()
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
284 try:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
285 unlink(TESTFN)
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
286 except OSError:
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
287 pass
6c82277e77f3 Generalize list(seq) to work with iterators. This also generalizes list()
Tim Peters <tim.peters@gmail.com>
parents: 17610
diff changeset
288
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
289 # Test tuples()'s use of iterators.
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
290 def test_builtin_tuple(self):
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
291 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
292 self.assertEqual(tuple(SequenceClass(0)), ())
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(()), ())
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
295 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
296
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
297 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
298 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
299
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
300 self.assertRaises(TypeError, tuple, list)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
301 self.assertRaises(TypeError, tuple, 42)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
302
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
303 f = open(TESTFN, "w")
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
304 try:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
305 for i in range(5):
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
306 f.write("%d\n" % i)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
307 finally:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
308 f.close()
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
309 f = open(TESTFN, "r")
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
310 try:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
311 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
312 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
313 self.assertEqual(tuple(f),
17684
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
314 ("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
315 finally:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
316 f.close()
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
317 try:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
318 unlink(TESTFN)
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
319 except OSError:
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
320 pass
399d99601b2d Generalize tuple() to work nicely with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
321
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
322 # 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
323 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
324 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
325 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
326 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
327 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
328
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
329 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
330 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
331
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, list)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
333 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
334
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
335 class Boolean:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
336 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
337 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
338 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
339 return self.truth
26755
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
340 bTrue = Boolean(1)
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
341 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
342
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
343 class Seq:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
344 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
345 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
346 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
347 class SeqIter:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
348 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
349 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
350 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
351 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
352 return self
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
353 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
354 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
355 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
356 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
357 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
358 else:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
359 raise StopIteration
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17648
diff changeset
360 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
361
26755
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
362 seq = Seq(*([bTrue, 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, seq), [bFalse]*25)
95bc0c052034 Don't rebind True and False.
Tim Peters <tim.peters@gmail.com>
parents: 24602
diff changeset
364 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
365
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
366 # 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
367 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
368 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
369 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
370 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
371 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
372
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
373 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
374 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
375 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
376 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
377 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
378
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
379 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
380 try:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
381 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
382 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
383 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
384 finally:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
385 f.close()
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
386 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
387 try:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
388 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
389 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
390 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
391 finally:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
392 f.close()
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
393 try:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
394 unlink(TESTFN)
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
395 except OSError:
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
396 pass
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
397
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
398 # Test map()'s use of iterators.
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
399 def test_builtin_map(self):
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(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
404 dkeys = d.keys()
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
405 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
406 i,
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
407 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
408 for i in range(5)]
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
409
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
410 # Deprecated map(None, ...)
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
411 with check_py3k_warnings():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
412 self.assertEqual(map(None, SequenceClass(5)), range(5))
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
413 self.assertEqual(map(None, d), d.keys())
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
414 self.assertEqual(map(None, d,
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
415 SequenceClass(5),
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
416 iter(d.iterkeys())),
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
417 expected)
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
418
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
419 f = open(TESTFN, "w")
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
420 try:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
421 for i in range(10):
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
422 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
423 finally:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
424 f.close()
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
425 f = open(TESTFN, "r")
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 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
428 finally:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
429 f.close()
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
430 try:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
431 unlink(TESTFN)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
432 except OSError:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
433 pass
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17668
diff changeset
434
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
435 # Test zip()'s use of iterators.
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
436 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
437 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
438 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
439 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
440
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
441 self.assertRaises(TypeError, zip, None)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
442 self.assertRaises(TypeError, zip, range(10), 42)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
443 self.assertRaises(TypeError, zip, range(10), zip)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
444
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
445 self.assertEqual(zip(IteratingSequenceClass(3)),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
446 [(0,), (1,), (2,)])
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
447 self.assertEqual(zip(SequenceClass(3)),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
448 [(0,), (1,), (2,)])
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
449
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
450 d = {"one": 1, "two": 2, "three": 3}
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
451 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
452
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
453 # Generate all ints starting at constructor arg.
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
454 class IntsFrom:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
455 def __init__(self, start):
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
456 self.i = start
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
457
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
458 def __iter__(self):
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
459 return self
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
460
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
461 def next(self):
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
462 i = self.i
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
463 self.i = i+1
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
464 return i
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
465
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
466 f = open(TESTFN, "w")
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
467 try:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
468 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
469 finally:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
470 f.close()
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
471 f = open(TESTFN, "r")
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
472 try:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
473 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
474 [(0, "a\n", -100),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
475 (1, "bbb\n", -99),
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
476 (2, "cc\n", -98)])
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
477 finally:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
478 f.close()
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
479 try:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
480 unlink(TESTFN)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
481 except OSError:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
482 pass
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17693
diff changeset
483
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
484 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
485
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 # 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
487 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
488 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
489 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
490 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
491 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
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 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
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 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
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 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
498 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
499 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
500
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(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
502 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
503 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
504 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
505 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
506
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 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
508 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
509 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
510 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
511
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
512 # Test reduces()'s use of iterators.
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
513 def test_deprecated_builtin_reduce(self):
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58460
diff changeset
514 with check_py3k_warnings():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
515 self._test_builtin_reduce()
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
516
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 30037
diff changeset
517 def _test_builtin_reduce(self):
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
518 from operator import add
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
519 self.assertEqual(reduce(add, SequenceClass(5)), 10)
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
520 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
521 self.assertRaises(TypeError, reduce, add, SequenceClass(0))
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
522 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
523 self.assertEqual(reduce(add, SequenceClass(1)), 0)
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
524 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
525
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
526 d = {"one": 1, "two": 2, "three": 3}
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17676
diff changeset
527 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
528
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
529 # 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
530 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
531
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
532 # 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
533 # 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
534 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
535 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
536 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
537 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
538
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
539 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
540 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
541
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
542 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
543 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
544 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
545 if i == 2:
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
546 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
547 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
548
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
549 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
550 try:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
551 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
552 finally:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
553 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
554
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
555 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
556 # 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
557 # 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
558 # 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
559 # 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
560 # 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
561 try:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
562 got = " - ".join(OhPhooey(f))
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
563 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
564 finally:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
565 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
566 try:
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17684
diff changeset
567 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
568 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
569 pass
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
570 if not have_unicode:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18195
diff changeset
571 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
572
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
573 # 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
574 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
575 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
576 for i in range(5):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
577 self.assertIn(i, sc5)
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17690
diff changeset
578 for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
579 self.assertNotIn(i, 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
580
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.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
582 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
583
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 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
585 for k in d:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
586 self.assertIn(k, d)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
587 self.assertNotIn(k, d.itervalues())
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
588 for v in d.values():
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
589 self.assertIn(v, d.itervalues())
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
590 self.assertNotIn(v, d)
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
591 for k, v in d.iteritems():
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
592 self.assertIn((k, v), d.iteritems())
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
593 self.assertNotIn((v, k), d.iteritems())
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
594
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 = 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
596 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
597 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
598 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
599 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
600 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
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 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
603 f.seek(0, 0)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
604 self.assertNotIn(chunk, f)
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
605 f.seek(0, 0)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
606 self.assertIn((chunk + "\n"), f)
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
607 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
608 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
609 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
610 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
611 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
612 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
613
17690
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
614 # 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
615 def test_countOf(self):
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
616 from operator import countOf
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
617 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
618 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
619 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
620 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
621
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
622 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
623 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
624
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
625 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
626 for k in d:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
627 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
628 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
629 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
630 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
631
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
632 f = open(TESTFN, "w")
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
633 try:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
634 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
635 finally:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
636 f.close()
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
637 f = open(TESTFN, "r")
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
638 try:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
639 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
640 f.seek(0, 0)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
641 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
642 finally:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
643 f.close()
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
644 try:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
645 unlink(TESTFN)
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
646 except OSError:
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
647 pass
ee4d17862b61 Generalize PySequence_Count() (operator.countOf) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
648
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
649 # 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
650 def test_indexOf(self):
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
651 from operator import indexOf
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
652 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
653 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
654 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
655 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
656 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
657 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
658
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
659 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
660 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
661 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
662
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
663 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
664 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
665
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
666 f = open(TESTFN, "w")
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
667 try:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
668 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
669 finally:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
670 f.close()
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
671 f = open(TESTFN, "r")
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 fiter = iter(f)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
674 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
675 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
676 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
677 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
678 finally:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
679 f.close()
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
680 try:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
681 unlink(TESTFN)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
682 except OSError:
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
683 pass
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
684
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
685 iclass = IteratingSequenceClass(3)
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
686 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
687 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
688 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
689
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
690 # Test iterators with file.writelines().
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
691 def test_writelines(self):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
692 f = file(TESTFN, "w")
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
693
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
694 try:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
695 self.assertRaises(TypeError, f.writelines, None)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
696 self.assertRaises(TypeError, f.writelines, 42)
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
697
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
698 f.writelines(["1\n", "2\n"])
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
699 f.writelines(("3\n", "4\n"))
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
700 f.writelines({'5\n': None})
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
701 f.writelines({})
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
702
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
703 # Try a big chunk too.
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
704 class Iterator:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
705 def __init__(self, start, finish):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
706 self.start = start
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
707 self.finish = finish
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
708 self.i = self.start
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
709
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
710 def next(self):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
711 if self.i >= self.finish:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
712 raise StopIteration
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
713 result = str(self.i) + '\n'
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
714 self.i += 1
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
715 return result
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 self
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
719
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
720 class Whatever:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
721 def __init__(self, start, finish):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
722 self.start = start
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
723 self.finish = finish
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
724
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
725 def __iter__(self):
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
726 return Iterator(self.start, self.finish)
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
727
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
728 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
729 f.close()
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
730
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
731 f = file(TESTFN)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
732 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
733 self.assertEqual(list(f), expected)
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20045
diff changeset
734
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
735 finally:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
736 f.close()
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
737 try:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
738 unlink(TESTFN)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
739 except OSError:
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
740 pass
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
741
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20013
diff changeset
742
18195
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
743 # 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
744 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
745 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
746 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
747
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
748 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
749 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
750
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
751 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
752 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
753 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
754 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
755 else:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
756 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
757
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
758 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
759 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
760 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
761 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
762 else:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
763 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
764
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
765 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
766 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
767 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
768 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
769 else:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
770 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
771
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
772 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
773 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
774
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
775 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
776 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
777 try:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
778 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
779 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
780 finally:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
781 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
782 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
783 try:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
784 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
785 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
786 finally:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
787 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
788 try:
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
789 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
790 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
791 pass
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
792
36e825660a24 Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
Tim Peters <tim.peters@gmail.com>
parents: 17694
diff changeset
793 (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
794 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
795
21170
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
796
62451
4dd8cff6b7be mark ref counting as impl detail
Benjamin Peterson <benjamin@python.org>
parents: 62449
diff changeset
797 @cpython_only
4dd8cff6b7be mark ref counting as impl detail
Benjamin Peterson <benjamin@python.org>
parents: 62449
diff changeset
798 def test_ref_counting_behavior(self):
21170
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
799 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
800 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
801 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
802 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
803 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
804 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
805 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
806 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
807 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
808 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
809 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
810 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
811 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
812 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
813 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
814 try:
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
815 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
816 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
817 pass
2dfc600f99ce unpack_iterable(): Add a missing DECREF in an error case. Reported by
Guido van Rossum <guido@python.org>
parents: 20240
diff changeset
818 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
819 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
820
24265
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
821
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
822 # 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
823 # 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
824 # 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
825
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
826 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
827 # 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
828 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
829 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
830 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
831 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
832 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
833
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
834 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
835 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
836 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
837 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
838 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
839
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
840 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
841 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
842 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
843 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
844 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
845
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
846 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
847 # 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
848 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
849 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
850 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
851 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
852 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
853
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
854 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
855 # 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
856 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
857 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
858 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
859 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
860 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
861 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
862 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
863 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
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_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
867 # 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
868 # 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
869 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
870 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
871 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
872 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
873 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
874
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
875 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
876 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
877 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
878 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
879 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
880 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
881 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
882
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
883 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
884 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
885 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
886 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
887 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
888
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
889 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
890 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
891 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
892 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
893 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
894 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
895
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
896 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
897 # 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
898 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
899 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
900 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
901 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
902 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
903 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
904
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 30037
diff changeset
905 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
906 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
907 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
908 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
909 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
910
24265
4a5c6f69447d Bunch of tests to make sure that StopIteration is a sink state.
Guido van Rossum <guido@python.org>
parents: 23305
diff changeset
911
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
912 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
913 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
914
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
915
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19808
diff changeset
916 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
917 test_main()