def test_hash_preservation(self): from pypy.rlib.objectmodel import current_object_addr_as_int from pypy.rlib.objectmodel import compute_identity_hash class C: pass class D(C): pass c = C() d = D() h_c = compute_identity_hash(c) h_d = compute_identity_hash(d) # def f(): d2 = D() return (compute_identity_hash(d2), current_object_addr_as_int(d2), compute_identity_hash(c), compute_identity_hash(d)) res = self.interpret(f, []) # xxx the following test is too precise, checking the exact # implementation. On Python 2.7 it doesn't work anyway, because # object.__hash__(x) is different from id(x). The test is disabled # for now, and nobody should rely on compute_identity_hash() returning # a value that is (or was) the current_object_addr_as_int(). # --- disabled: assert res.item0 == res.item1 # the following property is essential on top of the lltypesystem # otherwise prebuilt dictionaries are broken. It's wrong on # top of the ootypesystem though. if isinstance(self, LLRtypeMixin): assert res.item2 == h_c assert res.item3 == h_d
def test_hash_preservation(self): from pypy.rlib.objectmodel import current_object_addr_as_int from pypy.rlib.objectmodel import compute_identity_hash class C: pass class D(C): pass c = C() d = D() h_c = compute_identity_hash(c) h_d = compute_identity_hash(d) # def f(): d2 = D() return ( compute_identity_hash(d2), current_object_addr_as_int(d2), compute_identity_hash(c), compute_identity_hash(d), ) res = self.interpret(f, []) # xxx this is too precise, checking the exact implementation assert res.item0 == res.item1 # the following property is essential on top of the lltypesystem # otherwise prebuilt dictionaries are broken. It's wrong on # top of the ootypesystem though. if type(self) is TestLLtype: assert res.item2 == h_c assert res.item3 == h_d
def test_hash_preservation(self): from pypy.rlib.objectmodel import current_object_addr_as_int from pypy.rlib.objectmodel import compute_identity_hash class C: pass class D(C): pass c = C() d = D() h_c = compute_identity_hash(c) h_d = compute_identity_hash(d) # def f(): d2 = D() return (compute_identity_hash(d2), current_object_addr_as_int(d2), compute_identity_hash(c), compute_identity_hash(d)) res = self.interpret(f, []) # xxx this is too precise, checking the exact implementation assert res.item0 == res.item1 # the following property is essential on top of the lltypesystem # otherwise prebuilt dictionaries are broken. It's wrong on # top of the ootypesystem though. if type(self) is TestLLtype: assert res.item2 == h_c assert res.item3 == h_d
def test_hash_preservation(self): from pypy.rlib.objectmodel import current_object_addr_as_int from pypy.rlib.objectmodel import compute_identity_hash class C: pass class D(C): pass c = C() d = D() h_c = compute_identity_hash(c) h_d = compute_identity_hash(d) # def f(): d2 = D() return (compute_identity_hash(d2), current_object_addr_as_int(d2), compute_identity_hash(c), compute_identity_hash(d)) res = self.interpret(f, []) # xxx the following test is too precise, checking the exact # implementation. On Python 2.7 it doesn't work anyway, because # object.__hash__(x) is different from id(x). The test is disabled # for now, and nobody should rely on compute_identity_hash() returning # a value that is (or was) the current_object_addr_as_int(). # --- disabled: assert res.item0 == res.item1 # the following property is essential on top of the lltypesystem # otherwise prebuilt dictionaries are broken. It's wrong on # top of the ootypesystem though. if isinstance(self, LLRtypeMixin): assert res.item2 == h_c assert res.item3 == h_d
def f(): d2 = D() return ( compute_identity_hash(d2), current_object_addr_as_int(d2), compute_identity_hash(c), compute_identity_hash(d), )
def fn(): objects = [] hashes = [] for i in range(200): rgc.collect(0) # nursery-only collection, if possible obj = A() objects.append(obj) hashes.append(compute_identity_hash(obj)) unique = {} for i in range(len(objects)): assert compute_identity_hash(objects[i]) == hashes[i] unique[hashes[i]] = None return len(unique)
def fn(): objects = [] hashes = [] for i in range(200): rgc.collect(0) # nursery-only collection, if possible obj = A() objects.append(obj) hashes.append(compute_identity_hash(obj)) unique = {} for i in range(len(objects)): assert compute_identity_hash(objects[i]) == hashes[i] unique[hashes[i]] = None return len(unique)
def f(): if compute_hash(c) != compute_identity_hash(c): return 12 if compute_hash(d) != h_d: return 13 if compute_hash(("Hi", None, (7.5, 2, d))) != h_t: return 14 c2 = C() h_c2 = compute_hash(c2) if compute_hash(c2) != h_c2: return 15 if compute_identity_hash(s) == h_s: return 16 # unlikely i = 0 while i < 6: rgc.collect() if compute_hash(c2) != h_c2: return i i += 1 return 42
def f(): if compute_hash(c) != compute_identity_hash(c): return 12 if compute_hash(d) != h_d: return 13 if compute_hash(("Hi", None, (7.5, 2, d))) != h_t: return 14 c2 = C() h_c2 = compute_hash(c2) if compute_hash(c2) != h_c2: return 15 if compute_identity_hash(s) == h_s: return 16 # unlikely i = 0 while i < 6: rgc.collect() if compute_hash(c2) != h_c2: return i i += 1 return 42
def f(): rgc.collect() s2 = g() h2 = compute_identity_hash(s2) rgc.collect() # shift s2 to the left, but add a hash field assert s2.p1.x == 1010 assert s2.p2.x == 1020 assert s2.p3.x == 1030 assert s2.p4.x == 1040 assert s2.p5.x == 1050 assert s2.p6.x == 1060 assert s2.p7.x == 1070 assert s2.p8.x == 1080 assert s2.p9.x == 1090 return h2 - compute_identity_hash(s2)
def f(): rgc.collect() s2 = g() h2 = compute_identity_hash(s2) rgc.collect() # shift s2 to the left, but add a hash field assert s2.p1.x == 1010 assert s2.p2.x == 1020 assert s2.p3.x == 1030 assert s2.p4.x == 1040 assert s2.p5.x == 1050 assert s2.p6.x == 1060 assert s2.p7.x == 1070 assert s2.p8.x == 1080 assert s2.p9.x == 1090 return h2 - compute_identity_hash(s2)
def f1(): d2 = D() # xxx we assume that current_object_addr_as_int is defined as # simply returning the identity hash current_identityhash = current_object_addr_as_int(d2) instance_hash = compute_identity_hash(d2) return current_identityhash == instance_hash
def define_hash_preservation(cls): from pypy.rlib.objectmodel import compute_hash from pypy.rlib.objectmodel import compute_identity_hash from pypy.rlib.objectmodel import current_object_addr_as_int class C: pass class D(C): pass c = C() d = D() h_d = compute_hash(d) # force to be cached on 'd', but not on 'c' h_t = compute_hash(("Hi", None, (7.5, 2, d))) S = lltype.GcStruct('S', ('x', lltype.Signed), ('a', lltype.Array(lltype.Signed))) s = lltype.malloc(S, 15, zero=True) h_s = compute_identity_hash(s) # varsized: hash not saved/restored # def f(): if compute_hash(c) != compute_identity_hash(c): return 12 if compute_hash(d) != h_d: return 13 if compute_hash(("Hi", None, (7.5, 2, d))) != h_t: return 14 c2 = C() h_c2 = compute_hash(c2) if compute_hash(c2) != h_c2: return 15 if compute_identity_hash(s) == h_s: return 16 # unlikely i = 0 while i < 6: rgc.collect() if compute_hash(c2) != h_c2: return i i += 1 return 42 return f
def define_hash_preservation(cls): from pypy.rlib.objectmodel import compute_hash from pypy.rlib.objectmodel import compute_identity_hash from pypy.rlib.objectmodel import current_object_addr_as_int class C: pass class D(C): pass c = C() d = D() h_d = compute_hash(d) # force to be cached on 'd', but not on 'c' h_t = compute_hash(("Hi", None, (7.5, 2, d))) S = lltype.GcStruct('S', ('x', lltype.Signed), ('a', lltype.Array(lltype.Signed))) s = lltype.malloc(S, 15, zero=True) h_s = compute_identity_hash(s) # varsized: hash not saved/restored # def f(): if compute_hash(c) != compute_identity_hash(c): return 12 if compute_hash(d) != h_d: return 13 if compute_hash(("Hi", None, (7.5, 2, d))) != h_t: return 14 c2 = C() h_c2 = compute_hash(c2) if compute_hash(c2) != h_c2: return 15 if compute_identity_hash(s) == h_s: return 16 # unlikely i = 0 while i < 6: rgc.collect() if compute_hash(c2) != h_c2: return i i += 1 return 42 return f
def f1(): d2 = D() # xxx we assume that current_object_addr_as_int is defined as # simply returning the identity hash current_identityhash = current_object_addr_as_int(d2) instance_hash = compute_identity_hash(d2) return current_identityhash == instance_hash
def ll_set_null(d, llkey): hash = compute_identity_hash(llkey) i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK if d.entries.everused(i): # If the entry was ever used, clean up its key and value. # We don't store a NULL value, but a dead weakref, because # the entry must still be marked as everused(). d.entries[i].key = llmemory.dead_wref d.entries[i].value = NULLVALUE
def build(xr, n): "Build the identity hashes of all n objects of the chain." i = 0 while i < n: xr.hash = compute_identity_hash(xr) # ^^^ likely to trigger a collection xr = xr.prev i += 1 assert xr is None
def args_hash(args): res = 0x345678 for arg in args: if isinstance(arg, history.Const): y = arg._get_hash_() else: y = compute_identity_hash(arg) res = intmask((1000003 * res) ^ y) return res
def args_hash(args): res = 0x345678 for arg in args: if isinstance(arg, history.Const): y = arg._get_hash_() else: y = compute_identity_hash(arg) res = intmask((1000003 * res) ^ y) return res
def build(xr, n): "Build the identity hashes of all n objects of the chain." i = 0 while i < n: xr.hash = compute_identity_hash(xr) # ^^^ likely to trigger a collection xr = xr.prev i += 1 assert xr is None
def ll_set_null(d, llkey): hash = compute_identity_hash(llkey) i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK if d.entries.everused(i): # If the entry was ever used, clean up its key and value. # We don't store a NULL value, but a dead weakref, because # the entry must still be marked as everused(). d.entries[i].key = llmemory.dead_wref d.entries[i].value = NULLVALUE
def ll_get(d, llkey): hash = compute_identity_hash(llkey) i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK #llop.debug_print(lltype.Void, i, 'get', hex(hash), # ll_debugrepr(d.entries[i].key), # ll_debugrepr(d.entries[i].value)) # NB. ll_valid() above was just called at least on entry i, so if # it is an invalid entry with a dead weakref, the value was reset # to NULLVALUE. return d.entries[i].value
def ll_get(d, llkey): hash = compute_identity_hash(llkey) i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK #llop.debug_print(lltype.Void, i, 'get', hex(hash), # ll_debugrepr(d.entries[i].key), # ll_debugrepr(d.entries[i].value)) # NB. ll_valid() above was just called at least on entry i, so if # it is an invalid entry with a dead weakref, the value was reset # to NULLVALUE. return d.entries[i].value
def check(xr, n, step): "Check that the identity hashes are still correct." i = 0 while i < n: if xr.hash != compute_identity_hash(xr): os.write(2, "wrong hash! i=%d, n=%d, step=%d\n" % (i, n, step)) raise ValueError xr = xr.prev i += 1 assert xr is None
def check(xr, n, step): "Check that the identity hashes are still correct." i = 0 while i < n: if xr.hash != compute_identity_hash(xr): os.write(2, "wrong hash! i=%d, n=%d, step=%d\n" % (i, n, step)) raise ValueError xr = xr.prev i += 1 assert xr is None
def args_hash(args): make_sure_not_resized(args) res = 0x345678 for arg in args: if arg is None: y = 17 elif isinstance(arg, history.Const): y = arg._get_hash_() else: y = compute_identity_hash(arg) res = intmask((1000003 * res) ^ y) return res
def descr_hash(self, space): w_func = self.getattr(space, "__hash__", False) if w_func is None: w_eq = self.getattr(space, "__eq__", False) w_cmp = self.getattr(space, "__cmp__", False) if w_eq is not None or w_cmp is not None: raise OperationError(space.w_TypeError, space.wrap("unhashable instance")) else: return space.wrap(compute_identity_hash(self)) w_ret = space.call_function(w_func) if not space.is_true(space.isinstance(w_ret, space.w_int)) and not space.is_true( space.isinstance(w_ret, space.w_long) ): raise OperationError(space.w_TypeError, space.wrap("__hash__ must return int or long")) return w_ret
def ll_set_nonnull(d, llkey, llvalue): hash = compute_identity_hash(llkey) keyref = weakref_create(llkey) # GC effects here, before the rest i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK everused = d.entries.everused(i) d.entries[i].key = keyref d.entries[i].value = llvalue d.entries[i].f_hash = hash #llop.debug_print(lltype.Void, i, 'stored', hex(hash), # ll_debugrepr(llkey), # ll_debugrepr(llvalue)) if not everused: d.resize_counter -= 3 if d.resize_counter <= 0: #llop.debug_print(lltype.Void, 'RESIZE') ll_weakdict_resize(d)
def ll_set_nonnull(d, llkey, llvalue): hash = compute_identity_hash(llkey) keyref = weakref_create(llkey) # GC effects here, before the rest i = rdict.ll_dict_lookup(d, llkey, hash) & rdict.MASK everused = d.entries.everused(i) d.entries[i].key = keyref d.entries[i].value = llvalue d.entries[i].f_hash = hash #llop.debug_print(lltype.Void, i, 'stored', hex(hash), # ll_debugrepr(llkey), # ll_debugrepr(llvalue)) if not everused: d.resize_counter -= 3 if d.resize_counter <= 0: #llop.debug_print(lltype.Void, 'RESIZE') ll_weakdict_resize(d)
def descr_hash(self, space): w_func = self.getattr(space, space.wrap('__hash__'), False) if w_func is None: w_eq = self.getattr(space, space.wrap('__eq__'), False) w_cmp = self.getattr(space, space.wrap('__cmp__'), False) if w_eq is not None or w_cmp is not None: raise OperationError(space.w_TypeError, space.wrap("unhashable instance")) else: return space.wrap(compute_identity_hash(self)) w_ret = space.call_function(w_func) if (not space.is_true(space.isinstance(w_ret, space.w_int)) and not space.is_true(space.isinstance(w_ret, space.w_long))): raise OperationError( space.w_TypeError, space.wrap("__hash__ must return int or long")) return w_ret
def method_hash(self, space): return space.newint(compute_identity_hash(self))
def default_identity_hash(space, w_obj): w_unique_id = w_obj.immutable_unique_id(space) if w_unique_id is None: # common case return space.wrap(compute_identity_hash(w_obj)) else: return space.hash(w_unique_id)
def hash(self): return compute_identity_hash(self.dtype)
def descrlist_hash(l): res = 0x345678 for descr in l: y = compute_identity_hash(descr) res = intmask((1000003 * res) ^ y) return res
def descrlist_hash(l): res = 0x345678 for descr in l: y = compute_identity_hash(descr) res = intmask((1000003 * res) ^ y) return res
def _get_hash_(self): return compute_identity_hash(self)
def ll_debugrepr(x): if x: h = compute_identity_hash(x) else: h = 0 return '<%x>' % (h, )
def ll_debugrepr(x): if x: h = compute_identity_hash(x) else: h = 0 return '<%x>' % (h,)
def _get_hash_(self): return compute_identity_hash(self)
def method_hash(self, space): return space.newint(compute_identity_hash(self))
def default_identity_hash(space, w_obj): return space.wrap(compute_identity_hash(w_obj))
def f(): d2 = D() return (compute_identity_hash(d2), current_object_addr_as_int(d2), compute_identity_hash(c), compute_identity_hash(d))
def default_identity_hash(space, w_obj): return space.wrap(compute_identity_hash(w_obj))
def f(n): z = Z() got = my_gethash(z) expected = compute_identity_hash(z) return got - expected
def hash(self): return compute_identity_hash(self.dtype)
def f(n): z = Z() got = my_gethash(z) expected = compute_identity_hash(z) return got - expected