diff options
author | Guido van Rossum <guido@python.org> | 2001-09-18 20:38:53 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-09-18 20:38:53 (GMT) |
commit | ab3b0343b89b4683148dadaf89728ee1198ebee5 (patch) | |
tree | ce2bc61bcef280a23bbc326fd7ec755a72bf1baa /Lib | |
parent | eb9490526508dc643c2329771f95a925e5412049 (diff) | |
download | cpython-ab3b0343b89b4683148dadaf89728ee1198ebee5.zip cpython-ab3b0343b89b4683148dadaf89728ee1198ebee5.tar.gz cpython-ab3b0343b89b4683148dadaf89728ee1198ebee5.tar.bz2 |
Hopefully fix 3-way comparisons. This unfortunately adds yet another
hack, and it's even more disgusting than a PyInstance_Check() call.
If the tp_compare slot is the slot used for overrides in Python,
it's always called.
Add some tests that show what should work too.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_descr.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index fc00318..bd04605 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1831,6 +1831,33 @@ def str_subclass_as_dict_key(): verify(cistr('ONe') in d) verify(d.get(cistr('thrEE')) == 3) +def classic_comparisons(): + if verbose: print "Testing classic comparisons..." + for base in (int, object): + if verbose: print " (base = %s)" % base + class C(base): + def __init__(self, value): + self.value = int(value) + def __cmp__(self, other): + if isinstance(other, C): + return cmp(self.value, other.value) + if isinstance(other, int) or isinstance(other, long): + return cmp(self.value, other) + return NotImplemented + c1 = C(1) + c2 = C(2) + c3 = C(3) + verify(c1 == 1) + c = {1: c1, 2: c2, 3: c3} + for x in 1, 2, 3: + for y in 1, 2, 3: + verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) + for op in "<", "<=", "==", "!=", ">", ">=": + verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), + "x=%d, y=%d" % (x, y)) + verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y)) + verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) + def all(): lists() @@ -1869,6 +1896,7 @@ def all(): keywords() restricted() str_subclass_as_dict_key() + classic_comparisons() all() |