Пример #1
0
    def test_prebuilt_weakref(self):
        import weakref
        from pypy.rlib import rgc

        class A:
            pass

        a = A()
        a.hello = 42
        r1 = weakref.ref(a)
        r2 = weakref.ref(A())
        rgc.collect()
        assert r2() is None

        def fn(n):
            if n:
                r = r1
            else:
                r = r2
            a = r()
            rgc.collect()
            if a is None:
                return -5
            else:
                return a.hello

        c_fn = self.getcompiled(fn, [int])
        res = c_fn(1)
        assert res == 42
        res = c_fn(0)
        assert res == -5
Пример #2
0
 def func():
     rgc.collect(
     )  # check that a prebuilt erased integer doesn't explode
     u.x = rerased.erase_int(1000)
     u.tagged = True
     fn()
     return 1
Пример #3
0
 def test_prebuilt_weakref(self):
     import weakref
     from pypy.rlib import rgc
     class A:
         pass
     a = A()
     a.hello = 42
     r1 = weakref.ref(a)
     r2 = weakref.ref(A())
     rgc.collect()
     assert r2() is None
     def fn(n):
         if n:
             r = r1
         else:
             r = r2
         a = r()
         rgc.collect()
         if a is None:
             return -5
         else:
             return a.hello
     c_fn = self.getcompiled(fn, [int])
     res = c_fn(1)
     assert res == 42
     res = c_fn(0)
     assert res == -5
Пример #4
0
    def test_prebuilt_weakref(self):
        import weakref
        from pypy.rlib import rgc

        class A:
            pass

        a = A()
        a.hello = 42
        refs = [weakref.ref(a), weakref.ref(A())]
        rgc.collect()

        def fn():
            result = 0
            for i in range(2):
                a = refs[i]()
                rgc.collect()
                if a is None:
                    result += (i + 1)
                else:
                    result += a.hello * (i + 1)
            return result

        mod, f = compile_test(fn, [], gcpolicy=self.gcpolicy)
        res = f()
        assert res == fn()
Пример #5
0
    def test_prebuilt_weakref(self):
        import weakref
        from pypy.rlib import rgc

        class A:
            pass

        a = A()
        a.hello = 42
        refs = [weakref.ref(a), weakref.ref(A())]
        rgc.collect()

        def fn():
            result = 0
            for i in range(2):
                a = refs[i]()
                rgc.collect()
                if a is None:
                    result += i + 1
                else:
                    result += a.hello * (i + 1)
            return result

        mod, f = compile_test(fn, [], gcpolicy=self.gcpolicy)
        res = f()
        assert res == fn()
Пример #6
0
 def f():
     s = lltype.malloc(S)
     s.x = 42
     llop.bare_setfield(lltype.Void, s0, void('next'), s)
     llop.gc_assume_young_pointers(lltype.Void,
                                   llmemory.cast_ptr_to_adr(s0))
     rgc.collect(0)
     return s0.next.x
Пример #7
0
 def fn(n):
     if n > 0:
         x = BoxedObject(n)
     else:
         x = UnboxedObject(n)
     f.l.append(x)
     rgc.collect()
     return f.l[-1].meth(100)
Пример #8
0
 def func():
     #try:
     a = rgc.malloc_nonmovable(TP, 3, zero=True)
     rgc.collect()
     if a:
         assert not rgc.can_move(a)
         return 0
     return 1
Пример #9
0
 def f():
     # numbers optimized for a 8MB space
     for n in [100000, 225000, 250000, 300000, 380000,
               460000, 570000, 800000]:
         os.write(2, 'case %d\n' % n)
         rgc.collect()
         h(n)
     return -42
Пример #10
0
 def g():
     n = state.counter
     if n > 0:
         for i in range(5):
             state.a = A(n)
         state.a = None
     rgc.collect()
     return n
Пример #11
0
 def f():
     lst = lltype.malloc(A, 16)  # 16 > 10
     rgc.collect()
     sub(lst)
     null = lltype.nullptr(S)
     lst[15] = null  # clear, so that A() is only visible via lst[0]
     rgc.collect()  # -> crash
     return lst[0].x
Пример #12
0
 def g():
     n = state.counter
     if n > 0:
         for i in range(5):
             state.a = A(n)
         state.a = None
     rgc.collect()
     return n
Пример #13
0
 def f():
     # numbers optimized for a 8MB space
     for n in [100000, 225000, 250000, 300000, 380000,
               460000, 570000, 800000]:
         os.write(2, 'case %d\n' % n)
         rgc.collect()
         h(n)
     return -42
