[Python-checkins] CVS: python/dist/src/Objects object.c,2.149,2.150 typeobject.c,2.64,2.65

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 18 Sep 2001 13:38:55 -0700


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

Modified Files:
	object.c typeobject.c 
Log Message:
Hopefully fix 3-way comparisons.  This unfortunately adds yet another
hack, and it's even more disgusting than a PyInstance_Check() call.
If the tp_compare slot is the slot used for overrides in Python,
it's always called.

Add some tests that show what should work too.


Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.149
retrieving revision 2.150
diff -C2 -d -r2.149 -r2.150
*** object.c	2001/09/17 02:38:46	2.149
--- object.c	2001/09/18 20:38:53	2.150
***************
*** 456,463 ****
  	   which has the same return conventions as this function. */
  
  	if (PyInstance_Check(v))
! 		return (*v->ob_type->tp_compare)(v, w);
  	if (PyInstance_Check(w))
  		return (*w->ob_type->tp_compare)(v, w);
  
  	/* Try coercion; if it fails, give up */
--- 456,477 ----
  	   which has the same return conventions as this function. */
  
+ 	f = v->ob_type->tp_compare;
  	if (PyInstance_Check(v))
! 		return (*f)(v, w);
  	if (PyInstance_Check(w))
  		return (*w->ob_type->tp_compare)(v, w);
+ 
+ 	/* If both have the same (non-NULL) tp_compare, use it. */
+ 	if (f != NULL && f == w->ob_type->tp_compare) {
+ 		c = (*f)(v, w);
+ 		if (c < 0 && PyErr_Occurred())
+ 			return -1;
+ 		return c < 0 ? -1 : c > 0 ? 1 : 0;
+ 	}
+ 
+ 	/* If either tp_compare is _PyObject_SlotCompare, that's safe. */
+ 	if (f == _PyObject_SlotCompare ||
+ 	    w->ob_type->tp_compare == _PyObject_SlotCompare)
+ 		return _PyObject_SlotCompare(v, w);
  
  	/* Try coercion; if it fails, give up */

Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.64
retrieving revision 2.65
diff -C2 -d -r2.64 -r2.65
*** typeobject.c	2001/09/18 20:03:57	2.64
--- typeobject.c	2001/09/18 20:38:53	2.65
***************
*** 2762,2776 ****
  }
  
! static int
! slot_tp_compare(PyObject *self, PyObject *other)
  {
  	int c;
  
! 	if (self->ob_type->tp_compare == slot_tp_compare) {
  		c = half_compare(self, other);
  		if (c <= 1)
  			return c;
  	}
! 	if (other->ob_type->tp_compare == slot_tp_compare) {
  		c = half_compare(other, self);
  		if (c < -1)
--- 2762,2777 ----
  }
  
! /* This slot is published for the benefit of try_3way_compare in object.c */
! int
! _PyObject_SlotCompare(PyObject *self, PyObject *other)
  {
  	int c;
  
! 	if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
  		c = half_compare(self, other);
  		if (c <= 1)
  			return c;
  	}
! 	if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
  		c = half_compare(other, self);
  		if (c < -1)
***************
*** 3191,3195 ****
  		type->tp_print = NULL;
  
! 	TPSLOT("__cmp__", tp_compare, slot_tp_compare);
  	TPSLOT("__repr__", tp_repr, slot_tp_repr);
  	TPSLOT("__hash__", tp_hash, slot_tp_hash);
--- 3192,3196 ----
  		type->tp_print = NULL;
  
! 	TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare);
  	TPSLOT("__repr__", tp_repr, slot_tp_repr);
  	TPSLOT("__hash__", tp_hash, slot_tp_hash);