Example #1
0
    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
Example #2
0
    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
Example #3
0
    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
Example #4
0
    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
Example #5
0
 def f():
     d2 = D()
     return (
         compute_identity_hash(d2),
         current_object_addr_as_int(d2),
         compute_identity_hash(c),
         compute_identity_hash(d),
     )
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
 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
Example #9
0
 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
Example #10
0
 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)
Example #11
0
 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)
Example #12
0
 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
Example #13
0
 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
Example #14
0
 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
Example #15
0
 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
Example #16
0
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
Example #17
0
 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
Example #18
0
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
Example #19
0
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
Example #20
0
 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
Example #21
0
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
Example #22
0
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
Example #23
0
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
Example #24
0
 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
Example #25
0
 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
Example #26
0
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
Example #27
0
 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
Example #28
0
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)
Example #29
0
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)
Example #30
0
 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
Example #31
0
 def method_hash(self, space):
     return space.newint(compute_identity_hash(self))
Example #32
0
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)
Example #33
0
 def hash(self):
     return compute_identity_hash(self.dtype)
Example #34
0
File: util.py Project: njues/Sypy
def descrlist_hash(l):
    res = 0x345678
    for descr in l:
        y = compute_identity_hash(descr)
        res = intmask((1000003 * res) ^ y)
    return res
Example #35
0
def descrlist_hash(l):
    res = 0x345678
    for descr in l:
        y = compute_identity_hash(descr)
        res = intmask((1000003 * res) ^ y)
    return res
Example #36
0
 def _get_hash_(self):
     return compute_identity_hash(self)
Example #37
0
def ll_debugrepr(x):
    if x:
        h = compute_identity_hash(x)
    else:
        h = 0
    return '<%x>' % (h, )
Example #38
0
def ll_debugrepr(x):
    if x:
        h = compute_identity_hash(x)
    else:
        h = 0
    return '<%x>' % (h,)
Example #39
0
 def _get_hash_(self):
     return compute_identity_hash(self)
Example #40
0
 def method_hash(self, space):
     return space.newint(compute_identity_hash(self))
Example #41
0
def default_identity_hash(space, w_obj):
    return space.wrap(compute_identity_hash(w_obj))
Example #42
0
 def f():
     d2 = D()
     return (compute_identity_hash(d2), current_object_addr_as_int(d2),
             compute_identity_hash(c), compute_identity_hash(d))
Example #43
0
def default_identity_hash(space, w_obj):
    return space.wrap(compute_identity_hash(w_obj))
Example #44
0
 def f(n):
     z = Z()
     got = my_gethash(z)
     expected = compute_identity_hash(z)
     return got - expected
Example #45
0
 def hash(self):
     return compute_identity_hash(self.dtype)
Example #46
0
 def f(n):
     z = Z()
     got = my_gethash(z)
     expected = compute_identity_hash(z)
     return got - expected