Пример #14
0
 def f():
     lst = lltype.malloc(A, 16)   # 16 > 10
     rgc.collect()
     sub(lst)
     null = lltype.nullptr(S)
     lst[15] = null     # clear, so that A() is only visible via lst[0]
     rgc.collect()      # -> crash
     return lst[0].x
Пример #15
0
 def f():
     del state.freed[:]
     d = add_me()
     rgc.collect()
     # we want the dictionary to be really empty here.  It's hard to
     # ensure in the current implementation after just one collect(),
     # but at least two collects should be enough.
     rgc.collect()
     return len(state.freed)
Пример #16
0
 def fn(n):
     rgc.collect() # check that a prebuilt tagged pointer doesn't explode
     if n > 0:
         x = BoxedObject(n)
     else:
         x = UnboxedObject(n)
     u.x = x # invoke write barrier
     rgc.collect()
     return x.meth(100)
Пример #17
0
 def f():
     A()
     B()
     C()
     A()
     B()
     C()
     rgc.collect()
     return s.a_dels * 10 + s.b_dels
Пример #18
0
 def func():
     #try:
     from pypy.rlib import rgc
     a = rgc.malloc_nonmovable(TP, 3)
     rgc.collect()
     if a:
         assert not rgc.can_move(a)
         return 0
     return 1
Пример #19
0
 def f():
     A()
     B()
     C()
     A()
     B()
     C()
     rgc.collect()
     return s.a_dels * 10 + s.b_dels
 def func():
     #try:
     from pypy.rlib import rgc
     a = rgc.malloc_nonmovable(TP, 3)
     rgc.collect()
     if a:
         assert not rgc.can_move(a)
         return 0
     return 1
Пример #21
0
 def main(n):
     states = f(n)
     rgc.collect()
     rgc.collect()
     err = 1001
     for state in states:
         if state.num != 1001:
             err = state.num
             print 'ERROR:', err
     return err
Пример #22
0
 def main(n):
     states = f(n)
     rgc.collect()
     rgc.collect()
     err = 1001
     for state in states:
         if state.num != 1001:
             err = state.num
             print 'ERROR:', err
     return err
Пример #23
0
 def func():
     try:
         a = rgc.malloc_nonmovable(TP)
         rgc.collect()
         if a:
             assert not rgc.can_move(a)
             return 0
         return 1
     except Exception, e:
         return 2
Пример #24
0
 def fn():
     result = 0
     for i in range(2):
         a = refs[i]()
         rgc.collect()
         if a is None:
             result += (i+1)
         else:
             result += a.hello * (i+1)
     return result
Пример #25
0
 def fn():
     l = lltype.malloc(TP, 100)
     for i in range(100):
         l[i] = lltype.malloc(TP.OF.TO, i)
     l2 = lltype.malloc(TP, 50)
     rgc.ll_arraycopy(l, l2, 40, 0, 50)
     rgc.collect()
     for i in range(50):
         assert l2[i] == l[40 + i]
     return 0
Пример #26
0
 def fn():
     result = 0
     for i in range(2):
         a = refs[i]()
         rgc.collect()
         if a is None:
             result += (i + 1)
         else:
             result += a.hello * (i + 1)
     return result
Пример #27
0
 def fn():
     l = lltype.malloc(TP, 100)
     for i in range(100):
         l[i] = lltype.malloc(TP.OF.TO, i)
     l2 = lltype.malloc(TP, 50)
     rgc.ll_arraycopy(l, l2, 40, 0, 50)
     rgc.collect()
     for i in range(50):
         assert l2[i] == l[40 + i]
     return 0
Пример #28
0
 def fn():
     s = StringBuilder(4)
     s.append("abcd")
     s.append("defg")
     s.append("rty")
     s.append_multiple_char('y', 1000)
     rgc.collect()
     s.append_multiple_char('y', 1000)
     res = s.build()[1000]
     rgc.collect()
     return ord(res)
Пример #29
0
 def f(n, m):
     ptr = lltype.malloc(STR, n)
     ptr.hash = 0x62
     ptr.chars[0] = 'A'
     ptr.chars[1] = 'B'
     ptr.chars[2] = 'C'
     ptr2 = rgc.ll_shrink_array(ptr, 2)
     assert (ptr == ptr2) == GC_CAN_SHRINK_ARRAY
     rgc.collect()
     return (ord(ptr2.chars[0]) + (ord(ptr2.chars[1]) << 8) +
             (len(ptr2.chars) << 16) + (ptr2.hash << 24))
