Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
With some changes of my own thrown in (e.g. backport of r58107).
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 @@
 	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 @@
 	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 @@
 		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 @@
 	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 @@
 		}
 	}
 
+	/* 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 @@
 	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)
 {