ยปCore Development>Code coverage>Lib/test/test_iter.py

# Python code coverage for Lib/test/test_iter.py

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