Commit d1abd25e authored by Nick Coghlan's avatar Nick Coghlan

Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash...

Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
parent e6528211
...@@ -438,6 +438,7 @@ PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); ...@@ -438,6 +438,7 @@ PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
PyObject *, PyObject *); PyObject *, PyObject *);
PyAPI_FUNC(long) PyObject_Hash(PyObject *); PyAPI_FUNC(long) PyObject_Hash(PyObject *);
PyAPI_FUNC(long) PyObject_HashNotImplemented(PyObject *);
PyAPI_FUNC(int) PyObject_IsTrue(PyObject *); PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
PyAPI_FUNC(int) PyObject_Not(PyObject *); PyAPI_FUNC(int) PyObject_Not(PyObject *);
PyAPI_FUNC(int) PyCallable_Check(PyObject *); PyAPI_FUNC(int) PyCallable_Check(PyObject *);
......
...@@ -3783,10 +3783,8 @@ class Context(object): ...@@ -3783,10 +3783,8 @@ class Context(object):
for flag in flags: for flag in flags:
self._ignored_flags.remove(flag) self._ignored_flags.remove(flag)
def __hash__(self):
"""A Context cannot be hashed."""
# We inherit object.__hash__, so we must deny this explicitly # We inherit object.__hash__, so we must deny this explicitly
raise TypeError("Cannot hash a Context.") __hash__ = None
def Etiny(self): def Etiny(self):
"""Returns Etiny (= Emin - prec + 1)""" """Returns Etiny (= Emin - prec + 1)"""
......
...@@ -212,8 +212,7 @@ class CommonTest(unittest.TestCase): ...@@ -212,8 +212,7 @@ class CommonTest(unittest.TestCase):
# So instances of AllEq must be found in all non-empty sequences. # So instances of AllEq must be found in all non-empty sequences.
def __eq__(self, other): def __eq__(self, other):
return True return True
def __hash__(self): __hash__ = None # Can't meet hash invariant requirements
raise NotImplemented
self.assert_(AllEq() not in self.type2test([])) self.assert_(AllEq() not in self.type2test([]))
self.assert_(AllEq() in self.type2test([1])) self.assert_(AllEq() in self.type2test([1]))
......
...@@ -3070,12 +3070,20 @@ order (MRO) for bases """ ...@@ -3070,12 +3070,20 @@ order (MRO) for bases """
self.assertEqual(hash(d), 144) self.assertEqual(hash(d), 144)
D.__hash__ = lambda self: 100 D.__hash__ = lambda self: 100
self.assertEqual(hash(d), 100) self.assertEqual(hash(d), 100)
D.__hash__ = None
self.assertRaises(TypeError, hash, d)
del D.__hash__ del D.__hash__
self.assertEqual(hash(d), 144) self.assertEqual(hash(d), 144)
B.__hash__ = None
self.assertRaises(TypeError, hash, d)
del B.__hash__ del B.__hash__
self.assertEqual(hash(d), 314) self.assertEqual(hash(d), 314)
C.__hash__ = None
self.assertRaises(TypeError, hash, d)
del C.__hash__ del C.__hash__
self.assertEqual(hash(d), 42) self.assertEqual(hash(d), 42)
A.__hash__ = None
self.assertRaises(TypeError, hash, d)
del A.__hash__ del A.__hash__
self.assertEqual(hash(d), orig_hash) self.assertEqual(hash(d), orig_hash)
d.foo = 42 d.foo = 42
......
# test the invariant that # test the invariant that
# iff a==b then hash(a)==hash(b) # iff a==b then hash(a)==hash(b)
# #
# Also test that hash implementations are inherited as expected
import unittest import unittest
from test import support from test import support
from collections import Hashable
class HashEqualityTestCase(unittest.TestCase): class HashEqualityTestCase(unittest.TestCase):
...@@ -37,8 +39,74 @@ class HashEqualityTestCase(unittest.TestCase): ...@@ -37,8 +39,74 @@ class HashEqualityTestCase(unittest.TestCase):
self.same_hash(float(0.5), complex(0.5, 0.0)) self.same_hash(float(0.5), complex(0.5, 0.0))
_default_hash = object.__hash__
class DefaultHash(object): pass
_FIXED_HASH_VALUE = 42
class FixedHash(object):
def __hash__(self):
return _FIXED_HASH_VALUE
class OnlyEquality(object):
def __eq__(self, other):
return self is other
class OnlyInequality(object):
def __ne__(self, other):
return self is not other
class OnlyCmp(object):
def __cmp__(self, other):
return cmp(id(self), id(other))
class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
class NoHash(object):
__hash__ = None
class HashInheritanceTestCase(unittest.TestCase):
default_expected = [object(),
DefaultHash(),
OnlyInequality(),
]
fixed_expected = [FixedHash(),
InheritedHashWithEquality(),
InheritedHashWithInequality(),
InheritedHashWithCmp(),
]
error_expected = [NoHash(),
OnlyEquality(),
OnlyCmp(),
]
def test_default_hash(self):
for obj in self.default_expected:
self.assertEqual(hash(obj), _default_hash(obj))
def test_fixed_hash(self):
for obj in self.fixed_expected:
self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
def test_error_hash(self):
for obj in self.error_expected:
self.assertRaises(TypeError, hash, obj)
def test_hashable(self):
objects = (self.default_expected +
self.fixed_expected)
for obj in objects:
self.assert_(isinstance(obj, Hashable), repr(obj))
def test_not_hashable(self):
for obj in self.error_expected:
self.assertFalse(isinstance(obj, Hashable), repr(obj))
def test_main(): def test_main():
support.run_unittest(HashEqualityTestCase) support.run_unittest(HashEqualityTestCase,
HashInheritanceTestCase)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -48,8 +48,7 @@ class Vector: ...@@ -48,8 +48,7 @@ class Vector:
def __setitem__(self, i, v): def __setitem__(self, i, v):
self.data[i] = v self.data[i] = v
def __hash__(self): __hash__ = None # Vectors cannot be hashed
raise TypeError("Vectors cannot be hashed")
def __bool__(self): def __bool__(self):
raise TypeError("Vectors cannot be used in Boolean contexts") raise TypeError("Vectors cannot be used in Boolean contexts")
...@@ -85,35 +84,6 @@ class Vector: ...@@ -85,35 +84,6 @@ class Vector:
raise ValueError("Cannot compare vectors of different length") raise ValueError("Cannot compare vectors of different length")
return other return other
class SimpleOrder(object):
"""
A simple class that defines order but not full comparison.
"""
def __init__(self, value):
self.value = value
def __lt__(self, other):
if not isinstance(other, SimpleOrder):
return True
return self.value < other.value
def __gt__(self, other):
if not isinstance(other, SimpleOrder):
return False
return self.value > other.value
class DumbEqualityWithoutHash(object):
"""
A class that define __eq__, but no __hash__: it shouldn't be hashable.
"""
def __eq__(self, other):
return False
opmap = { opmap = {
"lt": (lambda a,b: a< b, operator.lt, operator.__lt__), "lt": (lambda a,b: a< b, operator.lt, operator.__lt__),
"le": (lambda a,b: a<=b, operator.le, operator.__le__), "le": (lambda a,b: a<=b, operator.le, operator.__le__),
...@@ -360,38 +330,8 @@ class ListTest(unittest.TestCase): ...@@ -360,38 +330,8 @@ class ListTest(unittest.TestCase):
self.assertIs(op(x, y), True) self.assertIs(op(x, y), True)
class HashableTest(unittest.TestCase):
"""
Test hashability of classes with rich operators defined.
"""
def test_simpleOrderHashable(self):
"""
A class that only defines __gt__ and/or __lt__ should be hashable.
"""
a = SimpleOrder(1)
b = SimpleOrder(2)
self.assert_(a < b)
self.assert_(b > a)
self.assert_(a.__hash__ is not None)
def test_notHashableException(self):
"""
If a class is not hashable, it should raise a TypeError with an
understandable message.
"""
a = DumbEqualityWithoutHash()
try:
hash(a)
except TypeError as e:
self.assertEquals(str(e),
"unhashable type: 'DumbEqualityWithoutHash'")
else:
raise support.TestFailed("Should not be here")
def test_main(): def test_main():
support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest, HashableTest) support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest)
if __name__ == "__main__": if __name__ == "__main__":
test_main() test_main()
...@@ -608,13 +608,6 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg) ...@@ -608,13 +608,6 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg)
return 0; return 0;
} }
static long
deque_nohash(PyObject *self)
{
PyErr_SetString(PyExc_TypeError, "deque objects are unhashable");
return -1;
}
static PyObject * static PyObject *
deque_copy(PyObject *deque) deque_copy(PyObject *deque)
{ {
...@@ -858,7 +851,7 @@ static PyTypeObject deque_type = { ...@@ -858,7 +851,7 @@ static PyTypeObject deque_type = {
0, /* tp_as_number */ 0, /* tp_as_number */
&deque_as_sequence, /* tp_as_sequence */ &deque_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
deque_nohash, /* tp_hash */ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
......
...@@ -2031,7 +2031,7 @@ PyTypeObject PyDict_Type = { ...@@ -2031,7 +2031,7 @@ PyTypeObject PyDict_Type = {
0, /* tp_as_number */ 0, /* tp_as_number */
&dict_as_sequence, /* tp_as_sequence */ &dict_as_sequence, /* tp_as_sequence */
&dict_as_mapping, /* tp_as_mapping */ &dict_as_mapping, /* tp_as_mapping */
0, /* tp_hash */ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
......
...@@ -2568,7 +2568,7 @@ PyTypeObject PyList_Type = { ...@@ -2568,7 +2568,7 @@ PyTypeObject PyList_Type = {
0, /* tp_as_number */ 0, /* tp_as_number */
&list_as_sequence, /* tp_as_sequence */ &list_as_sequence, /* tp_as_sequence */
&list_as_mapping, /* tp_as_mapping */ &list_as_mapping, /* tp_as_mapping */
0, /* tp_hash */ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
......
...@@ -781,17 +781,22 @@ finally: ...@@ -781,17 +781,22 @@ finally:
#endif #endif
} }
long
PyObject_HashNotImplemented(PyObject *v)
{
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
Py_TYPE(v)->tp_name);
return -1;
}
long long
PyObject_Hash(PyObject *v) PyObject_Hash(PyObject *v)
{ {
PyTypeObject *tp = v->ob_type; PyTypeObject *tp = Py_TYPE(v);
if (tp->tp_hash != NULL) if (tp->tp_hash != NULL)
return (*tp->tp_hash)(v); return (*tp->tp_hash)(v);
/* Otherwise, the object can't be hashed */ /* Otherwise, the object can't be hashed */
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", return PyObject_HashNotImplemented(v);
v->ob_type->tp_name);
return -1;
} }
PyObject * PyObject *
......
...@@ -2092,7 +2092,7 @@ PyTypeObject PySet_Type = { ...@@ -2092,7 +2092,7 @@ PyTypeObject PySet_Type = {
&set_as_number, /* tp_as_number */ &set_as_number, /* tp_as_number */
&set_as_sequence, /* tp_as_sequence */ &set_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
......
...@@ -3810,13 +3810,15 @@ PyType_Ready(PyTypeObject *type) ...@@ -3810,13 +3810,15 @@ PyType_Ready(PyTypeObject *type)
/* Hack for tp_hash and __hash__. /* Hack for tp_hash and __hash__.
If after all that, tp_hash is still NULL, and __hash__ is not in If after all that, tp_hash is still NULL, and __hash__ is not in
tp_dict, set tp_dict['__hash__'] equal to None. tp_dict, set tp_hash to PyObject_HashNotImplemented and
tp_dict['__hash__'] equal to None.
This signals that __hash__ is not inherited. This signals that __hash__ is not inherited.
*/ */
if (type->tp_hash == NULL) { if (type->tp_hash == NULL) {
if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) { if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0) if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
goto error; goto error;
type->tp_hash = PyObject_HashNotImplemented;
} }
} }
...@@ -4943,9 +4945,7 @@ slot_tp_hash(PyObject *self) ...@@ -4943,9 +4945,7 @@ slot_tp_hash(PyObject *self)
} }
if (func == NULL) { if (func == NULL) {
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", return PyObject_HashNotImplemented(self);
Py_TYPE(self)->tp_name);
return -1;
} }
res = PyEval_CallObject(func, NULL); res = PyEval_CallObject(func, NULL);
...@@ -5676,6 +5676,13 @@ update_one_slot(PyTypeObject *type, slotdef *p) ...@@ -5676,6 +5676,13 @@ update_one_slot(PyTypeObject *type, slotdef *p)
sanity checks. I'll buy the first person to sanity checks. I'll buy the first person to
point out a bug in this reasoning a beer. */ point out a bug in this reasoning a beer. */
} }
else if (descr == Py_None &&
strcmp(p->name, "__hash__") == 0) {
/* We specifically allow __hash__ to be set to None
to prevent inheritance of the default
implementation from object.__hash__ */
specific = PyObject_HashNotImplemented;
}
else { else {
use_generic = 1; use_generic = 1;
generic = p->function; generic = p->function;
...@@ -5889,6 +5896,14 @@ add_operators(PyTypeObject *type) ...@@ -5889,6 +5896,14 @@ add_operators(PyTypeObject *type)
continue; continue;
if (PyDict_GetItem(dict, p->name_strobj)) if (PyDict_GetItem(dict, p->name_strobj))
continue; continue;
if (*ptr == PyObject_HashNotImplemented) {
/* Classes may prevent the inheritance of the tp_hash
slot by storing PyObject_HashNotImplemented in it. Make it
visible as a None value for the __hash__ attribute. */
if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
return -1;
}
else {
descr = PyDescr_NewWrapper(type, p, *ptr); descr = PyDescr_NewWrapper(type, p, *ptr);
if (descr == NULL) if (descr == NULL)
return -1; return -1;
...@@ -5896,6 +5911,7 @@ add_operators(PyTypeObject *type) ...@@ -5896,6 +5911,7 @@ add_operators(PyTypeObject *type)
return -1; return -1;
Py_DECREF(descr); Py_DECREF(descr);
} }
}
if (type->tp_new != NULL) { if (type->tp_new != NULL) {
if (add_tp_new_wrapper(type) < 0) if (add_tp_new_wrapper(type) < 0)
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