Restructure comparison dramatically. There is no longer a default

*ordering* between objects; there is only a default equality test
(defined by an object being equal to itself only).  Read the comment
in object.c.  The current implementation never uses a three-way
comparison to compute a rich comparison, but it does use a rich
comparison to compute a three-way comparison.  I'm not quite done
ripping out all the calls to PyObject_Compare/Cmp, or replacing
tp_compare implementations with tp_richcompare implementations;
but much of that has happened (to make most unit tests pass).

The following tests still fail, because I need help deciding
or understanding:

test_codeop -- depends on comparing code objects
test_datetime -- need Tim Peters' opinion
test_marshal -- depends on comparing code objects
test_mutants -- need help understanding it

The problem with test_codeop and test_marshal is this: these tests
compare two different code objects and expect them to be equal.
Is that still a feature we'd like to support?  I've temporarily
removed the comparison and hash code from code objects, so they
use the default (equality by pointer only) comparison.

For the other two tests, run them to see for yourself.
(There may be more failing test with "-u all".)

A general problem with getting lots of these tests to pass is
the reality that for object types that have a natural total ordering,
implementing __cmp__ is much more convenient than implementing
__eq__, __ne__, __lt__, and so on.  Should we go back to allowing
__cmp__ to provide a total ordering?  Should we provide some other
way to implement rich comparison with a single method override?
Alex proposed a __key__() method; I've considered a __richcmp__()
method.  Or perhaps __cmp__() just shouldn't be killed off...
This commit is contained in:
Guido van Rossum 2006-08-24 00:41:19 +00:00
parent 9a6e62b947
commit 47b9ff6ba1
57 changed files with 972 additions and 902 deletions

View file

@ -361,16 +361,6 @@ static PyGetSetDef type_getsets[] = {
{0}
};
static int
type_compare(PyObject *v, PyObject *w)
{
/* This is called with type objects only. So we
can just compare the addresses. */
Py_uintptr_t vv = (Py_uintptr_t)v;
Py_uintptr_t ww = (Py_uintptr_t)w;
return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
}
static PyObject *
type_repr(PyTypeObject *type)
{
@ -2192,12 +2182,12 @@ PyTypeObject PyType_Type = {
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
type_compare, /* tp_compare */
0, /* tp_compare */
(reprfunc)type_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)_Py_HashPointer, /* tp_hash */
0, /* tp_hash */
(ternaryfunc)type_call, /* tp_call */
0, /* tp_str */
(getattrofunc)type_getattro, /* tp_getattro */
@ -2301,6 +2291,30 @@ object_str(PyObject *self)
return f(self);
}
static PyObject *
object_richcompare(PyObject *self, PyObject *other, int op)
{
PyObject *res;
switch (op) {
case Py_EQ:
res = (self == other) ? Py_True : Py_False;
break;
case Py_NE:
res = (self != other) ? Py_True : Py_False;
break;
default:
res = Py_NotImplemented;
break;
}
Py_INCREF(res);
return res;
}
static PyObject *
object_get_class(PyObject *self, void *closure)
{
@ -2703,7 +2717,7 @@ PyTypeObject PyBaseObject_Type = {
PyDoc_STR("The most base type"), /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
object_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */