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

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 18 Jan 2001 15:46:34 -0800


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

Modified Files:
	classobject.c 
Log Message:
Rich comparisons fallout: instance_hash() should check for both
__cmp__ and __eq__ absent before deciding to do a quickie
based on the object address.  (Tim Peters discovered this.)


Index: classobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/classobject.c,v
retrieving revision 2.116
retrieving revision 2.117
diff -C2 -r2.116 -r2.117
*** classobject.c	2001/01/17 23:43:43	2.116
--- classobject.c	2001/01/18 23:46:31	2.117
***************
*** 763,767 ****
  	PyObject *res;
  	long outcome;
! 	static PyObject *hashstr, *cmpstr;
  
  	if (hashstr == NULL)
--- 763,767 ----
  	PyObject *res;
  	long outcome;
! 	static PyObject *hashstr, *eqstr, *cmpstr;
  
  	if (hashstr == NULL)
***************
*** 769,781 ****
  	func = instance_getattr(inst, hashstr);
  	if (func == NULL) {
! 		/* If there is no __cmp__ method, we hash on the address.
! 		   If a __cmp__ method exists, there must be a __hash__. */
  		PyErr_Clear();
! 		if (cmpstr == NULL)
! 			cmpstr = PyString_InternFromString("__cmp__");
! 		func = instance_getattr(inst, cmpstr);
  		if (func == NULL) {
  			PyErr_Clear();
! 			return _Py_HashPointer(inst);
  		}
  		PyErr_SetString(PyExc_TypeError, "unhashable instance");
--- 769,788 ----
  	func = instance_getattr(inst, hashstr);
  	if (func == NULL) {
! 		/* If there is no __eq__ and no __cmp__ method, we hash on the
! 		   address.  If an __eq__ or __cmp__ method exists, there must
! 		   be a __hash__. */
  		PyErr_Clear();
! 		if (eqstr == NULL)
! 			eqstr = PyString_InternFromString("__eq__");
! 		func = instance_getattr(inst, eqstr);
  		if (func == NULL) {
  			PyErr_Clear();
! 			if (cmpstr == NULL)
! 				cmpstr = PyString_InternFromString("__cmp__");
! 			func = instance_getattr(inst, cmpstr);
! 			if (func == NULL) {
! 				PyErr_Clear();
! 				return _Py_HashPointer(inst);
! 			}
  		}
  		PyErr_SetString(PyExc_TypeError, "unhashable instance");