Debian Codegraph @

rpython/translator/c/test/test_refcount.py

raw symbols SCIP
1 import py
2
3 from rpython.rtyper.lltypesystem import lltype
4 from rpython.translator.translator import TranslationContext
5 from rpython.translator.c.test.test_genc import compile
6
7
8 class TestRefcount(object):
9 def compile_func(self, func, args):
10 return compile(func, args, gcpolicy='ref')
11
12 def test_something(self):
13 def f():
14 return 1
15 fn = self.compile_func(f, [])
16 assert fn() == 1
17
18 def test_something_more(self):
19 S = lltype.GcStruct("S", ('x', lltype.Signed))
20 def f(x):
21 s = lltype.malloc(S)
22 s.x = x
23 return s.x
24 fn = self.compile_func(f, [int])
25 assert fn(1) == 1
26
27 def test_call_function(self):
28 class C:
29 pass
30 def f():
31 c = C()
32 c.x = 1
33 return c
34 def g():
35 return f().x
36 fn = self.compile_func(g, [])
37 assert fn() == 1
38
39 def test_multiple_exits(self):
40 S = lltype.GcStruct("S", ('x', lltype.Signed))
41 T = lltype.GcStruct("T", ('y', lltype.Signed))
42 def f(n):
43 c = lltype.malloc(S)
44 d = lltype.malloc(T)
45 d.y = 1
46 e = lltype.malloc(T)
47 e.y = 2
48 if n:
49 x = d
50 else:
51 x = e
52 return x.y
53 fn = self.compile_func(f, [int])
54 assert fn(1) == 1
55 assert fn(0) == 2
56
57
58 def test_cleanup_vars_on_call(self):
59 S = lltype.GcStruct("S", ('x', lltype.Signed))
60 def f():
61 return lltype.malloc(S)
62 def g():
63 s1 = f()
64 s1.x = 42
65 s2 = f()
66 s3 = f()
67 return s1.x
68 fn = self.compile_func(g, [])
69 assert fn() == 42
70
71 def test_multiply_passed_var(self):
72 S = lltype.GcStruct("S", ('x', lltype.Signed))
73 def f(x):
74 if x:
75 a = lltype.malloc(S)
76 a.x = 1
77 b = a
78 else:
79 a = lltype.malloc(S)
80 a.x = 1
81 b = lltype.malloc(S)
82 b.x = 2
83 return a.x + b.x
84 fn = self.compile_func(f, [int])
85 fn(1) == 2
86 fn(0) == 3
87
88 def test_write_barrier(self):
89 S = lltype.GcStruct("S", ('x', lltype.Signed))
90 T = lltype.GcStruct("T", ('s', lltype.Ptr(S)))
91 def f(x):
92 s = lltype.malloc(S)
93 s.x = 0
94 s1 = lltype.malloc(S)
95 s1.x = 1
96 s2 = lltype.malloc(S)
97 s2.x = 2
98 t = lltype.malloc(T)
99 t.s = s
100 if x:
101 t.s = s1
102 else:
103 t.s = s2
104 return t.s.x + s.x + s1.x + s2.x
105 fn = self.compile_func(f, [int])
106 assert fn(1) == 4
107 assert fn(0) == 5
108
109 def test_del_catches(self):
110 import os
111 def g():
112 pass
113 class A(object):
114 def __del__(self):
115 try:
116 g()
117 except:
118 pass #os.write(1, "hallo")
119 def f1(i):
120 if i:
121 raise TypeError
122 def f(i):
123 a = A()
124 f1(i)
125 a.b = 1
126 return a.b
127 fn = self.compile_func(f, [int])
128 assert fn(0) == 1
129 fn(1, expected_exception_name="TypeError")
130
131 def test_del_raises(self):
132 class B(object):
133 def __del__(self):
134 raise TypeError
135 def func():
136 b = B()
137 fn = self.compile_func(func, [])
138 # does not crash
139 fn()
140
141 def test_wrong_order_setitem(self):
142 class A(object):
143 pass
144 a = A()
145 a.b = None
146 class B(object):
147 def __del__(self):
148 a.freed += 1
149 a.b = None
150 def f(n):
151 a.freed = 0
152 a.b = B()
153 if n:
154 a.b = None
155 return a.freed
156 fn = self.compile_func(f, [int])
157 res = fn(1)
158 assert res == 1