Skip to content

Commit be8c71b

Browse files
CPython devyouknowone
authored andcommitted
Lib/test/test_unpack.py from CPython 3.8.2
1 parent 72b28e4 commit be8c71b

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

Lib/test/test_unpack.py

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

0 commit comments

Comments
 (0)