blob: 25b94e973889fcf9b100e0a19a3536cdf6bee34e [file] [log] [blame]
Barry Warsaw09f95471997-08-25 22:17:45 +00001from test_support import *
2
3t = (1, 2, 3)
4l = [4, 5, 6]
5
6class Seq:
7 def __getitem__(self, i):
Guido van Rossum41360a41998-03-26 19:42:58 +00008 if i >= 0 and i < 3: return i
9 raise IndexError
Barry Warsaw09f95471997-08-25 22:17:45 +000010
11a = -1
12b = -1
13c = -1
14
15# unpack tuple
16if verbose:
17 print 'unpack tuple'
18a, b, c = t
19if a <> 1 or b <> 2 or c <> 3:
20 raise TestFailed
21
22# unpack list
23if verbose:
24 print 'unpack list'
25a, b, c = l
26if a <> 4 or b <> 5 or c <> 6:
27 raise TestFailed
28
29# unpack implied tuple
30if verbose:
31 print 'unpack implied tuple'
32a, b, c = 7, 8, 9
33if a <> 7 or b <> 8 or c <> 9:
34 raise TestFailed
35
36# unpack string... fun!
37if verbose:
38 print 'unpack string'
39a, b, c = 'one'
40if a <> 'o' or b <> 'n' or c <> 'e':
41 raise TestFailed
42
43# unpack generic sequence
44if verbose:
45 print 'unpack sequence'
46a, b, c = Seq()
47if a <> 0 or b <> 1 or c <> 2:
48 raise TestFailed
49
Barry Warsaw0157e7a2000-10-12 14:45:58 +000050# single element unpacking, with extra syntax
51if verbose:
52 print 'unpack single tuple/list'
53st = (99,)
54sl = [100]
55a, = st
56if a <> 99:
57 raise TestFailed
58b, = sl
59if b <> 100:
60 raise TestFailed
61
Barry Warsaw09f95471997-08-25 22:17:45 +000062# now for some failures
63
64# unpacking non-sequence
65if verbose:
66 print 'unpack non-sequence'
67try:
68 a, b, c = 7
69 raise TestFailed
70except TypeError:
71 pass
72
73
74# unpacking tuple of wrong size
75if verbose:
76 print 'unpack tuple wrong size'
77try:
78 a, b = t
79 raise TestFailed
80except ValueError:
81 pass
82
83# unpacking list of wrong size
84if verbose:
85 print 'unpack list wrong size'
86try:
87 a, b = l
88 raise TestFailed
89except ValueError:
90 pass
91
92
93# unpacking sequence too short
94if verbose:
95 print 'unpack sequence too short'
96try:
97 a, b, c, d = Seq()
98 raise TestFailed
99except ValueError:
100 pass
101
102
103# unpacking sequence too long
104if verbose:
105 print 'unpack sequence too long'
106try:
107 a, b = Seq()
108 raise TestFailed
109except ValueError:
110 pass
111
112
113# unpacking a sequence where the test for too long raises a different
114# kind of error
Fred Drakeb65b0062000-08-18 14:50:20 +0000115class BozoError(Exception):
116 pass
Barry Warsaw09f95471997-08-25 22:17:45 +0000117
118class BadSeq:
119 def __getitem__(self, i):
Guido van Rossum41360a41998-03-26 19:42:58 +0000120 if i >= 0 and i < 3:
121 return i
122 elif i == 3:
123 raise BozoError
124 else:
125 raise IndexError
Barry Warsaw09f95471997-08-25 22:17:45 +0000126
127
128# trigger code while not expecting an IndexError
129if verbose:
130 print 'unpack sequence too long, wrong error'
131try:
132 a, b, c, d, e = BadSeq()
133 raise TestFailed
134except BozoError:
135 pass
136
137# trigger code while expecting an IndexError
138if verbose:
139 print 'unpack sequence too short, wrong error'
140try:
141 a, b, c = BadSeq()
142 raise TestFailed
143except BozoError:
144 pass