|
| 1 | +from operator import imul, mul |
| 2 | + |
1 | 3 | from testutils import assert_raises |
2 | 4 |
|
3 | 5 | assert -3 // 2 == -2 |
|
52 | 54 | assert_raises(TypeError, lambda: 1.5 | True) |
53 | 55 | assert_raises(TypeError, lambda: 1.5 & True) |
54 | 56 | assert_raises(TypeError, lambda: 1.5 ^ True) |
| 57 | + |
| 58 | + |
| 59 | +def check_repeat_error(error, message, operation, *args): |
| 60 | + with assert_raises(error) as caught: |
| 61 | + operation(*args) |
| 62 | + assert str(caught.exception) == message, str(caught.exception) |
| 63 | + |
| 64 | + |
| 65 | +class GetItemOnly: |
| 66 | + def __getitem__(self, index): |
| 67 | + raise IndexError |
| 68 | + |
| 69 | + |
| 70 | +# An item slot does not imply support for repetition. |
| 71 | +for value in (range(3), GetItemOnly()): |
| 72 | + name = type(value).__name__ |
| 73 | + for operation, symbol in ((mul, "*"), (imul, "*=")): |
| 74 | + check_repeat_error( |
| 75 | + TypeError, |
| 76 | + f"unsupported operand type(s) for {symbol}: '{name}' and 'int'", |
| 77 | + operation, |
| 78 | + value, |
| 79 | + 2, |
| 80 | + ) |
| 81 | + check_repeat_error( |
| 82 | + TypeError, |
| 83 | + f"unsupported operand type(s) for *: 'int' and '{name}'", |
| 84 | + mul, |
| 85 | + 2, |
| 86 | + value, |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +# Native sequence slots use the same count conversion in both directions. |
| 91 | +for value in ([1], (1,), "a", b"a", bytearray(b"a")): |
| 92 | + for operation in (mul, lambda seq, count: count * seq, imul): |
| 93 | + for count, error, message in ( |
| 94 | + (1.5, TypeError, "can't multiply sequence by non-int of type 'float'"), |
| 95 | + (2**100, OverflowError, "cannot fit 'int' into an index-sized integer"), |
| 96 | + ): |
| 97 | + check_repeat_error(error, message, operation, value, count) |
| 98 | + |
| 99 | + |
| 100 | +class RepeatIndex: |
| 101 | + def __init__(self, value): |
| 102 | + self.value = value |
| 103 | + |
| 104 | + def __index__(self): |
| 105 | + return self.value |
| 106 | + |
| 107 | + |
| 108 | +assert RepeatIndex(2) * [1] == [1, 1] |
| 109 | +assert imul(2, [1]) == [1, 1] |
| 110 | +check_repeat_error( |
| 111 | + TypeError, |
| 112 | + "unsupported operand type(s) for *=: 'RepeatIndex' and 'list'", |
| 113 | + imul, |
| 114 | + RepeatIndex(2), |
| 115 | + [1], |
| 116 | +) |
| 117 | +check_repeat_error( |
| 118 | + OverflowError, |
| 119 | + "cannot fit 'RepeatIndex' into an index-sized integer", |
| 120 | + mul, |
| 121 | + "a", |
| 122 | + RepeatIndex(-(2**100)), |
| 123 | +) |
| 124 | + |
| 125 | + |
| 126 | +class DeclineRepeat(GetItemOnly): |
| 127 | + calls = [] |
| 128 | + |
| 129 | + def __mul__(self, other): |
| 130 | + self.calls.append("mul") |
| 131 | + return NotImplemented |
| 132 | + |
| 133 | + def __rmul__(self, other): |
| 134 | + self.calls.append("rmul") |
| 135 | + return NotImplemented |
| 136 | + |
| 137 | + def __imul__(self, other): |
| 138 | + self.calls.append("imul") |
| 139 | + return NotImplemented |
| 140 | + |
| 141 | + |
| 142 | +# Numeric fallback must not invoke the same Python method through a sequence slot. |
| 143 | +declining = DeclineRepeat() |
| 144 | +for operation, args, expected in ( |
| 145 | + (mul, (declining, 2), ["mul"]), |
| 146 | + (mul, (2, declining), ["rmul"]), |
| 147 | + (imul, (declining, 2), ["imul", "mul"]), |
| 148 | +): |
| 149 | + DeclineRepeat.calls.clear() |
| 150 | + assert_raises(TypeError, operation, *args) |
| 151 | + assert DeclineRepeat.calls == expected |
| 152 | + |
| 153 | + |
| 154 | +class RepeatList(list): |
| 155 | + pass |
| 156 | + |
| 157 | + |
| 158 | +class RepeatInt(int): |
| 159 | + pass |
| 160 | + |
| 161 | + |
| 162 | +assert "a" * RepeatInt(2) == RepeatInt(2) * "a" == "aa" |
| 163 | +for method, operation in ( |
| 164 | + ("__mul__", lambda value: value * 2), |
| 165 | + ("__rmul__", lambda value: 2 * value), |
| 166 | +): |
| 167 | + value = RepeatList([1]) |
| 168 | + assert operation(value) == [1, 1] |
| 169 | + setattr(RepeatList, method, lambda self, other: NotImplemented) |
| 170 | + assert_raises(TypeError, operation, value) |
| 171 | + delattr(RepeatList, method) |
| 172 | + assert operation(value) == [1, 1] |
0 commit comments