Debian Codegraph @

extra_tests/hpy_tests/_vendored/test_hpybuildvalue.py

raw symbols SCIP
1 from .support import HPyTest
2
3
4 class TestBuildValue(HPyTest):
5
6 def make_tests_module(self, test_cases):
7 # Creates a module with function "f", that takes index of a test case
8 # to execute. Argument test_cases should be a tuple with first item
9 # being C code of the test case.
10 # Generates, e.g.: case 0: return HPy_BuildValue(...);
11 test_cases_c_code = ["case {}: {}; break;".format(i, case[0]) for i, case in enumerate(test_cases)]
12 return self.make_module("""
13 #include <limits.h>
14
15 HPyDef_METH(f, "f", HPyFunc_O)
16 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
17 {{
18 switch (HPyLong_AsLong(ctx, arg)) {{
19 {test_cases}
20 default:
21 HPyErr_SetString(ctx, ctx->h_ValueError, "Wrong test case number");
22 return HPy_NULL;
23 }}
24 }}
25
26 @EXPORT(f)
27 @INIT
28 """.format(test_cases='\n'.join(test_cases_c_code)))
29
30 def test_formats(self):
31 test_cases = [
32 ('return HPy_BuildValue(ctx, "");', None),
33 ('return HPy_BuildValue(ctx, "i", 42);', 42),
34 ('return HPy_BuildValue(ctx, "i", 0);', 0),
35 ('return HPy_BuildValue(ctx, "i", -1);', -1),
36 ('return HPy_BuildValue(ctx, "I", 33);', 33),
37 ('return HPy_BuildValue(ctx, "k", 1);', 1),
38 ('return HPy_BuildValue(ctx, "K", 6543);', 6543),
39 ('return HPy_BuildValue(ctx, "n", 9876);', 9876),
40 ('return HPy_BuildValue(ctx, "l", 345L);', 345),
41 ('return HPy_BuildValue(ctx, "l", -876L);', -876),
42 ('return HPy_BuildValue(ctx, "L", 545LL);', 545),
43 ('return HPy_BuildValue(ctx, "L", -344LL);', -344),
44 ('return HPy_BuildValue(ctx, "f", 0.25f);', 0.25),
45 ('return HPy_BuildValue(ctx, "d", 0.25);', 0.25),
46 ('return HPy_BuildValue(ctx, "ii", -1, 1);', (-1, 1)),
47 ('return HPy_BuildValue(ctx, "(i)", -1);', (-1,)),
48 ('return HPy_BuildValue(ctx, "(i,i)", -1, 1);', (-1, 1)),
49 ('return HPy_BuildValue(ctx, "(ii)", -1, 1);', (-1, 1)),
50 ('return HPy_BuildValue(ctx, "s", "test string");', 'test string'),
51 ('return HPy_BuildValue(ctx, "[ii]", 4, 2);', [4, 2]),
52 ('return HPy_BuildValue(ctx, "[i,i]", 4, 2);', [4, 2]),
53 ('return HPy_BuildValue(ctx, "[is]", 4, "2");', [4, '2']),
54 ('return HPy_BuildValue(ctx, "[]");', []),
55 ('return HPy_BuildValue(ctx, "[(is)((f)[kk])i]", 4, "str", 0.25, 4, 2, 14267);',
56 [(4, 'str'), ((0.25,), [4, 2]), 14267]),
57 ('return HPy_BuildValue(ctx, "{s:i, s:f}", "A", 4, "B", 0.25);',
58 {'A':4, "B":0.25}),
59 ('return HPy_BuildValue(ctx, "{s:(i,i), s:f}", "A", 4, 4, "B", 0.25);',
60 {'A':(4, 4), "B":0.25}),
61 ('return HPy_BuildValue(ctx, "[{s:(i,i), s:f},i]", "A", 4, 4, "B", 0.25, 42);',
62 [{'A':(4, 4), "B":0.25}, 42]),
63 ('return HPy_BuildValue(ctx, "({s:(i,i), s:f},[i])", "A", 4, 4, "B", 0.25, 42);',
64 ({'A':(4, 4), "B":0.25}, [42])),
65 ]
66 mod = self.make_tests_module(test_cases)
67 for i, (code, expected) in enumerate(test_cases):
68 actual = mod.f(i)
69 assert actual == expected, code
70
71 def test_bad_formats(self):
72 test_cases = [
73 ('return HPy_BuildValue(ctx, "(q)", 42);',
74 "bad format char 'q' in the format string passed to HPy_BuildValue"),
75 ('return HPy_BuildValue(ctx, "(i", 42);',
76 "unmatched '(' in the format string passed to HPy_BuildValue"),
77 ('return HPy_BuildValue(ctx, "[i", 42);',
78 "unmatched '[' in the format string passed to HPy_BuildValue"),
79 ('return HPy_BuildValue(ctx, "([(i)k", 42);',
80 "unmatched '(' in the format string passed to HPy_BuildValue"),
81 ('return HPy_BuildValue(ctx, "(i]", 42);',
82 "unmatched '(' in the format string passed to HPy_BuildValue"),
83 ('return HPy_BuildValue(ctx, "[i)", 42);',
84 "unmatched '[' in the format string passed to HPy_BuildValue"),
85 ('return HPy_BuildValue(ctx, "N", 42);',
86 "HPy_BuildValue does not support the 'N' formatting unit."),
87 ('return HPy_BuildValue(ctx, "{i:i", "foo", 42);',
88 "unmatched '{' in the format string passed to HPy_BuildValue"),
89 ('return HPy_BuildValue(ctx, "{i:i,,}", "foo", 42);',
90 "unexpected ',' in the format string passed to HPy_BuildValue"),
91 ('return HPy_BuildValue(ctx, "{i:ii,}", "foo", 42, 42);',
92 "missing ',' in the format string passed to HPy_BuildValue"),
93 ('return HPy_BuildValue(ctx, "{i}", 42);',
94 "missing ':' in the format string passed to HPy_BuildValue"),
95 ]
96 import pytest
97 mod = self.make_tests_module(test_cases)
98 for i, (code, expected_error) in enumerate(test_cases):
99 with pytest.raises(SystemError) as e:
100 mod.f(i)
101 assert expected_error in str(e), code
102
103 def test_O_and_aliases(self):
104 mod = self.make_module("""
105 HPyDef_METH(fo, "fo", HPyFunc_O)
106 static HPy fo_impl(HPyContext *ctx, HPy self, HPy arg)
107 {
108 return HPy_BuildValue(ctx, "O", arg);
109 }
110
111 HPyDef_METH(fs, "fs", HPyFunc_O)
112 static HPy fs_impl(HPyContext *ctx, HPy self, HPy arg)
113 {
114 return HPy_BuildValue(ctx, "S", arg);
115 }
116
117 @EXPORT(fo)
118 @EXPORT(fs)
119 @INIT
120 """)
121
122 class Dummy:
123 pass
124 obj = Dummy()
125 assert mod.fo(obj) == obj
126 assert mod.fs(obj) == obj
127
128 def test_O_with_new_object(self):
129 # HPy_BuildValue does not steal the reference to the object passed as 'O',
130 # the caller still needs to close it, otherwise -> handle leak
131 mod = self.make_module("""
132 #include <stdio.h>
133 HPyDef_METH(f, "f", HPyFunc_O)
134 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
135 {
136 HPy o = HPyLong_FromLong(ctx, 42);
137 HPy result;
138 if (HPyLong_AsLong(ctx, arg)) {
139 result = HPy_BuildValue(ctx, "O", o);
140 } else {
141 result = HPy_BuildValue(ctx, "(dO)", 0.25, o);
142 }
143 HPy_Close(ctx, o);
144 return result;
145 }
146 @EXPORT(f)
147 @INIT
148 """)
149 assert mod.f(0) == (0.25, 42)
150 assert mod.f(1) == 42
151
152 def test_O_with_null(self):
153 import pytest
154 mod = self.make_module("""
155 HPyDef_METH(no_msg, "no_msg", HPyFunc_O)
156 static HPy no_msg_impl(HPyContext *ctx, HPy self, HPy arg)
157 {
158 if (HPyLong_AsLong(ctx, arg)) {
159 return HPy_BuildValue(ctx, "O", HPy_NULL);
160 } else {
161 return HPy_BuildValue(ctx, "(iO)", 42, HPy_NULL);
162 }
163 }
164
165 HPyDef_METH(with_msg, "with_msg", HPyFunc_O)
166 static HPy with_msg_impl(HPyContext *ctx, HPy self, HPy arg)
167 {
168 HPyErr_SetString(ctx, ctx->h_ValueError, "Some err msg that will be asserted");
169 return no_msg_impl(ctx, self, arg);
170 }
171
172 @EXPORT(with_msg)
173 @EXPORT(no_msg)
174 @INIT
175 """)
176 for i in [0, 1]:
177 with pytest.raises(ValueError) as e:
178 mod.with_msg(i)
179 assert "Some err msg that will be asserted" in str(e)
180 for i in [0, 1]:
181 with pytest.raises(SystemError) as e:
182 mod.no_msg(i)
183 assert 'HPy_NULL object passed to HPy_BuildValue' in str(e)
184
185
186 def test_OO_pars_with_new_objects(self):
187 mod = self.make_module("""
188 #include <stdio.h>
189 HPyDef_METH(f, "f", HPyFunc_O)
190 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
191 {
192 HPy o1 = HPyLong_FromLong(ctx, 1);
193 HPy o2 = HPyLong_FromLong(ctx, 2);
194 HPy result = HPy_BuildValue(ctx, "(OO)", o1, o2);
195 HPy_Close(ctx, o1);
196 HPy_Close(ctx, o2);
197 return result;
198 }
199 @EXPORT(f)
200 @INIT
201 """)
202 assert mod.f(None) == (1, 2)
203
204 def test_num_limits(self):
205 test_cases = [
206 ('return HPy_BuildValue(ctx, "(ii)", INT_MIN, INT_MAX);',),
207 ('return HPy_BuildValue(ctx, "(ll)", LONG_MIN, LONG_MAX);',),
208 ('return HPy_BuildValue(ctx, "(LL)", LLONG_MIN, LLONG_MAX);',),
209 ('return HPy_BuildValue(ctx, "(iI)", -1, UINT_MAX);',),
210 ('return HPy_BuildValue(ctx, "(ik)", -1, ULONG_MAX);',),
211 ('return HPy_BuildValue(ctx, "(iK)", -1, ULLONG_MAX);',),
212 ('return HPy_BuildValue(ctx, "(nn)", HPY_SSIZE_T_MIN, HPY_SSIZE_T_MAX);',),
213 ]
214 mod = self.make_tests_module(test_cases)
215 for i, (test,) in enumerate(test_cases):
216 result = mod.f(i)
217 assert result[0] < 0, test
218 assert result[1] > 0, test