File: test_refcount.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 (158 lines) | stat: -rw-r--r-- 4,072 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import py

from rpython.rtyper.lltypesystem import lltype
from rpython.translator.translator import TranslationContext
from rpython.translator.c.test.test_genc import compile


class TestRefcount(object):
    def compile_func(self, func, args):
        return compile(func, args, gcpolicy='ref')

    def test_something(self):
        def f():
            return 1
        fn = self.compile_func(f, [])
        assert fn() == 1

    def test_something_more(self):
        S = lltype.GcStruct("S", ('x', lltype.Signed))
        def f(x):
            s = lltype.malloc(S)
            s.x = x
            return s.x
        fn = self.compile_func(f, [int])
        assert fn(1) == 1

    def test_call_function(self):
        class C:
            pass
        def f():
            c = C()
            c.x = 1
            return c
        def g():
            return f().x
        fn = self.compile_func(g, [])
        assert fn() == 1

    def test_multiple_exits(self):
        S = lltype.GcStruct("S", ('x', lltype.Signed))
        T = lltype.GcStruct("T", ('y', lltype.Signed))
        def f(n):
            c = lltype.malloc(S)
            d = lltype.malloc(T)
            d.y = 1
            e = lltype.malloc(T)
            e.y = 2
            if n:
                x = d
            else:
                x = e
            return x.y
        fn = self.compile_func(f, [int])
        assert fn(1) == 1
        assert fn(0) == 2


    def test_cleanup_vars_on_call(self):
        S = lltype.GcStruct("S", ('x', lltype.Signed))
        def f():
            return lltype.malloc(S)
        def g():
            s1 = f()
            s1.x = 42
            s2 = f()
            s3 = f()
            return s1.x
        fn = self.compile_func(g, [])
        assert fn() == 42

    def test_multiply_passed_var(self):
        S = lltype.GcStruct("S", ('x', lltype.Signed))
        def f(x):
            if x:
                a = lltype.malloc(S)
                a.x = 1
                b = a
            else:
                a = lltype.malloc(S)
                a.x = 1
                b = lltype.malloc(S)
                b.x = 2
            return a.x + b.x
        fn = self.compile_func(f, [int])
        fn(1) == 2
        fn(0) == 3

    def test_write_barrier(self):
        S = lltype.GcStruct("S", ('x', lltype.Signed))
        T = lltype.GcStruct("T", ('s', lltype.Ptr(S)))
        def f(x):
            s = lltype.malloc(S)
            s.x = 0
            s1 = lltype.malloc(S)
            s1.x = 1
            s2 = lltype.malloc(S)
            s2.x = 2
            t = lltype.malloc(T)
            t.s = s
            if x:
                t.s = s1
            else:
                t.s = s2
            return t.s.x + s.x + s1.x + s2.x
        fn = self.compile_func(f, [int])
        assert fn(1) == 4
        assert fn(0) == 5

    def test_del_catches(self):
        import os
        def g():
            pass
        class A(object):
            def __del__(self):
                try:
                    g()
                except:
                    pass  #os.write(1, "hallo")
        def f1(i):
            if i:
                raise TypeError
        def f(i):
            a = A()
            f1(i)
            a.b = 1
            return a.b
        fn = self.compile_func(f, [int])
        assert fn(0) == 1
        fn(1, expected_exception_name="TypeError")

    def test_del_raises(self):
        class B(object):
            def __del__(self):
                raise TypeError
        def func():
            b = B()
        fn = self.compile_func(func, [])
        # does not crash
        fn()

    def test_wrong_order_setitem(self):
        class A(object):
            pass
        a = A()
        a.b = None
        class B(object):
            def __del__(self):
                a.freed += 1
                a.b = None
        def f(n):
            a.freed = 0
            a.b = B()
            if n:
                a.b = None
            return a.freed
        fn = self.compile_func(f, [int])
        res = fn(1)
        assert res == 1