Remove a ?? in the description of Mac OS support.
[python/dscho.git] / Lib / dos-8x3 / test_unp.py
blob25324c053a3fb87d0663114baaba7c27728722b8
1 from test_support import *
3 t = (1, 2, 3)
4 l = [4, 5, 6]
6 class Seq:
7 def __getitem__(self, i):
8 if i >= 0 and i < 3: return i
9 raise IndexError
11 a = -1
12 b = -1
13 c = -1
15 # unpack tuple
16 if verbose:
17 print 'unpack tuple'
18 a, b, c = t
19 if a <> 1 or b <> 2 or c <> 3:
20 raise TestFailed
22 # unpack list
23 if verbose:
24 print 'unpack list'
25 a, b, c = l
26 if a <> 4 or b <> 5 or c <> 6:
27 raise TestFailed
29 # unpack implied tuple
30 if verbose:
31 print 'unpack implied tuple'
32 a, b, c = 7, 8, 9
33 if a <> 7 or b <> 8 or c <> 9:
34 raise TestFailed
36 # unpack string... fun!
37 if verbose:
38 print 'unpack string'
39 a, b, c = 'one'
40 if a <> 'o' or b <> 'n' or c <> 'e':
41 raise TestFailed
43 # unpack generic sequence
44 if verbose:
45 print 'unpack sequence'
46 a, b, c = Seq()
47 if a <> 0 or b <> 1 or c <> 2:
48 raise TestFailed
50 # now for some failures
52 # unpacking non-sequence
53 if verbose:
54 print 'unpack non-sequence'
55 try:
56 a, b, c = 7
57 raise TestFailed
58 except TypeError:
59 pass
62 # unpacking tuple of wrong size
63 if verbose:
64 print 'unpack tuple wrong size'
65 try:
66 a, b = t
67 raise TestFailed
68 except ValueError:
69 pass
71 # unpacking list of wrong size
72 if verbose:
73 print 'unpack list wrong size'
74 try:
75 a, b = l
76 raise TestFailed
77 except ValueError:
78 pass
81 # unpacking sequence too short
82 if verbose:
83 print 'unpack sequence too short'
84 try:
85 a, b, c, d = Seq()
86 raise TestFailed
87 except ValueError:
88 pass
91 # unpacking sequence too long
92 if verbose:
93 print 'unpack sequence too long'
94 try:
95 a, b = Seq()
96 raise TestFailed
97 except ValueError:
98 pass
101 # unpacking a sequence where the test for too long raises a different
102 # kind of error
103 class BozoError(Exception):
104 pass
106 class BadSeq:
107 def __getitem__(self, i):
108 if i >= 0 and i < 3:
109 return i
110 elif i == 3:
111 raise BozoError
112 else:
113 raise IndexError
116 # trigger code while not expecting an IndexError
117 if verbose:
118 print 'unpack sequence too long, wrong error'
119 try:
120 a, b, c, d, e = BadSeq()
121 raise TestFailed
122 except BozoError:
123 pass
125 # trigger code while expecting an IndexError
126 if verbose:
127 print 'unpack sequence too short, wrong error'
128 try:
129 a, b, c = BadSeq()
130 raise TestFailed
131 except BozoError:
132 pass