Commit 65e8bd7f authored by Guido van Rossum's avatar Guido van Rossum

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.)
parent 0c6614c7
...@@ -762,14 +762,20 @@ instance_hash(PyInstanceObject *inst) ...@@ -762,14 +762,20 @@ instance_hash(PyInstanceObject *inst)
PyObject *func; PyObject *func;
PyObject *res; PyObject *res;
long outcome; long outcome;
static PyObject *hashstr, *cmpstr; static PyObject *hashstr, *eqstr, *cmpstr;
if (hashstr == NULL) if (hashstr == NULL)
hashstr = PyString_InternFromString("__hash__"); hashstr = PyString_InternFromString("__hash__");
func = instance_getattr(inst, hashstr); func = instance_getattr(inst, hashstr);
if (func == NULL) { if (func == NULL) {
/* If there is no __cmp__ method, we hash on the address. /* If there is no __eq__ and no __cmp__ method, we hash on the
If a __cmp__ method exists, there must be a __hash__. */ 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(); PyErr_Clear();
if (cmpstr == NULL) if (cmpstr == NULL)
cmpstr = PyString_InternFromString("__cmp__"); cmpstr = PyString_InternFromString("__cmp__");
...@@ -778,6 +784,7 @@ instance_hash(PyInstanceObject *inst) ...@@ -778,6 +784,7 @@ instance_hash(PyInstanceObject *inst)
PyErr_Clear(); PyErr_Clear();
return _Py_HashPointer(inst); return _Py_HashPointer(inst);
} }
}
PyErr_SetString(PyExc_TypeError, "unhashable instance"); PyErr_SetString(PyExc_TypeError, "unhashable instance");
return -1; return -1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment