diff options
Diffstat (limited to 'Objects/typeobject.c')
-rw-r--r-- | Objects/typeobject.c | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 59dec4a..1a221c8 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2590,12 +2590,6 @@ object_str(PyObject *self) return f(self); } -static long -object_hash(PyObject *self) -{ - return _Py_HashPointer(self); -} - static PyObject * object_get_class(PyObject *self, void *closure) { @@ -3030,7 +3024,7 @@ PyTypeObject PyBaseObject_Type = { 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ - object_hash, /* tp_hash */ + (hashfunc)_Py_HashPointer, /* tp_hash */ 0, /* tp_call */ object_str, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ @@ -3236,6 +3230,33 @@ 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 int +overrides_hash(PyTypeObject *type) +{ + int i; + PyObject *dict = type->tp_dict; + + assert(dict != NULL); + for (i = 0; i < 8; i++) { + if (PyDict_GetItemString(dict, name_op[i]) != NULL) + return 1; + } + return 0; +} + static void inherit_slots(PyTypeObject *type, PyTypeObject *base) { @@ -3367,7 +3388,8 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base) if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { if (type->tp_compare == NULL && type->tp_richcompare == NULL && - type->tp_hash == NULL) + type->tp_hash == NULL && + !overrides_hash(type)) { type->tp_compare = base->tp_compare; type->tp_richcompare = base->tp_richcompare; @@ -3548,6 +3570,18 @@ PyType_Ready(PyTypeObject *type) } } + /* Hack for tp_hash and __hash__. + If after all that, tp_hash is still NULL, and __hash__ is not in + tp_dict, set tp_dict['__hash__'] equal to None. + This signals that __hash__ is not inherited. + */ + if (type->tp_hash == NULL && + PyDict_GetItemString(type->tp_dict, "__hash__") == NULL && + PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) + { + goto error; + } + /* Some more special stuff */ base = type->tp_base; if (base != NULL) { @@ -4937,16 +4971,6 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) return 0; } -/* Map rich comparison operators to their __xx__ namesakes */ -static char *name_op[] = { - "__lt__", - "__le__", - "__eq__", - "__ne__", - "__gt__", - "__ge__", -}; - static PyObject * half_richcompare(PyObject *self, PyObject *other, int op) { |