Пример #30
0
 def fn():
     s = StringBuilder(4)
     s.append("abcd")
     s.append("defg")
     s.append("rty")
     s.append_multiple_char('y', 1000)
     rgc.collect()
     s.append_multiple_char('y', 1000)
     res = s.build()[1000]
     rgc.collect()
     return ord(res)
Пример #31
0
 def fn(n):
     if n:
         r = r1
     else:
         r = r2
     a = r()
     rgc.collect()
     if a is None:
         return -5
     else:
         return a.hello
Пример #32
0
 def fn(n):
     if n:
         r = r1
     else:
         r = r2
     a = r()
     rgc.collect()
     if a is None:
         return -5
     else:
         return a.hello
Пример #33
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)
Пример #34
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)
Пример #35
0
 def f(n, m, gc_can_shrink_array):
     ptr = lltype.malloc(STR, n)
     ptr.hash = 0x62
     ptr.chars[0] = 'A'
     ptr.chars[1] = 'B'
     ptr.chars[2] = 'C'
     ptr2 = rgc.ll_shrink_array(ptr, 2)
     assert (ptr == ptr2) == gc_can_shrink_array
     rgc.collect()
     return ( ord(ptr2.chars[0])       +
             (ord(ptr2.chars[1]) << 8) +
             (len(ptr2.chars)   << 16) +
             (ptr2.hash         << 24))
Пример #36
0
def collect(space):
    "Run a full collection."
    # First clear the method cache.  See test_gc for an example of why.
    if space.config.objspace.std.withmethodcache:
        from pypy.objspace.std.typeobject import MethodCache
        cache = space.fromcache(MethodCache)
        cache.clear()
        if space.config.objspace.std.withmapdict:
            from pypy.objspace.std.mapdict import IndexCache
            cache = space.fromcache(IndexCache)
            cache.clear()
    rgc.collect()
    return space.wrap(0)
Пример #37
0
def collect(space):
    "Run a full collection."
    # First clear the method cache.  See test_gc for an example of why.
    if space.config.objspace.std.withmethodcache:
        from pypy.objspace.std.typeobject import MethodCache
        cache = space.fromcache(MethodCache)
        cache.clear()
        if space.config.objspace.std.withmapdict:
            from pypy.objspace.std.mapdict import IndexCache
            cache = space.fromcache(IndexCache)
            cache.clear()
    rgc.collect()
    return space.wrap(0)
Пример #38
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
Пример #39
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
Пример #40
0
 def fn(n):
     id_prebuilt1 = compute_unique_id(u.x)
     if n > 0:
         x = BoxedObject(n)
     else:
         x = UnboxedObject(n)
     id_x1 = compute_unique_id(x)
     rgc.collect() # check that a prebuilt tagged pointer doesn't explode
     id_prebuilt2 = compute_unique_id(u.x)
     id_x2 = compute_unique_id(x)
     print u.x, id_prebuilt1, id_prebuilt2
     print x, id_x1, id_x2
     return ((id_x1 == id_x2) * 1 +
             (id_prebuilt1 == id_prebuilt2) * 10 +
             (id_x1 != id_prebuilt1) * 100)
Пример #41
0
 def fn(n):
     id_prebuilt1 = compute_unique_id(u.x)
     if n > 0:
         x = BoxedObject(n)
     else:
         x = UnboxedObject(n)
     id_x1 = compute_unique_id(x)
     rgc.collect() # check that a prebuilt tagged pointer doesn't explode
     id_prebuilt2 = compute_unique_id(u.x)
     id_x2 = compute_unique_id(x)
     print u.x, id_prebuilt1, id_prebuilt2
     print x, id_x1, id_x2
     return ((id_x1 == id_x2) * 1 +
             (id_prebuilt1 == id_prebuilt2) * 10 +
             (id_x1 != id_prebuilt1) * 100)
Пример #42
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)
Пример #43
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)
Пример #44
0
 def define_prebuilt_weakref(cls):
     import weakref
     class A:
         pass
     a = A()
     a.hello = 42
     refs = [weakref.ref(a), weakref.ref(A())]
     rgc.collect()
     def fn():
         result = 0
         for i in range(2):
             a = refs[i]()
             rgc.collect()
             if a is None:
                 result += (i+1)
             else:
                 result += a.hello * (i+1)
         return result
     return fn
