diff options
author | Guido van Rossum <guido@python.org> | 2001-09-28 23:49:48 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-09-28 23:49:48 (GMT) |
commit | 4bb1e36eec19b30f2e582fceffa250e1598262e1 (patch) | |
tree | 0ba999260cfc7eaa6f9ce6a7e9f251d79edcf1e7 /Lib | |
parent | 9bea3abf0d7c658a95a92a295c82d5bf0b583f08 (diff) | |
download | cpython-4bb1e36eec19b30f2e582fceffa250e1598262e1.zip cpython-4bb1e36eec19b30f2e582fceffa250e1598262e1.tar.gz cpython-4bb1e36eec19b30f2e582fceffa250e1598262e1.tar.bz2 |
It's a fact: for binary operators, *under certain circumstances*,
__rop__ now takes precendence over __op__. Those circumstances are:
- Both arguments are new-style classes
- Both arguments are new-style numbers
- Their implementation slots for tp_op differ
- Their types differ
- The right argument's type is a subtype of the left argument's type
Also did this for the ternary operator (pow) -- only the binary case
is dealt with properly though, since __rpow__ is not supported anyway.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_descr.py | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 2c5e7a4..22ae09a 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1,8 +1,12 @@ -# Test descriptor-related enhancements +# Test enhancements related to descriptors and new-style classes from test_support import verify, verbose, TestFailed, TESTFN from copy import deepcopy +def vereq(a, b): + if a != b: + raise TestFailed, "%r != %r" % (a, b) + def testunop(a, res, expr="len(a)", meth="__len__"): if verbose: print "checking", expr dict = {'a': a} @@ -2133,6 +2137,36 @@ def copies(): a.bar.append(4) verify(d.bar == [1,2,3]) +def binopoverride(): + if verbose: print "Testing overrides of binary operations..." + class I(int): + def __repr__(self): + return "I(%r)" % int(self) + def __add__(self, other): + return I(int(self) + int(other)) + __radd__ = __add__ + def __pow__(self, other, mod=None): + if mod is None: + return I(pow(int(self), int(other))) + else: + return I(pow(int(self), int(other), int(mod))) + def __rpow__(self, other, mod=None): + if mod is None: + return I(pow(int(other), int(self), mod)) + else: + return I(pow(int(other), int(self), int(mod))) + + vereq(`I(1) + I(2)`, "I(3)") + vereq(`I(1) + 2`, "I(3)") + vereq(`1 + I(2)`, "I(3)") + vereq(`I(2) ** I(3)`, "I(8)") + vereq(`2 ** I(3)`, "I(8)") + vereq(`I(2) ** 3`, "I(8)") + vereq(`pow(I(2), I(3), I(5))`, "I(3)") + class S(str): + def __eq__(self, other): + return self.lower() == other.lower() + def test_main(): lists() @@ -2178,6 +2212,7 @@ def test_main(): setclass() pickles() copies() + binopoverride() if verbose: print "All OK" if __name__ == "__main__": |