Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Lib / test / test_unpack.py
blob7ddbc6211844182309ac72e0ab2740bd5ba6fec7
1 doctests = """
3 Unpack tuple
5 >>> t = (1, 2, 3)
6 >>> a, b, c = t
7 >>> a == 1 and b == 2 and c == 3
8 True
10 Unpack list
12 >>> l = [4, 5, 6]
13 >>> a, b, c = l
14 >>> a == 4 and b == 5 and c == 6
15 True
17 Unpack implied tuple
19 >>> a, b, c = 7, 8, 9
20 >>> a == 7 and b == 8 and c == 9
21 True
23 Unpack string... fun!
25 >>> a, b, c = 'one'
26 >>> a == 'o' and b == 'n' and c == 'e'
27 True
29 Unpack generic sequence
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
40 Single element unpacking, with extra syntax
42 >>> st = (99,)
43 >>> sl = [100]
44 >>> a, = st
45 >>> a
47 >>> b, = sl
48 >>> b
49 100
51 Now for some failures
53 Unpacking non-sequence
55 >>> a, b, c = 7
56 Traceback (most recent call last):
57 ...
58 TypeError: 'int' object is not iterable
60 Unpacking tuple of wrong size
62 >>> a, b = t
63 Traceback (most recent call last):
64 ...
65 ValueError: too many values to unpack
67 Unpacking tuple of wrong size
69 >>> a, b = l
70 Traceback (most recent call last):
71 ...
72 ValueError: too many values to unpack
74 Unpacking sequence too short
76 >>> a, b, c, d = Seq()
77 Traceback (most recent call last):
78 ...
79 ValueError: need more than 3 values to unpack
81 Unpacking sequence too long
83 >>> a, b = Seq()
84 Traceback (most recent call last):
85 ...
86 ValueError: too many values to unpack
88 Unpacking a sequence where the test for too long raises a different kind of
89 error
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
104 Trigger code while not expecting an IndexError (unpack sequence too long, wrong
105 error)
107 >>> a, b, c, d, e = BadSeq()
108 Traceback (most recent call last):
110 BozoError
112 Trigger code while expecting an IndexError (unpack sequence too short, wrong
113 error)
115 >>> a, b, c = BadSeq()
116 Traceback (most recent call last):
118 BozoError
122 __test__ = {'doctests' : doctests}
124 def test_main(verbose=False):
125 from test import test_support
126 from test import test_unpack
127 test_support.run_doctest(test_unpack, verbose)
129 if __name__ == "__main__":
130 test_main(verbose=True)