Пример #45
0
 def h(n):
     x3 = g(3)
     x4 = g(3)
     x1 = g(n)
     build(x1, n)       # can collect!
     check(x1, n, 1)
     build(x3, 3)
     x2 = g(n//2)       # allocate more and try again
     build(x2, n//2)
     check(x1, n, 11)
     check(x2, n//2, 12)
     build(x4, 3)
     check(x3, 3, 13)   # check these old objects too
     check(x4, 3, 14)   # check these old objects too
     rgc.collect()
     check(x1, n, 21)
     check(x2, n//2, 22)
     check(x3, 3, 23)
     check(x4, 3, 24)
Пример #46
0
 def h(n):
     x3 = g(3)
     x4 = g(3)
     x1 = g(n)
     build(x1, n)       # can collect!
     check(x1, n, 1)
     build(x3, 3)
     x2 = g(n//2)       # allocate more and try again
     build(x2, n//2)
     check(x1, n, 11)
     check(x2, n//2, 12)
     build(x4, 3)
     check(x3, 3, 13)   # check these old objects too
     check(x4, 3, 14)   # check these old objects too
     rgc.collect()
     check(x1, n, 21)
     check(x2, n//2, 22)
     check(x3, 3, 23)
     check(x4, 3, 24)
Пример #47
0
 def define_prebuilt_weakref(cls):
     import weakref
     class A:
         pass
     a = A()
     a.hello = 42
     refs = [weakref.ref(a), weakref.ref(A())]
     rgc.collect()
     def fn():
         result = 0
         for i in range(2):
             a = refs[i]()
             rgc.collect()
             if a is None:
                 result += (i+1)
             else:
                 result += a.hello * (i+1)
         return result
     return fn
Пример #48
0
    def test_multiple_prebuilt_dead_weakrefs(self):
        class A:
            pass

        a1 = A()
        w1 = weakref.ref(a1)
        a2 = A()
        w2 = weakref.ref(a2)
        a3 = A()
        w3 = weakref.ref(a3)
        a4 = A()
        w4 = weakref.ref(a4)

        del a1, a3
        rgc.collect()
        assert w1() is None
        assert w3() is None

        def f(n):
            if n > 0:
                if n > 5:
                    r = w1
                else:
                    r = w3
                assert r() is None
            else:
                if n < -5:
                    r = w2
                else:
                    r = w4
                assert r() is not None
            return r() is not None

        res = self.interpret(f, [1])
        assert res == False
        res = self.interpret(f, [0])
        assert res == True
        res = self.interpret(f, [100])
        assert res == False
        res = self.interpret(f, [-100])
        assert res == True
Пример #49
0
 def f():
     d = RWeakValueDictionary(keyclass, X)
     x1, x3 = g(d)
     rgc.collect()
     rgc.collect()
     assert d.get(keys[0]) is x1
     assert d.get(keys[1]) is None
     assert d.get(keys[2]) is x3
     assert d.get(keys[3]) is None
     d.set(keys[0], None)
     assert d.get(keys[0]) is None
     assert d.get(keys[1]) is None
     assert d.get(keys[2]) is x3
     assert d.get(keys[3]) is None
     # resizing should also work
     for i in range(loop):
         d.set(make_key(i), x1)
     for i in range(loop):
         assert d.get(make_key(i)) is x1
     assert d.get(keys[0]) is None
     assert d.get(keys[1]) is None
     assert d.get(keys[2]) is x3
     assert d.get(keys[3]) is None
     # a subclass
     y = Y()
     d.set(keys[3], y)
     assert d.get(keys[3]) is y
     # storing a lot of Nones
     for i in range(loop, loop * 2 - 5):
         d.set(make_key(1000 + i), x1)
     for i in range(loop):
         d.set(make_key(i), None)
     for i in range(loop):
         assert d.get(make_key(i)) is None
     assert d.get(keys[0]) is None
     assert d.get(keys[1]) is None
     assert d.get(keys[2]) is x3
     assert d.get(keys[3]) is y
     for i in range(loop, loop * 2 - 5):
         assert d.get(make_key(1000 + i)) is x1
Пример #50
0
 def f():
     d = prebuilt
     if d is None:
         d = RWeakKeyDictionary(KX, VX)
     k1, k3, v1, v2, v3 = g(d)
     rgc.collect()
     rgc.collect()
     assert d.get(k1) is v1
     assert d.get(k3) is v3
     assert d.get(k1) is not v2
     assert d.get(k3) is not v2
     assert d.length() == 2
     d.set(k1, None)
     assert d.get(k1) is None
     assert d.get(k3) is v3
     assert d.length() == 1
     # resizing should also work
     lots_of_keys = [KX() for i in range(loop)]
     for k in lots_of_keys:
         d.set(k, v1)
     for k in lots_of_keys:
         assert d.get(k) is v1
     assert d.get(k1) is None
     assert d.get(k3) is v3
     assert d.length() == loop + 1
     # a subclass
     ky = KY()
     vy = VY()
     d.set(ky, vy)
     assert d.get(ky) is vy
     assert d.length() == loop + 2
     # deleting by storing Nones
     for k in lots_of_keys:
         d.set(k, None)
     for k in lots_of_keys:
         assert d.get(k) is None
     assert d.get(k1) is None
     assert d.get(k3) is v3
     assert d.get(ky) is vy
     assert d.length() == 2
Пример #51
0
    def test_multiple_prebuilt_dead_weakrefs(self):
        class A:
            pass
        a1 = A()
        w1 = weakref.ref(a1)
        a2 = A()
        w2 = weakref.ref(a2)
        a3 = A()
        w3 = weakref.ref(a3)
        a4 = A()
        w4 = weakref.ref(a4)

        del a1, a3
        rgc.collect()
        assert w1() is None
        assert w3() is None

        def f(n):
            if n > 0:
                if n > 5:
                    r = w1
                else:
                    r = w3
                assert r() is None
            else:
                if n < -5:
                    r = w2
                else:
                    r = w4
                assert r() is not None
            return r() is not None
        res = self.interpret(f, [1])
        assert res == False
        res = self.interpret(f, [0])
        assert res == True
        res = self.interpret(f, [100])
        assert res == False
        res = self.interpret(f, [-100])
        assert res == True
Пример #52
0
 def f():
     d = RWeakValueDictionary(X)
     x1, x3 = g(d)
     rgc.collect(); rgc.collect()
     assert d.get("abc") is x1
     assert d.get("def") is None
     assert d.get("ghi") is x3
     assert d.get("hello") is None
     d.set("abc", None)
     assert d.get("abc") is None
     assert d.get("def") is None
     assert d.get("ghi") is x3
     assert d.get("hello") is None
     # resizing should also work
     for i in range(loop):
         d.set(str(i), x1)
     for i in range(loop):
         assert d.get(str(i)) is x1
     assert d.get("abc") is None
     assert d.get("def") is None
     assert d.get("ghi") is x3
     assert d.get("hello") is None
     # a subclass
     y = Y()
     d.set("hello", y)
     assert d.get("hello") is y
     # storing a lot of Nones
     for i in range(loop, loop*2-5):
         d.set('%dfoobar' % i, x1)
     for i in range(loop):
         d.set(str(i), None)
     for i in range(loop):
         assert d.get(str(i)) is None
     assert d.get("abc") is None
     assert d.get("def") is None
     assert d.get("ghi") is x3
     assert d.get("hello") is y
     for i in range(loop, loop*2-5):
         assert d.get('%dfoobar' % i) is x1
Пример #53
0
 def f():
     d = prebuilt
     if d is None:
         d = RWeakKeyDictionary(KX, VX)
     k1, k3, v1, v2, v3 = g(d)
     rgc.collect(); rgc.collect()
     assert d.get(k1) is v1
     assert d.get(k3) is v3
     assert d.get(k1) is not v2
     assert d.get(k3) is not v2
     assert d.length() == 2
     d.set(k1, None)
     assert d.get(k1) is None
     assert d.get(k3) is v3
     assert d.length() == 1
     # resizing should also work
     lots_of_keys = [KX() for i in range(loop)]
     for k in lots_of_keys:
         d.set(k, v1)
     for k in lots_of_keys:
         assert d.get(k) is v1
     assert d.get(k1) is None
     assert d.get(k3) is v3
     assert d.length() == loop + 1
     # a subclass
     ky = KY()
     vy = VY()
     d.set(ky, vy)
     assert d.get(ky) is vy
     assert d.length() == loop + 2
     # deleting by storing Nones
     for k in lots_of_keys:
         d.set(k, None)
     for k in lots_of_keys:
         assert d.get(k) is None
     assert d.get(k1) is None
     assert d.get(k3) is v3
     assert d.get(ky) is vy
     assert d.length() == 2
Пример #54
0
 def test_prebuilt_weakref(self):
     import weakref
     from pypy.rlib import rgc
     class A:
         pass
     a = A()
     a.hello = 42
     refs = [weakref.ref(a), weakref.ref(A())]
     rgc.collect()
     def fn():
         result = 0
         for i in range(2):
             a = refs[i]()
             rgc.collect()
             if a is None:
                 result += (i+1)
             else:
                 result += a.hello * (i+1)
         return result
     c_fn = self.getcompiled(fn)
     res = c_fn()
     assert res == fn()