[Python-bugs-list] [Bug #125989] cmp() broken on instances

noreply@sourceforge.net noreply@sourceforge.net
Sat, 16 Dec 2000 09:22:32 -0800


Bug #125989, was updated on 2000-Dec-16 09:22
Here is a current snapshot of the bug.

Project: Python
Category: Python Interpreter Core
Status: Open
Resolution: None
Bug Group: None
Priority: 5
Submitted by: nascheme
Assigned to : nobody
Summary: cmp() broken on instances

Details: I found this while working on the implementation for PEP 208. Python
1.5.2 and Python 2.0 both give the same behavior (tested on Linux and
Solaris).

class CoerceNumber:
    def __init__(self, arg):
        self.arg = arg

    def __coerce__(self, other):
        if isinstance(other, CoerceNumber):
            return self.arg, other.arg
        else:
            return self.arg, other

class MethodNumber:
    def __init__(self,arg):
	self.arg = arg

    def __cmp__(self, other):
        return cmp(self.arg, other)

# the order of instantiation matters!
m = MethodNumber(1)
c = CoerceNumber(2)
print "cmp(<MethodNumber 1>,<CoerceNumber 2>) =", cmp(m, c)
print "cmp(<CoerceNumber 2>,<MethodNumber 1>) =", cmp(c, m)


Randomly assigned to Tim.  I don't have time to figure out what's
happening right now.  Tim, feel free to ignore.  It should be fixed by PEP
208 anyhow.

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=125989&group_id=5470