Debian Codegraph @

rpython/rlib/test/test_signature.py

raw symbols SCIP
1 import py
2 from rpython.rlib.signature import signature, finishsigs, FieldSpec, ClassSpec
3 from rpython.rlib import types
4 from rpython.annotator import model
5 from rpython.rtyper.llannotation import SomePtr
6 from rpython.annotator.signature import SignatureError
7 from rpython.translator.translator import TranslationContext, graphof
8 from rpython.rtyper.lltypesystem import rstr
9 from rpython.rtyper.annlowlevel import LowLevelAnnotatorPolicy
10
11
12 def annotate_at(f, policy=None):
13 t = TranslationContext()
14 t.config.translation.check_str_without_nul = True
15 a = t.buildannotator(policy=policy)
16 a.annotate_helper(f, [model.s_ImpossibleValue]*f.__code__.co_argcount, policy=policy)
17 return a
18
19 def sigof(a, f):
20 # returns [param1, param2, ..., ret]
21 g = graphof(a.translator, f)
22 return [a.binding(v) for v in g.startblock.inputargs] + [a.binding(g.getreturnvar())]
23
24 def getsig(f, policy=None):
25 a = annotate_at(f, policy=policy)
26 return sigof(a, f)
27
28 def check_annotator_fails(caller):
29 exc = py.test.raises(model.AnnotatorError, annotate_at, caller).value
30 assert caller.__name__ in str(exc)
31
32
33 def test_bookkeeping():
34 @signature('x', 'y', returns='z')
35 def f(a, b):
36 return a + len(b)
37 f.foo = 'foo'
38 assert f._signature_ == (('x', 'y'), 'z')
39 assert f.__name__ == 'f'
40 assert f.foo == 'foo'
41 assert f(1, 'hello') == 6
42
43 def test_basic():
44 @signature(types.int(), types.str(), returns=types.char())
45 def f(a, b):
46 return b[a]
47 assert getsig(f) == [model.SomeInteger(), model.SomeString(), model.SomeChar()]
48
49 def test_arg_errors():
50 @signature(types.int(), types.str(), returns=types.int())
51 def f(a, b):
52 return a + len(b)
53 @check_annotator_fails
54 def ok_for_body(): # would give no error without signature
55 f(2.0, 'b')
56 @check_annotator_fails
57 def bad_for_body(): # would give error inside 'f' body, instead errors at call
58 f('a', 'b')
59
60 def test_return():
61 @signature(returns=types.str())
62 def f():
63 return 'a'
64 assert getsig(f) == [model.SomeString()]
65
66 @signature(types.str(), returns=types.str())
67 def f(x):
68 return x
69 def g():
70 return f('a')
71 a = annotate_at(g)
72 assert sigof(a, f) == [model.SomeString(), model.SomeString()]
73
74 def test_return_errors():
75 @check_annotator_fails
76 @signature(returns=types.int())
77 def int_not_char():
78 return 'a'
79
80 @check_annotator_fails
81 @signature(types.str(), returns=types.int())
82 def str_to_int(s):
83 return s
84
85 @signature(returns=types.str())
86 def str_not_None():
87 return None
88 @check_annotator_fails
89 def caller_of_str_not_None():
90 return str_not_None()
91
92 @py.test.mark.xfail
93 def test_return_errors_xfail():
94 @check_annotator_fails
95 @signature(returns=types.str())
96 def str_not_None():
97 return None
98
99
100 def test_none():
101 @signature(returns=types.none())
102 def f():
103 pass
104 assert getsig(f) == [model.s_None]
105
106 def test_float():
107 @signature(types.longfloat(), types.singlefloat(), returns=types.float())
108 def f(a, b):
109 return 3.0
110 assert getsig(f) == [model.SomeLongFloat(), model.SomeSingleFloat(), model.SomeFloat()]
111
112 def test_unicode():
113 @signature(types.unicode(), returns=types.int())
114 def f(u):
115 return len(u)
116 assert getsig(f) == [model.SomeUnicodeString(), model.SomeInteger()]
117
118 def test_str0():
119 @signature(types.unicode0(), returns=types.str0())
120 def f(u):
121 return 'str'
122 assert getsig(f) == [model.SomeUnicodeString(no_nul=True),
123 model.SomeString(no_nul=True)]
124
125 def test_ptr():
126 policy = LowLevelAnnotatorPolicy()
127 @signature(types.ptr(rstr.STR), returns=types.none())
128 def f(buf):
129 pass
130 argtype = getsig(f, policy=policy)[0]
131 assert isinstance(argtype, SomePtr)
132 assert argtype.ll_ptrtype.TO == rstr.STR
133
134 def g():
135 f(rstr.mallocstr(10))
136 getsig(g, policy=policy)
137
138
139 def test_list():
140 @signature(types.list(types.int()), returns=types.int())
141 def f(a):
142 return len(a)
143 argtype = getsig(f)[0]
144 assert isinstance(argtype, model.SomeList)
145 item = argtype.listdef.listitem
146 assert item.s_value == model.SomeInteger()
147 assert item.resized == True
148
149 @check_annotator_fails
150 def ok_for_body():
151 f(['a'])
152 @check_annotator_fails
153 def bad_for_body():
154 f('a')
155
156 @signature(returns=types.list(types.char()))
157 def ff():
158 return ['a']
159 @check_annotator_fails
160 def mutate_broader():
161 ff()[0] = 'abc'
162 @check_annotator_fails
163 def mutate_unrelated():
164 ff()[0] = 1
165 @check_annotator_fails
166 @signature(types.list(types.char()), returns=types.int())
167 def mutate_in_body(l):
168 l[0] = 'abc'
169 return len(l)
170
171 def can_append():
172 l = ff()
173 l.append('b')
174 getsig(can_append)
175
176 def test_array():
177 @signature(returns=types.array(types.int()))
178 def f():
179 return [1]
180 rettype = getsig(f)[0]
181 assert isinstance(rettype, model.SomeList)
182 item = rettype.listdef.listitem
183 assert item.s_value == model.SomeInteger()
184 assert item.resized == False
185
186 def try_append():
187 l = f()
188 l.append(2)
189 check_annotator_fails(try_append)
190
191 def test_dict():
192 @signature(returns=types.dict(types.str(), types.int()))
193 def f():
194 return {'a': 1, 'b': 2}
195 rettype = getsig(f)[0]
196 assert isinstance(rettype, model.SomeDict)
197 assert rettype.dictdef.dictkey.s_value == model.SomeString()
198 assert rettype.dictdef.dictvalue.s_value == model.SomeInteger()
199
200
201 def test_instance():
202 class C1(object):
203 pass
204 class C2(C1):
205 pass
206 class C3(C2):
207 pass
208 @signature(types.instance(C3), returns=types.instance(C2))
209 def f(x):
210 assert isinstance(x, C2)
211 return x
212 argtype, rettype = getsig(f)
213 assert isinstance(argtype, model.SomeInstance)
214 assert argtype.classdef.classdesc.pyobj == C3
215 assert isinstance(rettype, model.SomeInstance)
216 assert rettype.classdef.classdesc.pyobj == C2
217
218 @check_annotator_fails
219 def ok_for_body():
220 f(C2())
221 @check_annotator_fails
222 def bad_for_body():
223 f(C1())
224 @check_annotator_fails
225 def ok_for_body():
226 f(None)
227
228 def test_instance_or_none():
229 class C1(object):
230 pass
231 class C2(C1):
232 pass
233 class C3(C2):
234 pass
235 @signature(types.instance(C3, can_be_None=True), returns=types.instance(C2, can_be_None=True))
236 def f(x):
237 assert isinstance(x, C2) or x is None
238 return x
239 argtype, rettype = getsig(f)
240 assert isinstance(argtype, model.SomeInstance)
241 assert argtype.classdef.classdesc.pyobj == C3
242 assert argtype.can_be_None
243 assert isinstance(rettype, model.SomeInstance)
244 assert rettype.classdef.classdesc.pyobj == C2
245 assert rettype.can_be_None
246
247 @check_annotator_fails
248 def ok_for_body():
249 f(C2())
250 @check_annotator_fails
251 def bad_for_body():
252 f(C1())
253
254
255 def test_self():
256 @finishsigs
257 class C(object):
258 @signature(types.self(), types.self(), returns=types.none())
259 def f(self, other):
260 pass
261 class D1(C):
262 pass
263 class D2(C):
264 pass
265
266 def g():
267 D1().f(D2())
268 a = annotate_at(g)
269
270 argtype = sigof(a, C.__dict__['f'])[0]
271 assert isinstance(argtype, model.SomeInstance)
272 assert argtype.classdef.classdesc.pyobj == C
273
274 def test_self_error():
275 class C(object):
276 @signature(types.self(), returns=types.none())
277 def incomplete_sig_meth(self):
278 pass
279
280 exc = py.test.raises(SignatureError, annotate_at, C.incomplete_sig_meth).value
281 assert 'incomplete_sig_meth' in str(exc)
282 assert 'finishsigs' in str(exc)
283
284 def test_any_as_argument():
285 @signature(types.any(), types.int(), returns=types.float())
286 def f(x, y):
287 return x + y
288 @signature(types.int(), returns=types.float())
289 def g(x):
290 return f(x, x)
291 sig = getsig(g)
292 assert sig == [model.SomeInteger(), model.SomeFloat()]
293
294 @signature(types.float(), returns=types.float())
295 def g(x):
296 return f(x, 4)
297 sig = getsig(g)
298 assert sig == [model.SomeFloat(), model.SomeFloat()]
299
300 @signature(types.str(), returns=types.int())
301 def cannot_add_string(x):
302 return f(x, 2)
303 exc = py.test.raises(model.AnnotatorError, annotate_at, cannot_add_string).value
304 assert 'Blocked block' in str(exc)
305
306 def test_return_any():
307 @signature(types.int(), returns=types.any())
308 def f(x):
309 return x
310 sig = getsig(f)
311 assert sig == [model.SomeInteger(), model.SomeInteger()]
312
313 @signature(types.str(), returns=types.any())
314 def cannot_add_string(x):
315 return f(3) + x
316 exc = py.test.raises(model.AnnotatorError, annotate_at, cannot_add_string).value
317 assert 'Blocked block' in str(exc)
318 assert 'cannot_add_string' in str(exc)
319
320
321
322 @py.test.mark.xfail
323 def test_class_basic():
324 class C(object):
325 _fields_ = ClassSpec({'x': FieldSpec(types.int)})
326
327 def wrong_type():
328 c = C()
329 c.x = 'a'
330 check_annotator_fails(wrong_type)
331
332 def bad_field():
333 c = C()
334 c.y = 3
335 check_annotator_fails(bad_field)
336
337
338 @py.test.mark.xfail
339 def test_class_shorthand():
340 class C1(object):
341 _fields_ = {'x': FieldSpec(types.int)}
342 def wrong_type_1():
343 c = C1()
344 c.x = 'a'
345 check_annotator_fails(wrong_type_1)
346
347 class C2(object):
348 _fields_ = ClassSpec({'x': types.int})
349 def wrong_type_2():
350 c = C2()
351 c.x = 'a'
352 check_annotator_fails(wrong_type_1)
353
354
355 @py.test.mark.xfail
356 def test_class_inherit():
357 class C(object):
358 _fields_ = ClassSpec({'x': FieldSpec(types.int)})
359
360 class C1(object):
361 _fields_ = ClassSpec({'y': FieldSpec(types.int)})
362
363 class C2(object):
364 _fields_ = ClassSpec({'y': FieldSpec(types.int)}, inherit=True)
365
366 def no_inherit():
367 c = C1()
368 c.x = 3
369 check_annotator_fails(no_inherit)
370
371 def good():
372 c = C2()
373 c.x = 3
374 annotate_at(good)
375
376 def wrong_type():
377 c = C2()
378 c.x = 'a'
379 check_annotator_fails(wrong_type)