diff options
author | Guido van Rossum <guido@python.org> | 2007-12-19 22:51:13 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-12-19 22:51:13 (GMT) |
commit | 0b7b6fdff3b5bd7974c80a891598aa81ed634e61 (patch) | |
tree | cdfaa22a883c513a81ff477342eee52df8f6c06b /Objects | |
parent | 70fcfd03bcedb920efe84f829ed354ea0a0b3d3f (diff) | |
download | cpython-0b7b6fdff3b5bd7974c80a891598aa81ed634e61.zip cpython-0b7b6fdff3b5bd7974c80a891598aa81ed634e61.tar.gz cpython-0b7b6fdff3b5bd7974c80a891598aa81ed634e61.tar.bz2 |
Patch #1549 by Thomas Herve.
This changes the rules for when __hash__ is inherited slightly,
by allowing it to be inherited when one or more of __lt__, __le__,
__gt__, __ge__ are overridden, as long as __eq__ and __ne__ aren't.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/typeobject.c | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index e418137..74a00b1 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -3230,28 +3230,22 @@ inherit_special(PyTypeObject *type, PyTypeObject *base) type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; } -/* Map rich comparison operators to their __xx__ namesakes */ -static char *name_op[] = { - "__lt__", - "__le__", - "__eq__", - "__ne__", - "__gt__", - "__ge__", - "__cmp__", - /* These are only for overrides_hash(): */ - "__hash__", +static char *hash_name_op[] = { + "__eq__", + "__cmp__", + "__hash__", + NULL }; static int overrides_hash(PyTypeObject *type) { - int i; + char **p; PyObject *dict = type->tp_dict; assert(dict != NULL); - for (i = 0; i < 8; i++) { - if (PyDict_GetItemString(dict, name_op[i]) != NULL) + for (p = hash_name_op; *p; p++) { + if (PyDict_GetItemString(dict, *p) != NULL) return 1; } return 0; @@ -4846,7 +4840,7 @@ slot_tp_hash(PyObject *self) func = lookup_method(self, "__hash__", &hash_str); - if (func != NULL) { + if (func != NULL && func != Py_None) { PyObject *res = PyEval_CallObject(func, NULL); Py_DECREF(func); if (res == NULL) @@ -4971,6 +4965,15 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) return 0; } +static char *name_op[] = { + "__lt__", + "__le__", + "__eq__", + "__ne__", + "__gt__", + "__ge__", +}; + static PyObject * half_richcompare(PyObject *self, PyObject *other, int op) { |