/* Do a 3-way comparison, by hook or by crook. Return: -2 for an exception (but see below); -1 if v < w; 0 if v == w; 1 if v > w; BUT: if the object implements a tp_compare function, it returns whatever this function returns (whether with an exception or not). */ static int do_cmp(PyObject *v, PyObject *w) { int c; cmpfunc f; if (v->ob_type == w->ob_type && (f = v->ob_type->tp_compare) != NULL) { c = (*f)(v, w); if (PyInstance_Check(v)) { /* Instance tp_compare has a different signature. But if it returns undefined we fall through. */ if (c != 2) return c; /* Else fall through to try_rich_to_3way_compare() */ } else return adjust_tp_compare(c); } /* We only get here if one of the following is true: a) v and w have different types b) v and w have the same type, which doesn't have tp_compare c) v and w are instances, and either __cmp__ is not defined or __cmp__ returns NotImplemented */ c = try_rich_to_3way_compare(v, w); if (c < 2) return c; c = try_3way_compare(v, w); if (c < 2) return c; return default_3way_compare(v, w); }
/* We want a rich comparison but don't have one. Try a 3-way cmp instead. Return NULL if error Py_True if v op w Py_False if not (v op w) */ static PyObject * try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) { int c; c = try_3way_compare(v, w); if (c >= 2) c = default_3way_compare(v, w); if (c <= -2) return NULL; return convert_3way_to_object(op, c); }
/* Do a 3-way comparison, by hook or by crook. Return: -2 for an exception; -1 if v < w; 0 if v == w; 1 if v > w; If the object implements a tp_compare function, it returns whatever this function returns (whether with an exception or not). */ static int do_cmp(PyObject *v, PyObject *w) { int c; cmpfunc f; if (v->ob_type == w->ob_type && (f = v->ob_type->tp_compare) != NULL) { c = (*f)(v, w); if (c != 2 || !PyInstance_Check(v)) return c; } c = try_rich_to_3way_compare(v, w); if (c < 2) return c; c = try_3way_compare(v, w); if (c < 2) return c; return default_3way_compare(v, w); }