blob: ae2dcbd985ed5a93f672ac58023ae6e694da7365 [file] [log] [blame]
Guido van Rossum0368b722007-05-11 16:50:42 +00001# Tests for extended unpacking, starred expressions.
2
3doctests = """
4
5Unpack tuple
6
7 >>> t = (1, 2, 3)
8 >>> a, *b, c = t
9 >>> a == 1 and b == [2] and c == 3
10 True
11
12Unpack list
13
14 >>> l = [4, 5, 6]
15 >>> a, *b = l
16 >>> a == 4 and b == [5, 6]
17 True
18
19Unpack implied tuple
20
21 >>> *a, = 7, 8, 9
22 >>> a == [7, 8, 9]
23 True
24
25Unpack string... fun!
26
27 >>> a, *b = 'one'
28 >>> a == 'o' and b == ['n', 'e']
29 True
30
31Unpack long sequence
32
33 >>> a, b, c, *d, e, f, g = range(10)
34 >>> (a, b, c, d, e, f, g) == (0, 1, 2, [3, 4, 5, 6], 7, 8, 9)
35 True
36
37Unpack short sequence
38
39 >>> a, *b, c = (1, 2)
40 >>> a == 1 and c == 2 and b == []
41 True
42
43Unpack generic sequence
44
45 >>> class Seq:
46 ... def __getitem__(self, i):
47 ... if i >= 0 and i < 3: return i
48 ... raise IndexError
49 ...
50 >>> a, *b = Seq()
51 >>> a == 0 and b == [1, 2]
52 True
53
54Unpack in for statement
55
56 >>> for a, *b, c in [(1,2,3), (4,5,6,7)]:
57 ... print(a, b, c)
58 ...
59 1 [2] 3
60 4 [5, 6] 7
61
62Unpack in list
63
64 >>> [a, *b, c] = range(5)
65 >>> a == 0 and b == [1, 2, 3] and c == 4
66 True
67
68Multiple targets
69
70 >>> a, *b, c = *d, e = range(5)
71 >>> a == 0 and b == [1, 2, 3] and c == 4 and d == [0, 1, 2, 3] and e == 4
72 True
73
74Now for some failures
75
76Unpacking non-sequence
77
78 >>> a, *b = 7
79 Traceback (most recent call last):
80 ...
81 TypeError: 'int' object is not iterable
82
83Unpacking sequence too short
84
85 >>> a, *b, c, d, e = Seq()
86 Traceback (most recent call last):
87 ...
88 ValueError: need more than 3 values to unpack
89
90Unpacking a sequence where the test for too long raises a different kind of
91error
92
93 >>> class BozoError(Exception):
94 ... pass
95 ...
96 >>> class BadSeq:
97 ... def __getitem__(self, i):
98 ... if i >= 0 and i < 3:
99 ... return i
100 ... elif i == 3:
101 ... raise BozoError
102 ... else:
103 ... raise IndexError
104 ...
105
106Trigger code while not expecting an IndexError (unpack sequence too long, wrong
107error)
108
109 >>> a, *b, c, d, e = BadSeq()
110 Traceback (most recent call last):
111 ...
112 test.test_unpack_ex.BozoError
113
114Now some general starred expressions (all fail).
115
116 >>> a, *b, c, *d, e = range(10) # doctest:+ELLIPSIS
117 Traceback (most recent call last):
118 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000119 SyntaxError: two starred expressions in assignment
Guido van Rossum0368b722007-05-11 16:50:42 +0000120
121 >>> [*b, *c] = range(10) # doctest:+ELLIPSIS
122 Traceback (most recent call last):
123 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000124 SyntaxError: two starred expressions in assignment
Guido van Rossum0368b722007-05-11 16:50:42 +0000125
126 >>> *a = range(10) # doctest:+ELLIPSIS
127 Traceback (most recent call last):
128 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000129 SyntaxError: starred assignment target must be in a list or tuple
Guido van Rossum0368b722007-05-11 16:50:42 +0000130
131 >>> *a # doctest:+ELLIPSIS
132 Traceback (most recent call last):
133 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000134 SyntaxError: can use starred expression only as assignment target
Guido van Rossum0368b722007-05-11 16:50:42 +0000135
136 >>> *1 # doctest:+ELLIPSIS
137 Traceback (most recent call last):
138 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000139 SyntaxError: can use starred expression only as assignment target
Guido van Rossum0368b722007-05-11 16:50:42 +0000140
141 >>> x = *a # doctest:+ELLIPSIS
142 Traceback (most recent call last):
143 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000144 SyntaxError: can use starred expression only as assignment target
Guido van Rossum0368b722007-05-11 16:50:42 +0000145
Thomas Woutersdeef6742008-03-14 17:16:59 +0000146Some size constraints (all fail.)
147
148 >>> s = ", ".join("a%d" % i for i in range(1<<8)) + ", *rest = range(1<<8 + 1)"
149 >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
150 Traceback (most recent call last):
151 ...
152 SyntaxError: too many expressions in star-unpacking assignment
153
154 >>> s = ", ".join("a%d" % i for i in range(1<<8 + 1)) + ", *rest = range(1<<8 + 2)"
155 >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
156 Traceback (most recent call last):
157 ...
158 SyntaxError: too many expressions in star-unpacking assignment
159
160(there is an additional limit, on the number of expressions after the
161'*rest', but it's 1<<24 and testing it takes too much memory.)
162
Guido van Rossum0368b722007-05-11 16:50:42 +0000163"""
164
165__test__ = {'doctests' : doctests}
166
167def test_main(verbose=False):
168 import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000169 from test import support
Guido van Rossum0368b722007-05-11 16:50:42 +0000170 from test import test_unpack_ex
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000171 support.run_doctest(test_unpack_ex, verbose)
Guido van Rossum0368b722007-05-11 16:50:42 +0000172
173if __name__ == "__main__":
174 test_main(verbose=True)