[Python-checkins] CVS: python/dist/src/Objects complexobject.c,2.48,2.48.2.1 object.c,2.151,2.151.2.1 stringobject.c,2.134,2.134.2.1

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 27 Sep 2001 13:21:13 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv14426/Objects

Modified Files:
      Tag: r22a4-branch
	complexobject.c object.c stringobject.c 
Log Message:
Policy change for rich comparison: if the second operand's type is a
subtype of the first operand's type, try its comparison *before*
trying the first operand.

This removes the need for hacks in string and complex rich comparisons
that test whether the operands have the proper comparison slot;
instead, we can use a straightforward typecheck.

This solves some issues related to make __dynamic__ the default: the
test failed if the comparison slot was slot_tp_richcompare.

It also solves the situation where a derived class overrides a base
class's rich comparison but wants to call the base class's rich
comparison implementation:

  class C(complex):
    def __eq__(self, other):
      print "Comparing", self, "to", other
      return complex.__eq__(self, other)

Here the base class implementation would refuse the explicit call
because of the test for a specific slot function.



Index: complexobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/complexobject.c,v
retrieving revision 2.48
retrieving revision 2.48.2.1
diff -C2 -d -r2.48 -r2.48.2.1
*** complexobject.c	2001/09/24 17:52:04	2.48
--- complexobject.c	2001/09/27 20:21:11	2.48.2.1
***************
*** 561,568 ****
  		return Py_NotImplemented;
  	}
! 	/* May sure both arguments use complex comparison.
! 	   This implies PyComplex_Check(a) && PyComplex_Check(b). */
! 	if (v->ob_type->tp_richcompare != complex_richcompare ||
! 	    w->ob_type->tp_richcompare != complex_richcompare) {
  		Py_DECREF(v);
  		Py_DECREF(w);
--- 561,566 ----
  		return Py_NotImplemented;
  	}
! 	/* Make sure both arguments are complex. */
! 	if (!(PyComplex_Check(v) && PyComplex_Check(w))) {
  		Py_DECREF(v);
  		Py_DECREF(w);

Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.151
retrieving revision 2.151.2.1
diff -C2 -d -r2.151 -r2.151.2.1
*** object.c	2001/09/20 13:38:22	2.151
--- object.c	2001/09/27 20:21:11	2.151.2.1
***************
*** 366,369 ****
--- 366,377 ----
  	PyObject *res;
  
+ 	if (v->ob_type != w->ob_type &&
+ 	    PyType_IsSubtype(w->ob_type, v->ob_type) &&
+ 	    (f = RICHCOMPARE(w->ob_type)) != NULL) {
+ 		res = (*f)(w, v, swapped_op[op]);
+ 		if (res != Py_NotImplemented)
+ 			return res;
+ 		Py_DECREF(res);
+ 	}
  	if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
  		res = (*f)(v, w, op);

Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.134
retrieving revision 2.134.2.1
diff -C2 -d -r2.134 -r2.134.2.1
*** stringobject.c	2001/09/24 16:51:54	2.134
--- stringobject.c	2001/09/27 20:21:11	2.134.2.1
***************
*** 825,832 ****
  	PyObject *result;
  
! 	/* May sure both arguments use string comparison.
! 	   This implies PyString_Check(a) && PyString_Check(b). */
! 	if (a->ob_type->tp_richcompare != (richcmpfunc)string_richcompare ||
! 	    b->ob_type->tp_richcompare != (richcmpfunc)string_richcompare) {
  		result = Py_NotImplemented;
  		goto out;
--- 825,830 ----
  	PyObject *result;
  
! 	/* Make sure both arguments are strings. */
! 	if (!(PyString_Check(a) && PyString_Check(b))) {
  		result = Py_NotImplemented;
  		goto out;