Debian Codegraph @

rpython/rtyper/test/test_normalizecalls.py

raw symbols SCIP
1 import py
2 from rpython.annotator import model as annmodel
3 from rpython.translator.translator import TranslationContext, graphof
4 from rpython.rtyper.llinterp import LLInterpreter
5 from rpython.rtyper.error import TyperError
6 from rpython.rtyper.test.test_llinterp import interpret
7 from rpython.rtyper.lltypesystem import lltype
8 from rpython.rtyper.normalizecalls import TotalOrderSymbolic, MAX
9 from rpython.rtyper.normalizecalls import TooLateForNewSubclass
10
11
12 def test_TotalOrderSymbolic():
13 lst = []
14 t1 = TotalOrderSymbolic([3, 4], lst)
15 t2 = TotalOrderSymbolic([3, 4, 2], lst)
16 t3 = TotalOrderSymbolic([3, 4, 2, MAX], lst)
17 t4 = TotalOrderSymbolic([3, 4, MAX], lst)
18 assert t1 < t2 < t3 < t4
19 assert t1.value is t2.value is t3.value is t4.value is None
20 assert 1 <= t3
21 assert t3.value == 2
22 assert t1 <= 5
23 assert t1.value == 0
24
25 def test_TotalOrderSymbolic_with_subclasses():
26 lst = []
27 t3 = TotalOrderSymbolic([3, 4, 2, MAX], lst)
28 t1 = TotalOrderSymbolic([3, 4], lst)
29 t2 = TotalOrderSymbolic([3, 4, 2], lst)
30 t4 = TotalOrderSymbolic([3, 4, MAX], lst)
31 assert t1.number_with_subclasses()
32 assert not t2.number_with_subclasses()
33 assert [t.compute_fn() for t in [t1, t2, t3, t4]] == range(4)
34 #
35 lst = []
36 t1 = TotalOrderSymbolic([3, 4], lst)
37 t3 = TotalOrderSymbolic([3, 4, 2, MAX], lst)
38 t4 = TotalOrderSymbolic([3, 4, MAX], lst)
39 t2 = TotalOrderSymbolic([3, 4, 2], lst)
40 assert not t2.number_with_subclasses()
41 assert t1.number_with_subclasses()
42 assert [t.compute_fn() for t in [t1, t2, t3, t4]] == range(4)
43 #
44 lst = []
45 t1 = TotalOrderSymbolic([3, 4], lst)
46 t4 = TotalOrderSymbolic([3, 4, MAX], lst)
47 assert not t1.number_with_subclasses()
48 t2 = TotalOrderSymbolic([3, 4, 2], lst)
49 t3 = TotalOrderSymbolic([3, 4, 2, MAX], lst)
50 py.test.raises(TooLateForNewSubclass, t2.compute_fn)
51 #
52 lst = []
53 t1 = TotalOrderSymbolic([3, 4], lst)
54 t4 = TotalOrderSymbolic([3, 4, MAX], lst)
55 assert not t1.number_with_subclasses()
56 t2 = TotalOrderSymbolic([1], lst)
57 t3 = TotalOrderSymbolic([1, MAX], lst)
58 assert [t.compute_fn() for t in [t2, t3, t1, t4]] == range(4)
59 #
60 lst = []
61 t1 = TotalOrderSymbolic([3, 4], lst)
62 t4 = TotalOrderSymbolic([3, 4, MAX], lst)
63 assert not t1.number_with_subclasses()
64 t2 = TotalOrderSymbolic([6], lst)
65 t3 = TotalOrderSymbolic([6, MAX], lst)
66 assert [t.compute_fn() for t in [t1, t4, t2, t3]] == range(4)
67
68 # ____________________________________________________________
69
70 class TestNormalize(object):
71
72 def rtype(self, fn, argtypes, resulttype):
73 t = TranslationContext()
74 a = t.buildannotator()
75 s = a.build_types(fn, argtypes)
76 assert s == a.typeannotation(resulttype)
77 typer = t.buildrtyper()
78 typer.specialize()
79 #t.view()
80 t.checkgraphs()
81 return t
82
83
84 def test_normalize_f2_as_taking_string_argument(self):
85 def f1(l1):
86 pass
87 def f2(l2):
88 pass
89 def g(n):
90 if n > 0:
91 f1("123")
92 f = f1
93 else:
94 f2("b")
95 f = f2
96 f("a")
97
98 # The call table looks like:
99 #
100 # FuncDesc(f1) FuncDesc(f2)
101 # --------------------------------------------
102 # line g+2: graph1
103 # line g+5: graph2
104 # line g+7: graph1 graph2
105 #
106 # But all lines get compressed to a single line.
107
108 translator = self.rtype(g, [int], annmodel.s_None)
109 f1graph = graphof(translator, f1)
110 f2graph = graphof(translator, f2)
111 s_l1 = translator.annotator.binding(f1graph.getargs()[0])
112 s_l2 = translator.annotator.binding(f2graph.getargs()[0])
113 assert s_l1.__class__ == annmodel.SomeString # and not SomeChar
114 assert s_l2.__class__ == annmodel.SomeString # and not SomeChar
115 #translator.view()
116
117 def test_normalize_keyword_call(self):
118 def f1(a, b):
119 return (a, b, 0, 0)
120 def f2(b, c=123, a=456, d=789):
121 return (a, b, c, d)
122 def g(n):
123 if n > 0:
124 f = f1
125 else:
126 f = f2
127 f(a=5, b=6)
128
129 translator = self.rtype(g, [int], annmodel.s_None)
130 f1graph = graphof(translator, f1)
131 f2graph = graphof(translator, f2)
132 assert len(f1graph.getargs()) == 2
133 assert len(f2graph.getargs()) == 2 # normalized to the common call pattern
134 #translator.view()
135
136 def test_normalize_returnvar(self):
137 def add_one(n):
138 return n+1
139 def add_half(n):
140 return n+0.5
141 def dummyfn(n, i):
142 if i == 1:
143 adder = add_one
144 else:
145 adder = add_half
146 return adder(n)
147
148 res = interpret(dummyfn, [52, 1])
149 assert type(res) is float and res == 53.0
150 res = interpret(dummyfn, [7, 2])
151 assert type(res) is float and res == 7.5
152
153 def test_normalize_missing_return(self):
154 def add_one(n):
155 return n+1
156 def oups(n):
157 raise ValueError
158 def dummyfn(n, i):
159 if i == 1:
160 adder = add_one
161 else:
162 adder = oups
163 try:
164 return adder(n)
165 except ValueError:
166 return -1
167
168 translator = self.rtype(dummyfn, [int, int], int)
169 add_one_graph = graphof(translator, add_one)
170 oups_graph = graphof(translator, oups)
171 assert add_one_graph.getreturnvar().concretetype == lltype.Signed
172 assert oups_graph .getreturnvar().concretetype == lltype.Signed
173 #translator.view()
174
175 def test_normalize_abstract_method(self):
176 class Base:
177 def fn(self):
178 raise NotImplementedError
179 class Sub1(Base):
180 def fn(self):
181 return 1
182 class Sub2(Base):
183 def fn(self):
184 return -2
185 def dummyfn(n):
186 if n == 1:
187 x = Sub1()
188 else:
189 x = Sub2()
190 return x.fn()
191
192 translator = self.rtype(dummyfn, [int], int)
193 base_graph = graphof(translator, Base.fn.im_func)
194 sub1_graph = graphof(translator, Sub1.fn.im_func)
195 sub2_graph = graphof(translator, Sub2.fn.im_func)
196 assert base_graph.getreturnvar().concretetype == lltype.Signed
197 assert sub1_graph.getreturnvar().concretetype == lltype.Signed
198 assert sub2_graph.getreturnvar().concretetype == lltype.Signed
199
200 llinterp = LLInterpreter(translator.rtyper)
201 res = llinterp.eval_graph(graphof(translator, dummyfn), [1])
202 assert res == 1
203 res = llinterp.eval_graph(graphof(translator, dummyfn), [2])
204 assert res == -2
205
206 def test_methods_with_defaults(self):
207 class Base:
208 def fn(self):
209 raise NotImplementedError
210 class Sub1(Base):
211 def fn(self, x=1):
212 return 1 + x
213 class Sub2(Base):
214 def fn(self):
215 return -2
216 def otherfunc(x):
217 return x.fn()
218 def dummyfn(n):
219 if n == 1:
220 x = Sub1()
221 n = x.fn(2)
222 else:
223 x = Sub2()
224 return otherfunc(x) + x.fn()
225
226 excinfo = py.test.raises(TyperError, "self.rtype(dummyfn, [int], int)")
227 msg = """the following functions:
228 .+Base.fn
229 .+Sub1.fn
230 .+Sub2.fn
231 are called with inconsistent numbers of arguments
232 \(and/or the argument names are different, which is not supported in this case\)
233 sometimes with \d arguments, sometimes with \d
234 the callers of these functions are:
235 .+otherfunc
236 .+dummyfn"""
237 import re
238 assert re.match(msg, excinfo.value.args[0])
239
240
241 class PBase:
242 def fn(self):
243 raise NotImplementedError
244 class PSub1(PBase):
245 def fn(self):
246 return 1
247 class PSub2(PBase):
248 def fn(self):
249 return 2
250 def prefn(n):
251 if n == 1:
252 x = PSub1()
253 else:
254 x = PSub2()
255 return x.fn() * 100 + isinstance(x, PSub2)
256
257
258 class TestNormalizeAfterTheFact(TestNormalize):
259
260 def rtype(self, fn, argtypes, resulttype, checkfunction=None):
261 t = TranslationContext()
262 a = t.buildannotator()
263 a.build_types(prefn, [int])
264 typer = t.buildrtyper()
265 typer.specialize()
266 #t.view()
267
268 s_result = a.typeannotation(resulttype)
269
270 from rpython.rtyper import annlowlevel
271 # annotate, normalize and rtype fn after the fact
272 annhelper = annlowlevel.MixLevelHelperAnnotator(typer)
273 graph = annhelper.getgraph(fn, [a.typeannotation(argtype) for argtype in argtypes],
274 s_result)
275 annhelper.finish()
276 t.checkgraphs()
277
278 if checkfunction is not None:
279 checkfunction(t)
280
281 # sanity check prefn
282 llinterp = LLInterpreter(typer)
283 res = llinterp.eval_graph(graphof(t, prefn), [1])
284 assert res == 100
285 res = llinterp.eval_graph(graphof(t, prefn), [2])
286 assert res == 201
287
288 return t
289
290 def test_mix_after_recursion(self):
291 def prefn(n):
292 if n:
293 return 2*prefn(n-1)
294 else:
295 return 1
296
297 t = TranslationContext()
298 a = t.buildannotator()
299 a.build_types(prefn, [int])
300 typer = t.buildrtyper()
301 typer.specialize()
302 #t.view()
303
304 def f():
305 return 1
306
307 from rpython.rtyper import annlowlevel
308 annhelper = annlowlevel.MixLevelHelperAnnotator(typer)
309 graph = annhelper.getgraph(f, [], annmodel.SomeInteger())
310 annhelper.finish()
311
312 def test_add_more_subclasses(self):
313 from rpython.rtyper import rclass
314 from rpython.rtyper.rclass import ll_issubclass, CLASSTYPE
315 class Sub3(PBase):
316 def newmethod(self):
317 return 3
318 def dummyfn(n):
319 x = Sub3()
320 return x.newmethod()
321
322 def checkfunction(translator):
323 # make sure that there is a sensible comparison defined on the
324 # symbolics
325 bk = translator.annotator.bookkeeper
326 rtyper = translator.rtyper
327 base_classdef = bk.getuniqueclassdef(PBase)
328 base_vtable = rclass.getclassrepr(rtyper, base_classdef).getruntime(CLASSTYPE)
329 sub3_classdef = bk.getuniqueclassdef(Sub3)
330 sub3_vtable = rclass.getclassrepr(rtyper, sub3_classdef).getruntime(CLASSTYPE)
331 assert ll_issubclass(sub3_vtable, base_vtable)
332 assert not ll_issubclass(base_vtable, sub3_vtable)
333
334 translator = self.rtype(dummyfn, [int], int, checkfunction)
335 base_graph = graphof(translator, PBase.fn.im_func)
336 sub1_graph = graphof(translator, PSub1.fn.im_func)
337 sub2_graph = graphof(translator, PSub2.fn.im_func)
338 sub3_graph = graphof(translator, Sub3.fn.im_func)
339 dummyfn_graph = graphof(translator, dummyfn)
340 assert base_graph.getreturnvar().concretetype == lltype.Signed
341 assert sub1_graph.getreturnvar().concretetype == lltype.Signed
342 assert sub2_graph.getreturnvar().concretetype == lltype.Signed
343 assert sub3_graph.getreturnvar().concretetype == lltype.Signed
344 assert dummyfn_graph.getreturnvar().concretetype == lltype.Signed
345
346 def test_call_memoized_function_with_defaults(self):
347 class Freezing:
348 def _freeze_(self):
349 return True
350 fr1 = Freezing(); fr1.x = 1
351 fr2 = Freezing(); fr2.x = 2
352 def getorbuild(key1, key2=fr2, flag3=True):
353 return key1.x * 100 + key2.x * 10 + flag3
354 getorbuild._annspecialcase_ = "specialize:memo"
355
356 def f1(i):
357 if i > 0:
358 fr = fr1
359 else:
360 fr = fr2
361 if i % 2:
362 return getorbuild(fr)
363 else:
364 return getorbuild(fr, fr2, False)
365
366 for i in [-7, -2, 100, 5]:
367 res = interpret(f1, [i])
368 assert res == f1(i)