def ll_strhash(s): # unlike CPython, there is no reason to avoid to return -1 # but our malloc initializes the memory to zero, so we use zero as the # special non-computed-yet value. x = s.hash if x == 0: x = _hash_string(s.chars) if x == 0: x = 29872897 s.hash = x return x
def test_compute_hash(): from pypy.rlib.objectmodel import _hash_string, _hash_float, _hash_tuple assert compute_hash("Hello") == _hash_string("Hello") assert compute_hash(7) == 7 assert compute_hash(-3.5) == _hash_float(-3.5) assert compute_hash(None) == 0 assert compute_hash(("world", None, 7)) == _hash_tuple(("world", None, 7)) # class Foo(object): def __hash__(self): return 42 foo = Foo() h = compute_hash(foo) assert h == object.__hash__(foo) assert h == getattr(foo, '__precomputed_identity_hash') assert compute_hash(None) == 0