]> granicus.if.org Git - python/commitdiff
Rich comparisons fallout: instance_hash() should check for both
authorGuido van Rossum <guido@python.org>
Thu, 18 Jan 2001 23:46:31 +0000 (23:46 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 18 Jan 2001 23:46:31 +0000 (23:46 +0000)
__cmp__ and __eq__ absent before deciding to do a quickie
based on the object address.  (Tim Peters discovered this.)

Objects/classobject.c

index 5e987ff6b8bf6b29e5b4a433fb5662b1d4e9f165..40ff9839b9368a8d67422ea92ba2b3c18d2fc3b2 100644 (file)
@@ -762,21 +762,28 @@ instance_hash(PyInstanceObject *inst)
        PyObject *func;
        PyObject *res;
        long outcome;
-       static PyObject *hashstr, *cmpstr;
+       static PyObject *hashstr, *eqstr, *cmpstr;
 
        if (hashstr == NULL)
                hashstr = PyString_InternFromString("__hash__");
        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__. */
+               /* 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 (cmpstr == NULL)
-                       cmpstr = PyString_InternFromString("__cmp__");
-               func = instance_getattr(inst, cmpstr);
+               if (eqstr == NULL)
+                       eqstr = PyString_InternFromString("__eq__");
+               func = instance_getattr(inst, eqstr);
                if (func == NULL) {
                        PyErr_Clear();
-                       return _Py_HashPointer(inst);
+                       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");
                return -1;