Debian Codegraph @

rpython/rtyper/test/test_extfunc.py

raw symbols SCIP
1 import py
2
3 from rpython.rtyper.extfunc import register_external
4 from rpython.annotator.model import SomeInteger, SomeString, AnnotatorError
5 from rpython.annotator.annrpython import RPythonAnnotator
6 from rpython.annotator.policy import AnnotatorPolicy
7 from rpython.annotator.signature import SignatureError
8 from rpython.rtyper.test.test_llinterp import interpret
9
10 class TestExtFuncEntry:
11
12 def test_basic(self):
13 """
14 A ExtFuncEntry provides an annotation for a function, no need to flow
15 its graph.
16 """
17 def b(x):
18 "NOT_RPYTHON"
19 return eval("x+40")
20
21 register_external(b, [int], result=int)
22
23 def f():
24 return b(2)
25
26 policy = AnnotatorPolicy()
27 a = RPythonAnnotator(policy=policy)
28 s = a.build_types(f, [])
29 assert isinstance(s, SomeInteger)
30
31 res = interpret(f, [])
32 assert res == 42
33
34 def test_lltypeimpl(self):
35 """
36 interpret() calls lltypeimpl instead of of the function/
37 """
38 def c(y, x):
39 yyy
40
41 def llimpl(y, x):
42 return y + x
43
44 register_external(c, [int, int], result=int, llimpl=llimpl,
45 export_name='ccc')
46
47 def f():
48 return c(3, 4)
49
50 res = interpret(f, [])
51 assert res == 7
52
53 def test_register_external_tuple_args(self):
54 """
55 Verify the annotation of a registered external function which takes a
56 tuple argument.
57 """
58
59 def function_with_tuple_arg():
60 """
61 Dummy function which is declared via register_external to take a
62 tuple as an argument so that register_external's behavior for
63 tuple-taking functions can be verified.
64 """
65 register_external(function_with_tuple_arg, [(int,)], int)
66
67 def f():
68 return function_with_tuple_arg((1,))
69
70 policy = AnnotatorPolicy()
71 a = RPythonAnnotator(policy=policy)
72 s = a.build_types(f, [])
73
74 # Not a very good assertion, but at least it means _something_ happened.
75 assert isinstance(s, SomeInteger)
76
77 def test_register_external_return_goes_back(self):
78 """
79 Check whether it works to pass the same list from one external
80 fun to another
81 [bookkeeper and list joining issues]
82 """
83 def function_with_list():
84 pass
85 register_external(function_with_list, [[int]], int)
86
87 def function_returning_list():
88 pass
89 register_external(function_returning_list, [], [int])
90
91 def f():
92 return function_with_list(function_returning_list())
93
94 policy = AnnotatorPolicy()
95 a = RPythonAnnotator(policy=policy)
96 s = a.build_types(f, [])
97 assert isinstance(s, SomeInteger)
98
99 def test_str0(self):
100 str0 = SomeString(no_nul=True)
101 def os_open(s):
102 pass
103 register_external(os_open, [str0], None)
104 def f(s):
105 return os_open(s)
106 policy = AnnotatorPolicy()
107 a = RPythonAnnotator(policy=policy)
108 a.build_types(f, [str]) # Does not raise
109 assert a.translator.config.translation.check_str_without_nul == False
110 # Now enable the str0 check, and try again with a similar function
111 a.translator.config.translation.check_str_without_nul=True
112 def g(s):
113 return os_open(s)
114 with py.test.raises(SignatureError):
115 a.build_types(g, [str])
116 a.build_types(g, [str0]) # Does not raise
117
118 def test_list_of_str0_unchecked(self):
119 str0 = SomeString(no_nul=True)
120
121 def os_execve(l):
122 pass
123
124 register_external(os_execve, [[str0]], None)
125
126 def f(l):
127 return os_execve(l)
128
129 policy = AnnotatorPolicy()
130 a = RPythonAnnotator(policy=policy)
131 assert a.translator.config.translation.check_str_without_nul == False
132 a.build_types(f, [[str]]) # Does not raise
133 # Now enable the str0 check, and try again with a similar function
134 a.translator.config.translation.check_str_without_nul=True
135
136 def g(l):
137 return os_execve(l)
138
139 with py.test.raises(AnnotatorError):
140 # fails with TooLateForChange
141 a.build_types(g, [[str]])
142 a.build_types(g, [[str0]]) # Does not raise
143
144 def test_register_external_llfakeimpl(self):
145 def a(i):
146 return i
147 def a_llimpl(i):
148 return i * 2
149 def a_llfakeimpl(i):
150 return i * 3
151 register_external(a, [int], int, llimpl=a_llimpl,
152 llfakeimpl=a_llfakeimpl)
153 def f(i):
154 return a(i)
155
156 res = interpret(f, [7])
157 assert res == 21
158
159 from rpython.translator.c.test.test_genc import compile
160 fc = compile(f, [int])
161 assert fc(7) == 14