summaryrefslogtreecommitdiffstats
path: root/Objects/classobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-01-18 23:46:31 (GMT)
committerGuido van Rossum <guido@python.org>2001-01-18 23:46:31 (GMT)
commit65e8bd7fd5af5de8e6354832d5c00e7cc9dff7ab (patch)
treec53ffaf4c3ab69635351466bb1847a6e024e63da /Objects/classobject.c
parent0c6614c78904b6ad0a92033a3224b95d182f1b17 (diff)
downloadcpython-65e8bd7fd5af5de8e6354832d5c00e7cc9dff7ab.zip
cpython-65e8bd7fd5af5de8e6354832d5c00e7cc9dff7ab.tar.gz
cpython-65e8bd7fd5af5de8e6354832d5c00e7cc9dff7ab.tar.bz2
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.)
Diffstat (limited to 'Objects/classobject.c')
-rw-r--r--Objects/classobject.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 5e987ff..40ff983 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -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;