File: test_incminimark_gc.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (93 lines) | stat: -rw-r--r-- 2,810 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import py
from rpython.rtyper.lltypesystem import lltype
from rpython.rtyper.lltypesystem.lloperation import llop
from rpython.rlib import rgc

from rpython.memory.test import test_minimark_gc

class TestIncrementalMiniMarkGC(test_minimark_gc.TestMiniMarkGC):
    from rpython.memory.gc.incminimark import IncrementalMiniMarkGC as GCClass
    WREF_IS_INVALID_BEFORE_DEL_IS_CALLED = True

    def test_weakref_not_in_stack(self):
        import weakref
        class A(object):
            pass
        class B(object):
            def __init__(self, next):
                self.next = next
        def g():
            a = A()
            a.x = 5
            wr = weakref.ref(a)
            llop.gc__collect(lltype.Void)   # make everything old
            assert wr() is not None
            assert a.x == 5
            return wr
        def f():
            ref = g()
            llop.gc__collect(lltype.Void, 1)    # start a major cycle
            # at this point the stack is scanned, and the weakref points
            # to an object not found, but still reachable:
            b = ref()
            llop.debug_print(lltype.Void, b)
            assert b is not None
            llop.gc__collect(lltype.Void)   # finish the major cycle
            # assert does not crash, because 'b' is still kept alive
            b.x = 42
            return ref() is b
        res = self.interpret(f, [])
        assert res == True

    def test_pin_weakref_not_implemented(self):
        import weakref
        class A:
            pass
        def f():
            a = A()
            ref = weakref.ref(a)
            assert not rgc.pin(ref)
        self.interpret(f, [])

    def test_pin_finalizer_not_implemented(self):
        import weakref
        class A:
            def __del__(self):
                pass
        class B:
            def __del__(self):
                foo.bar += 1
        class Foo:
            bar = 0
        foo = Foo()
        def f():
            a = A()
            b = B()
            assert not rgc.pin(a)
            assert not rgc.pin(b)
        self.interpret(f, [])

    def test_weakref_to_pinned(self):
        py.test.skip("weakref to pinned object: not supported")
        import weakref
        from rpython.rlib import rgc
        class A(object):
            pass
        def g():
            a = A()
            assert rgc.pin(a)
            a.x = 100
            wr = weakref.ref(a)
            llop.gc__collect(lltype.Void)
            assert wr() is not None
            assert a.x == 100
            return wr
        def f():
            ref = g()
            llop.gc__collect(lltype.Void, 1)
            b = ref()
            assert b is not None
            b.x = 101
            return ref() is b
        res = self.interpret(f, [])
        assert res == True