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