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

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

#countcontent
2n/a
3n/aUnpack tuple
4n/a
5n/a >>> t = (1, 2, 3)
6n/a >>> a, b, c = t
7n/a >>> a == 1 and b == 2 and c == 3
8n/a True
9n/a
10n/aUnpack list
11n/a
12n/a >>> l = [4, 5, 6]
13n/a >>> a, b, c = l
14n/a >>> a == 4 and b == 5 and c == 6
15n/a True
16n/a
17n/aUnpack implied tuple
18n/a
19n/a >>> a, b, c = 7, 8, 9
20n/a >>> a == 7 and b == 8 and c == 9
21n/a True
22n/a
23n/aUnpack string... fun!
24n/a
25n/a >>> a, b, c = 'one'
26n/a >>> a == 'o' and b == 'n' and c == 'e'
27n/a True
28n/a
29n/aUnpack generic sequence
30n/a
31n/a >>> class Seq:
32n/a ... def __getitem__(self, i):
33n/a ... if i >= 0 and i < 3: return i
34n/a ... raise IndexError
35n/a ...
36n/a >>> a, b, c = Seq()
37n/a >>> a == 0 and b == 1 and c == 2
38n/a True
39n/a
40n/aSingle element unpacking, with extra syntax
41n/a
42n/a >>> st = (99,)
43n/a >>> sl = [100]
44n/a >>> a, = st
45n/a >>> a
46n/a 99
47n/a >>> b, = sl
48n/a >>> b
49n/a 100
50n/a
51n/aNow for some failures
52n/a
53n/aUnpacking non-sequence
54n/a
55n/a >>> a, b, c = 7
56n/a Traceback (most recent call last):
57n/a ...
58n/a TypeError: 'int' object is not iterable
59n/a
60n/aUnpacking tuple of wrong size
61n/a
62n/a >>> a, b = t
63n/a Traceback (most recent call last):
64n/a ...
65n/a ValueError: too many values to unpack (expected 2)
66n/a
67n/aUnpacking tuple of wrong size
68n/a
69n/a >>> a, b = l
70n/a Traceback (most recent call last):
71n/a ...
72n/a ValueError: too many values to unpack (expected 2)
73n/a
74n/aUnpacking sequence too short
75n/a
76n/a >>> a, b, c, d = Seq()
77n/a Traceback (most recent call last):
78n/a ...
79n/a ValueError: not enough values to unpack (expected 4, got 3)
80n/a
81n/aUnpacking sequence too long
82n/a
83n/a >>> a, b = Seq()
84n/a Traceback (most recent call last):
85n/a ...
86n/a ValueError: too many values to unpack (expected 2)
87n/a
88n/aUnpacking a sequence where the test for too long raises a different kind of
89n/aerror
90n/a
91n/a >>> class BozoError(Exception):
92n/a ... pass
93n/a ...
95n/a ... def __getitem__(self, i):
96n/a ... if i >= 0 and i < 3:
97n/a ... return i
98n/a ... elif i == 3:
99n/a ... raise BozoError
100n/a ... else:
101n/a ... raise IndexError
102n/a ...
103n/a
104n/aTrigger code while not expecting an IndexError (unpack sequence too long, wrong
105n/aerror)
106n/a
107n/a >>> a, b, c, d, e = BadSeq()
108n/a Traceback (most recent call last):
109n/a ...
110n/a test.test_unpack.BozoError
111n/a
112n/aTrigger code while expecting an IndexError (unpack sequence too short, wrong
113n/aerror)
114n/a
115n/a >>> a, b, c = BadSeq()
116n/a Traceback (most recent call last):
117n/a ...
118n/a test.test_unpack.BozoError
119n/a
120n/aAllow unpacking empty iterables
121n/a
122n/a >>> () = []
123n/a >>> [] = ()
124n/a >>> [] = []
125n/a >>> () = ()
126n/a
127n/aUnpacking non-iterables should raise TypeError
128n/a
129n/a >>> () = 42
130n/a Traceback (most recent call last):
131n/a ...
132n/a TypeError: 'int' object is not iterable
133n/a
134n/aUnpacking to an empty iterable should raise ValueError
135n/a
136n/a >>> () = [42]
137n/a Traceback (most recent call last):
138n/a ...
139n/a ValueError: too many values to unpack (expected 0)
140n/a
141n/a"""
142n/a
143n/a__test__ = {'doctests' : doctests}
144n/a