diff Python/bltinmodule.c @ 39644:13d5e5f0e993

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...
author Guido van Rossum <guido@python.org>
date Thu, 24 Aug 2006 00:41:19 +0000
parents e86c0b29b4a2
children 8b9ec344bed2
line wrap: on
line diff
--- a/Python/bltinmodule.c	Wed Aug 23 23:20:29 2006 +0000
+++ b/Python/bltinmodule.c	Thu Aug 24 00:41:19 2006 +0000
@@ -1324,9 +1324,10 @@
 	PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
 		/* holds sub-expression evaluations */
 
-	/* if (lo >= hi), return length of 0. */
-	if (PyObject_Compare(lo, hi) >= 0)
-		return 0;
+	/* If (lo >= hi), return length of 0 (or error). */
+	n = PyObject_RichCompareBool(lo, hi, Py_LT);
+	if (n <= 0)
+		return n;
 
 	if ((one = PyLong_FromLong(1L)) == NULL)
 		goto Fail;
@@ -1378,7 +1379,7 @@
 	PyObject *v = NULL;
 	long bign;
 	int i, n;
-	int cmp_result;
+	int step_pos;
 
 	PyObject *zero = PyLong_FromLong(0);
 
@@ -1439,17 +1440,20 @@
 		goto Fail;
 	}
 
-	if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
+	step_pos = PyObject_RichCompareBool(istep, zero, Py_GT);
+	if (step_pos < 0)
 		goto Fail;
-	if (cmp_result == 0) {
-		PyErr_SetString(PyExc_ValueError,
-				"range() step argument must not be zero");
-		goto Fail;
-	}
-
-	if (cmp_result > 0)
+	if (step_pos)
 		bign = get_len_of_range_longs(ilow, ihigh, istep);
 	else {
+		int step_zero = PyObject_RichCompareBool(istep, zero, Py_EQ);
+		if (step_zero < 0)
+			goto Fail;
+		if (step_zero) {
+			PyErr_SetString(PyExc_ValueError,
+				"range() step argument must not be zero");
+			goto Fail;
+		}
 		PyObject *neg_istep = PyNumber_Negative(istep);
 		if (neg_istep == NULL)
 			goto Fail;