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