[Python-checkins] CVS: python/dist/src/Objects object.c,2.117,2.118

Guido van Rossum gvanrossum@users.sourceforge.net
Wed, 24 Jan 2001 14:14:45 -0800


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

Modified Files:
	object.c 
Log Message:
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
tp_richcompare field!  (Hopefully this will make Python 2.1 binary
compatible with certain Zope extensions. :-)


Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.117
retrieving revision 2.118
diff -C2 -r2.117 -r2.118
*** object.c	2001/01/24 04:17:56	2.117
--- object.c	2001/01/24 22:14:43	2.118
***************
*** 372,375 ****
--- 372,379 ----
  
  
+ /* Macro to get the tp_richcompare field of a type if defined */
+ #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
+                          ? (t)->tp_richcompare : NULL)
+ 
  /* Map rich comparison operators to their swapped version, e.g. LT --> GT */
  static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
***************
*** 388,392 ****
  	PyObject *res;
  
! 	if ((f = v->ob_type->tp_richcompare) != NULL) {
  		res = (*f)(v, w, op);
  		if (res != Py_NotImplemented)
--- 392,396 ----
  	PyObject *res;
  
! 	if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
  		res = (*f)(v, w, op);
  		if (res != Py_NotImplemented)
***************
*** 394,398 ****
  		Py_DECREF(res);
  	}
! 	if ((f = w->ob_type->tp_richcompare) != NULL) {
  		return (*f)(w, v, swapped_op[op]);
  	}
--- 398,402 ----
  		Py_DECREF(res);
  	}
! 	if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
  		return (*f)(w, v, swapped_op[op]);
  	}
***************
*** 415,420 ****
  	int ok;
  
! 	if (v->ob_type->tp_richcompare == NULL &&
! 	    w->ob_type->tp_richcompare == NULL)
  		return 2; /* Shortcut, avoid INCREF+DECREF */
  	res = try_rich_compare(v, w, op);
--- 419,423 ----
  	int ok;
  
! 	if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
  		return 2; /* Shortcut, avoid INCREF+DECREF */
  	res = try_rich_compare(v, w, op);
***************
*** 448,453 ****
  	int i;
  
! 	if (v->ob_type->tp_richcompare == NULL &&
! 	    w->ob_type->tp_richcompare == NULL)
  		return 2; /* Shortcut */
  
--- 451,455 ----
  	int i;
  
! 	if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
  		return 2; /* Shortcut */
  
***************
*** 955,959 ****
  	if (tp->tp_hash != NULL)
  		return (*tp->tp_hash)(v);
! 	if (tp->tp_compare == NULL && tp->tp_richcompare == NULL) {
  		return _Py_HashPointer(v); /* Use address as hash value */
  	}
--- 957,961 ----
  	if (tp->tp_hash != NULL)
  		return (*tp->tp_hash)(v);
! 	if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
  		return _Py_HashPointer(v); /* Use address as hash value */
  	}