| 1 |
import doctest |
| 2 |
import unittest |
| 3 |
|
| 4 |
|
| 5 |
doctests = """ |
| 6 |
|
| 7 |
Unpack tuple |
| 8 |
|
| 9 |
>>> t = (1, 2, 3) |
| 10 |
>>> a, b, c = t |
| 11 |
>>> a == 1 and b == 2 and c == 3 |
| 12 |
True |
| 13 |
|
| 14 |
Unpack list |
| 15 |
|
| 16 |
>>> l = [4, 5, 6] |
| 17 |
>>> a, b, c = l |
| 18 |
>>> a == 4 and b == 5 and c == 6 |
| 19 |
True |
| 20 |
|
| 21 |
Unpack dict |
| 22 |
|
| 23 |
>>> d = {4: 'four', 5: 'five', 6: 'six'} |
| 24 |
>>> a, b, c = d |
| 25 |
>>> a == 4 and b == 5 and c == 6 |
| 26 |
True |
| 27 |
|
| 28 |
Unpack implied tuple |
| 29 |
|
| 30 |
>>> a, b, c = 7, 8, 9 |
| 31 |
>>> a == 7 and b == 8 and c == 9 |
| 32 |
True |
| 33 |
|
| 34 |
Unpack string... fun! |
| 35 |
|
| 36 |
>>> a, b, c = 'one' |
| 37 |
>>> a == 'o' and b == 'n' and c == 'e' |
| 38 |
True |
| 39 |
|
| 40 |
Unpack generic sequence |
| 41 |
|
| 42 |
>>> class Seq: |
| 43 |
... def __getitem__(self, i): |
| 44 |
... if i >= 0 and i < 3: return i |
| 45 |
... raise IndexError |
| 46 |
... |
| 47 |
>>> a, b, c = Seq() |
| 48 |
>>> a == 0 and b == 1 and c == 2 |
| 49 |
True |
| 50 |
|
| 51 |
Single element unpacking, with extra syntax |
| 52 |
|
| 53 |
>>> st = (99,) |
| 54 |
>>> sl = [100] |
| 55 |
>>> a, = st |
| 56 |
>>> a |
| 57 |
99 |
| 58 |
>>> b, = sl |
| 59 |
>>> b |
| 60 |
100 |
| 61 |
|
| 62 |
Now for some failures |
| 63 |
|
| 64 |
Unpacking non-sequence |
| 65 |
|
| 66 |
>>> a, b, c = 7 |
| 67 |
Traceback (most recent call last): |
| 68 |
... |
| 69 |
TypeError: cannot unpack non-iterable int object |
| 70 |
|
| 71 |
Unpacking tuple of wrong size |
| 72 |
|
| 73 |
>>> a, b = t |
| 74 |
Traceback (most recent call last): |
| 75 |
... |
| 76 |
ValueError: too many values to unpack (expected 2, got 3) |
| 77 |
|
| 78 |
Unpacking tuple of wrong size |
| 79 |
|
| 80 |
>>> a, b = l |
| 81 |
Traceback (most recent call last): |
| 82 |
... |
| 83 |
ValueError: too many values to unpack (expected 2, got 3) |
| 84 |
|
| 85 |
Unpacking sequence too short |
| 86 |
|
| 87 |
>>> a, b, c, d = Seq() |
| 88 |
Traceback (most recent call last): |
| 89 |
... |
| 90 |
ValueError: not enough values to unpack (expected 4, got 3) |
| 91 |
|
| 92 |
Unpacking sequence too long |
| 93 |
|
| 94 |
>>> a, b = Seq() |
| 95 |
Traceback (most recent call last): |
| 96 |
... |
| 97 |
ValueError: too many values to unpack (expected 2) |
| 98 |
|
| 99 |
Unpacking a sequence where the test for too long raises a different kind of |
| 100 |
error |
| 101 |
|
| 102 |
>>> class BozoError(Exception): |
| 103 |
... pass |
| 104 |
... |
| 105 |
>>> class BadSeq: |
| 106 |
... def __getitem__(self, i): |
| 107 |
... if i >= 0 and i < 3: |
| 108 |
... return i |
| 109 |
... elif i == 3: |
| 110 |
... raise BozoError |
| 111 |
... else: |
| 112 |
... raise IndexError |
| 113 |
... |
| 114 |
|
| 115 |
Trigger code while not expecting an IndexError (unpack sequence too long, wrong |
| 116 |
error) |
| 117 |
|
| 118 |
>>> a, b, c, d, e = BadSeq() |
| 119 |
Traceback (most recent call last): |
| 120 |
... |
| 121 |
test.test_unpack.BozoError |
| 122 |
|
| 123 |
Trigger code while expecting an IndexError (unpack sequence too short, wrong |
| 124 |
error) |
| 125 |
|
| 126 |
>>> a, b, c = BadSeq() |
| 127 |
Traceback (most recent call last): |
| 128 |
... |
| 129 |
test.test_unpack.BozoError |
| 130 |
|
| 131 |
Allow unpacking empty iterables |
| 132 |
|
| 133 |
>>> () = [] |
| 134 |
>>> [] = () |
| 135 |
>>> [] = [] |
| 136 |
>>> () = () |
| 137 |
|
| 138 |
Unpacking non-iterables should raise TypeError |
| 139 |
|
| 140 |
>>> () = 42 |
| 141 |
Traceback (most recent call last): |
| 142 |
... |
| 143 |
TypeError: cannot unpack non-iterable int object |
| 144 |
|
| 145 |
Unpacking to an empty iterable should raise ValueError |
| 146 |
|
| 147 |
>>> () = [42] |
| 148 |
Traceback (most recent call last): |
| 149 |
... |
| 150 |
ValueError: too many values to unpack (expected 0, got 1) |
| 151 |
|
| 152 |
Unpacking a larger iterable should raise ValuleError, but it |
| 153 |
should not entirely consume the iterable |
| 154 |
|
| 155 |
>>> it = iter(range(100)) |
| 156 |
>>> x, y, z = it |
| 157 |
Traceback (most recent call last): |
| 158 |
... |
| 159 |
ValueError: too many values to unpack (expected 3) |
| 160 |
>>> next(it) |
| 161 |
4 |
| 162 |
|
| 163 |
Unpacking unbalanced dict |
| 164 |
|
| 165 |
>>> d = {4: 'four', 5: 'five', 6: 'six', 7: 'seven'} |
| 166 |
>>> a, b, c = d |
| 167 |
Traceback (most recent call last): |
| 168 |
... |
| 169 |
ValueError: too many values to unpack (expected 3, got 4) |
| 170 |
|
| 171 |
Ensure that custom `__len__()` is NOT called when showing the error message |
| 172 |
|
| 173 |
>>> class LengthTooLong: |
| 174 |
... def __len__(self): |
| 175 |
... return 5 |
| 176 |
... def __getitem__(self, i): |
| 177 |
... return i*2 |
| 178 |
... |
| 179 |
>>> x, y, z = LengthTooLong() |
| 180 |
Traceback (most recent call last): |
| 181 |
... |
| 182 |
ValueError: too many values to unpack (expected 3) |
| 183 |
|
| 184 |
For evil cases like these as well, no actual count to be shown |
| 185 |
|
| 186 |
>>> class BadLength: |
| 187 |
... def __len__(self): |
| 188 |
... return 1 |
| 189 |
... def __getitem__(self, i): |
| 190 |
... return i*2 |
| 191 |
... |
| 192 |
>>> x, y, z = BadLength() |
| 193 |
Traceback (most recent call last): |
| 194 |
... |
| 195 |
ValueError: too many values to unpack (expected 3) |
| 196 |
""" |
| 197 |
|
| 198 |
__test__ = {'doctests' : doctests} |
| 199 |
|
| 200 |
def load_tests(loader, tests, pattern): |
| 201 |
tests.addTest(doctest.DocTestSuite()) |
| 202 |
return tests |
| 203 |
|
| 204 |
|
| 205 |
class TestCornerCases(unittest.TestCase): |
| 206 |
def test_extended_oparg_not_ignored(self): |
| 207 |
# https://github.com/python/cpython/issues/91625 |
| 208 |
target = "(" + "y,"*400 + ")" |
| 209 |
code = f"""def unpack_400(x): |
| 210 |
{target} = x |
| 211 |
return y |
| 212 |
""" |
| 213 |
ns = {} |
| 214 |
exec(code, ns) |
| 215 |
unpack_400 = ns["unpack_400"] |
| 216 |
# Warm up the function for quickening (PEP 659) |
| 217 |
for _ in range(30): |
| 218 |
y = unpack_400(range(400)) |
| 219 |
self.assertEqual(y, 399) |
| 220 |
|
| 221 |
if __name__ == "__main__": |
| 222 |
unittest.main() |