Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 1 | # Tests for extended unpacking, starred expressions. |
| 2 | |
| 3 | doctests = """ |
| 4 | |
| 5 | Unpack tuple |
| 6 | |
| 7 | >>> t = (1, 2, 3) |
| 8 | >>> a, *b, c = t |
| 9 | >>> a == 1 and b == [2] and c == 3 |
| 10 | True |
| 11 | |
| 12 | Unpack list |
| 13 | |
| 14 | >>> l = [4, 5, 6] |
| 15 | >>> a, *b = l |
| 16 | >>> a == 4 and b == [5, 6] |
| 17 | True |
| 18 | |
| 19 | Unpack implied tuple |
| 20 | |
| 21 | >>> *a, = 7, 8, 9 |
| 22 | >>> a == [7, 8, 9] |
| 23 | True |
| 24 | |
| 25 | Unpack string... fun! |
| 26 | |
| 27 | >>> a, *b = 'one' |
| 28 | >>> a == 'o' and b == ['n', 'e'] |
| 29 | True |
| 30 | |
| 31 | Unpack long sequence |
| 32 | |
| 33 | >>> a, b, c, *d, e, f, g = range(10) |
| 34 | >>> (a, b, c, d, e, f, g) == (0, 1, 2, [3, 4, 5, 6], 7, 8, 9) |
| 35 | True |
| 36 | |
| 37 | Unpack short sequence |
| 38 | |
| 39 | >>> a, *b, c = (1, 2) |
| 40 | >>> a == 1 and c == 2 and b == [] |
| 41 | True |
| 42 | |
| 43 | Unpack generic sequence |
| 44 | |
| 45 | >>> class Seq: |
| 46 | ... def __getitem__(self, i): |
| 47 | ... if i >= 0 and i < 3: return i |
| 48 | ... raise IndexError |
| 49 | ... |
| 50 | >>> a, *b = Seq() |
| 51 | >>> a == 0 and b == [1, 2] |
| 52 | True |
| 53 | |
| 54 | Unpack in for statement |
| 55 | |
| 56 | >>> for a, *b, c in [(1,2,3), (4,5,6,7)]: |
| 57 | ... print(a, b, c) |
| 58 | ... |
| 59 | 1 [2] 3 |
| 60 | 4 [5, 6] 7 |
| 61 | |
| 62 | Unpack in list |
| 63 | |
| 64 | >>> [a, *b, c] = range(5) |
| 65 | >>> a == 0 and b == [1, 2, 3] and c == 4 |
| 66 | True |
| 67 | |
| 68 | Multiple targets |
| 69 | |
| 70 | >>> a, *b, c = *d, e = range(5) |
| 71 | >>> a == 0 and b == [1, 2, 3] and c == 4 and d == [0, 1, 2, 3] and e == 4 |
| 72 | True |
| 73 | |
| 74 | Now for some failures |
| 75 | |
| 76 | Unpacking non-sequence |
| 77 | |
| 78 | >>> a, *b = 7 |
| 79 | Traceback (most recent call last): |
| 80 | ... |
| 81 | TypeError: 'int' object is not iterable |
| 82 | |
| 83 | Unpacking sequence too short |
| 84 | |
| 85 | >>> a, *b, c, d, e = Seq() |
| 86 | Traceback (most recent call last): |
| 87 | ... |
| 88 | ValueError: need more than 3 values to unpack |
| 89 | |
| 90 | Unpacking a sequence where the test for too long raises a different kind of |
| 91 | error |
| 92 | |
| 93 | >>> class BozoError(Exception): |
| 94 | ... pass |
| 95 | ... |
| 96 | >>> class BadSeq: |
| 97 | ... def __getitem__(self, i): |
| 98 | ... if i >= 0 and i < 3: |
| 99 | ... return i |
| 100 | ... elif i == 3: |
| 101 | ... raise BozoError |
| 102 | ... else: |
| 103 | ... raise IndexError |
| 104 | ... |
| 105 | |
| 106 | Trigger code while not expecting an IndexError (unpack sequence too long, wrong |
| 107 | error) |
| 108 | |
| 109 | >>> a, *b, c, d, e = BadSeq() |
| 110 | Traceback (most recent call last): |
| 111 | ... |
| 112 | test.test_unpack_ex.BozoError |
| 113 | |
| 114 | Now some general starred expressions (all fail). |
| 115 | |
| 116 | >>> a, *b, c, *d, e = range(10) # doctest:+ELLIPSIS |
| 117 | Traceback (most recent call last): |
| 118 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 119 | SyntaxError: two starred expressions in assignment |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 120 | |
| 121 | >>> [*b, *c] = range(10) # doctest:+ELLIPSIS |
| 122 | Traceback (most recent call last): |
| 123 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 124 | SyntaxError: two starred expressions in assignment |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 125 | |
| 126 | >>> *a = range(10) # doctest:+ELLIPSIS |
| 127 | Traceback (most recent call last): |
| 128 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 129 | SyntaxError: starred assignment target must be in a list or tuple |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 130 | |
| 131 | >>> *a # doctest:+ELLIPSIS |
| 132 | Traceback (most recent call last): |
| 133 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 134 | SyntaxError: can use starred expression only as assignment target |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 135 | |
| 136 | >>> *1 # doctest:+ELLIPSIS |
| 137 | Traceback (most recent call last): |
| 138 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 139 | SyntaxError: can use starred expression only as assignment target |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 140 | |
| 141 | >>> x = *a # doctest:+ELLIPSIS |
| 142 | Traceback (most recent call last): |
| 143 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 144 | SyntaxError: can use starred expression only as assignment target |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 145 | |
Thomas Wouters | deef674 | 2008-03-14 17:16:59 +0000 | [diff] [blame] | 146 | Some size constraints (all fail.) |
| 147 | |
| 148 | >>> s = ", ".join("a%d" % i for i in range(1<<8)) + ", *rest = range(1<<8 + 1)" |
| 149 | >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS |
| 150 | Traceback (most recent call last): |
| 151 | ... |
| 152 | SyntaxError: too many expressions in star-unpacking assignment |
| 153 | |
| 154 | >>> s = ", ".join("a%d" % i for i in range(1<<8 + 1)) + ", *rest = range(1<<8 + 2)" |
| 155 | >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS |
| 156 | Traceback (most recent call last): |
| 157 | ... |
| 158 | SyntaxError: too many expressions in star-unpacking assignment |
| 159 | |
| 160 | (there is an additional limit, on the number of expressions after the |
| 161 | '*rest', but it's 1<<24 and testing it takes too much memory.) |
| 162 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 163 | """ |
| 164 | |
| 165 | __test__ = {'doctests' : doctests} |
| 166 | |
| 167 | def test_main(verbose=False): |
| 168 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 169 | from test import support |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 170 | from test import test_unpack_ex |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 171 | support.run_doctest(test_unpack_ex, verbose) |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 172 | |
| 173 | if __name__ == "__main__": |
| 174 | test_main(verbose=True) |