| 4901 | self.assertIs(list.append.__objclass__, list) |
| 4902 | |
| 4903 | def test_not_implemented(self): |
| 4904 | # Testing NotImplemented... |
| 4905 | # all binary methods should be able to return a NotImplemented |
| 4906 | |
| 4907 | def specialmethod(self, other): |
| 4908 | return NotImplemented |
| 4909 | |
| 4910 | def check(expr, x, y): |
| 4911 | with ( |
| 4912 | self.subTest(expr=expr, x=x, y=y), |
| 4913 | self.assertRaises(TypeError), |
| 4914 | ): |
| 4915 | exec(expr, {'x': x, 'y': y}) |
| 4916 | |
| 4917 | N1 = sys.maxsize + 1 # might trigger OverflowErrors instead of |
| 4918 | # TypeErrors |
| 4919 | N2 = sys.maxsize # if sizeof(int) < sizeof(long), might trigger |
| 4920 | # ValueErrors instead of TypeErrors |
| 4921 | for name, expr, iexpr in [ |
| 4922 | ('__add__', 'x + y', 'x += y'), |
| 4923 | ('__sub__', 'x - y', 'x -= y'), |
| 4924 | ('__mul__', 'x * y', 'x *= y'), |
| 4925 | ('__matmul__', 'x @ y', 'x @= y'), |
| 4926 | ('__truediv__', 'x / y', 'x /= y'), |
| 4927 | ('__floordiv__', 'x // y', 'x //= y'), |
| 4928 | ('__mod__', 'x % y', 'x %= y'), |
| 4929 | ('__divmod__', 'divmod(x, y)', None), |
| 4930 | ('__pow__', 'x ** y', 'x **= y'), |
| 4931 | ('__lshift__', 'x << y', 'x <<= y'), |
| 4932 | ('__rshift__', 'x >> y', 'x >>= y'), |
| 4933 | ('__and__', 'x & y', 'x &= y'), |
| 4934 | ('__or__', 'x | y', 'x |= y'), |
| 4935 | ('__xor__', 'x ^ y', 'x ^= y')]: |
| 4936 | # Defines 'left' magic method: |
| 4937 | A = type('A', (), {name: specialmethod}) |
| 4938 | a = A() |
| 4939 | check(expr, a, a) |
| 4940 | check(expr, a, N1) |
| 4941 | check(expr, a, N2) |
| 4942 | # Defines 'right' magic method: |
| 4943 | rname = '__r' + name[2:] |
| 4944 | B = type('B', (), {rname: specialmethod}) |
| 4945 | b = B() |
| 4946 | check(expr, b, b) |
| 4947 | check(expr, a, b) |
| 4948 | check(expr, b, a) |
| 4949 | check(expr, b, N1) |
| 4950 | check(expr, b, N2) |
| 4951 | check(expr, N1, b) |
| 4952 | check(expr, N2, b) |
| 4953 | if iexpr: |
| 4954 | check(iexpr, a, a) |
| 4955 | check(iexpr, a, N1) |
| 4956 | check(iexpr, a, N2) |
| 4957 | iname = '__i' + name[2:] |
| 4958 | C = type('C', (), {iname: specialmethod}) |
| 4959 | c = C() |
| 4960 | check(iexpr, c, a) |