Debian Codegraph @

rpython/rtyper/test/test_nongc.py

raw symbols SCIP
1 import py
2
3 from rpython.annotator import model as annmodel
4 from rpython.rtyper.llannotation import SomeAddress
5 from rpython.annotator.annrpython import RPythonAnnotator
6 from rpython.rtyper.rtyper import RPythonTyper
7 from rpython.rlib.objectmodel import free_non_gc_object
8 from rpython.rtyper.test.test_llinterp import interpret as llinterpret
9
10 def interpret(f, args):
11 return llinterpret(f, args, malloc_check=False)
12
13 def test_free_non_gc_object():
14 class TestClass(object):
15 _alloc_flavor_ = ""
16 def __init__(self, a):
17 self.a = a
18 def method1(self):
19 return self.a
20 def method2(self):
21 return 42
22 class TestClass2(object):
23 pass
24 t = TestClass(1)
25 assert t.method1() == 1
26 assert t.method2() == 42
27 free_non_gc_object(t)
28 py.test.raises(RuntimeError, "t.method1()")
29 py.test.raises(RuntimeError, "t.method2()")
30 py.test.raises(RuntimeError, "t.a")
31 py.test.raises(RuntimeError, "t.a = 1")
32 py.test.raises(AssertionError, "free_non_gc_object(TestClass2())")
33
34 def test_alloc_flavor():
35 class A:
36 _alloc_flavor_ = "raw"
37 def f():
38 return A()
39 a = RPythonAnnotator()
40 #does not raise:
41 s = a.build_types(f, [])
42 Adef = a.bookkeeper.getuniqueclassdef(A)
43 assert s.classdef == Adef
44 rtyper = RPythonTyper(a)
45 rtyper.specialize()
46 assert (Adef, 'raw') in rtyper.instance_reprs
47 assert (Adef, 'gc') not in rtyper.instance_reprs
48
49 def test_alloc_flavor_subclassing():
50 class A:
51 _alloc_flavor_ = "raw"
52 class B(A):
53 def __init__(self, a):
54 self.a = a
55 def f():
56 return B(0)
57 a = RPythonAnnotator()
58 #does not raise:
59 s = a.build_types(f, [])
60 Adef = a.bookkeeper.getuniqueclassdef(A)
61 Bdef = a.bookkeeper.getuniqueclassdef(B)
62 assert s.classdef == Bdef
63 rtyper = RPythonTyper(a)
64 rtyper.specialize()
65 assert (Adef, 'raw') in rtyper.instance_reprs
66 assert (Adef, 'gc') not in rtyper.instance_reprs
67 assert (Bdef, 'raw') in rtyper.instance_reprs
68 assert (Bdef, 'gc') not in rtyper.instance_reprs
69
70 def test_unsupported():
71 class A:
72 _alloc_flavor_ = "raw"
73 def f():
74 return str(A())
75 a = RPythonAnnotator()
76 #does not raise:
77 s = a.build_types(f, [])
78 assert s.knowntype == str
79 rtyper = RPythonTyper(a)
80 py.test.raises(TypeError,rtyper.specialize) # results in an invalid cast
81
82 def test_isinstance():
83 class A(object):
84 _alloc_flavor_ = "raw"
85 class B(A):
86 pass
87 class C(B):
88 pass
89
90 def f(i):
91 if i == 0:
92 o = None
93 elif i == 1:
94 o = A()
95 elif i == 2:
96 o = B()
97 else:
98 o = C()
99 res = 100*isinstance(o, A) + 10*isinstance(o, B) + 1*isinstance(o, C)
100 if i == 0:
101 pass
102 elif i == 1:
103 assert isinstance(o, A)
104 free_non_gc_object(o)
105 elif i == 2:
106 assert isinstance(o, B)
107 free_non_gc_object(o)
108 else:
109 assert isinstance(o, C)
110 free_non_gc_object(o)
111 return res
112
113 assert f(1) == 100
114 assert f(2) == 110
115 assert f(3) == 111
116 assert f(0) == 0
117
118 a = RPythonAnnotator()
119 #does not raise:
120 s = a.build_types(f, [int])
121 assert s.knowntype == int
122 rtyper = RPythonTyper(a)
123 rtyper.specialize()
124 res = interpret(f, [1])
125 assert res == 100
126 res = interpret(f, [2])
127 assert res == 110
128 res = interpret(f, [3])
129 assert res == 111
130 res = interpret(f, [0])
131 assert res == 0
132
133 def test_is():
134 class A:
135 _alloc_flavor_ = "raw"
136 pass
137 class B(A): pass
138 class C:
139 _alloc_flavor_ = "raw"
140 def f(i):
141 a = A()
142 b = B()
143 c = C()
144 d = None
145 e = None
146 if i == 0:
147 d = a
148 elif i == 1:
149 d = b
150 elif i == 2:
151 e = c
152 res = (0x0001*(a is b) | 0x0002*(a is c) | 0x0004*(a is d) |
153 0x0008*(a is e) | 0x0010*(b is c) | 0x0020*(b is d) |
154 0x0040*(b is e) | 0x0080*(c is d) | 0x0100*(c is e) |
155 0x0200*(d is e))
156 free_non_gc_object(a)
157 free_non_gc_object(b)
158 free_non_gc_object(c)
159 return res
160 a = RPythonAnnotator()
161 #does not raise:
162 s = a.build_types(f, [int])
163 assert s.knowntype == int
164 rtyper = RPythonTyper(a)
165 rtyper.specialize()
166 res = interpret(f, [0])
167 assert res == 0x0004
168 res = interpret(f, [1])
169 assert res == 0x0020
170 res = interpret(f, [2])
171 assert res == 0x0100
172 res = interpret(f, [3])
173 assert res == 0x0200
174
175 def test_is_mixing():
176 class A:
177 _alloc_flavor_ = "raw"
178 pass
179 class B(A): pass
180 class C:
181 pass
182 def f(i):
183 a = A()
184 b = B()
185 c = C()
186 d = None
187 e = None
188 if i == 0:
189 d = a
190 elif i == 1:
191 d = b
192 elif i == 2:
193 e = c
194 res = (0x0001*(a is b) | 0x0002*(a is c) | 0x0004*(a is d) |
195 0x0008*(a is e) | 0x0010*(b is c) | 0x0020*(b is d) |
196 0x0040*(b is e) | 0x0080*(c is d) | 0x0100*(c is e) |
197 0x0200*(d is e))
198 free_non_gc_object(a)
199 free_non_gc_object(b)
200 return res
201 a = RPythonAnnotator()
202 #does not raise:
203 s = a.build_types(f, [int])
204 assert s.knowntype == int
205 rtyper = RPythonTyper(a)
206 rtyper.specialize()
207 res = interpret(f, [0])
208 assert res == 0x0004
209 res = interpret(f, [1])
210 assert res == 0x0020
211 res = interpret(f, [2])
212 assert res == 0x0100
213 res = interpret(f, [3])
214 assert res == 0x0200
215
216 def test_rtype_nongc_object():
217 class TestClass(object):
218 _alloc_flavor_ = "raw"
219 def __init__(self, a):
220 self.a = a
221 def method1(self):
222 return self.a
223 def malloc_and_free(a):
224 ci = TestClass(a)
225 b = ci.method1()
226 free_non_gc_object(ci)
227 return b
228 a = RPythonAnnotator()
229 #does not raise:
230 s = a.build_types(malloc_and_free, [SomeAddress()])
231 assert isinstance(s, SomeAddress)
232 rtyper = RPythonTyper(a)
233 rtyper.specialize()