Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/test_hpymodule.py

raw symbols SCIP
1 from .support import HPyTest
2
3 class TestModule(HPyTest):
4 def test_HPyModule_simple(self):
5 """
6 The simplest fully declarative module creation.
7 """
8 mod = self.make_module("""
9 HPyDef_METH(f, "f", HPyFunc_NOARGS)
10 static HPy f_impl(HPyContext *ctx, HPy self)
11 {
12 return HPyLong_FromLong(ctx, 42);
13 }
14
15 static HPyDef *moduledefs[] = { &f, NULL };
16 static HPyModuleDef moduledef = {
17 .doc = "Some doc",
18 .size = 0,
19 .legacy_methods = NULL,
20 .defines = moduledefs,
21 .globals = NULL,
22 };
23
24 @HPy_MODINIT(moduledef)
25 """)
26 import sys
27 ISPYPY = "__pypy__" in sys.modules
28 if not ISPYPY:
29 # see https://github.com/hpyproject/hpy/issues/456
30 assert mod.__name__ == mod.__spec__.name
31 assert mod.__doc__ == "Some doc"
32 assert mod.f() == 42
33
34 def test_HPyModule_custom_exec(self):
35 """
36 Module that defines several exec slots. HPy specifies that the slots
37 will be executed in declaration order. Exec slots can add new types,
38 and other objects into the module. They can also initialize other
39 objects. The exec slots will be called on every new instance of the
40 module, for example, when it is imported in several subinterpreters.
41 """
42 mod = self.make_module("""
43 HPyDef_SLOT(exec1, HPy_mod_exec)
44 static int exec1_impl(HPyContext *ctx, HPy mod)
45 {
46 HPy list = HPyList_New(ctx, 0);
47 if (HPy_IsNull(list))
48 return -1;
49 HPy_SetAttr_s(ctx, mod, "data", list);
50 HPy_Close(ctx, list);
51 return 0;
52 }
53
54 HPyDef_SLOT(exec2, HPy_mod_exec)
55 static int exec2_impl(HPyContext *ctx, HPy mod)
56 {
57 HPy list = HPy_GetAttr_s(ctx, mod, "data");
58 if (HPy_IsNull(list))
59 return -1;
60 if (HPy_Length(ctx, list) != 0) {
61 HPyErr_SetString(ctx, ctx->h_RuntimeError, "Unexpected: len(list) != 0");
62 return -1;
63 }
64 HPy item = HPyLong_FromLong(ctx, 42);
65 HPyList_Append(ctx, list, item);
66 HPy_Close(ctx, item);
67 HPy_Close(ctx, list);
68 return 0;
69 }
70
71 static HPyDef *moduledefs[] = {
72 &exec1,
73 &exec2,
74 NULL
75 };
76
77 static HPyModuleDef moduledef = {
78 .doc = "Some doc",
79 .size = 0,
80 .legacy_methods = NULL,
81 .defines = moduledefs,
82 .globals = NULL,
83 };
84
85 @HPy_MODINIT(moduledef)
86 """)
87 assert mod.data == [42]
88
89 def test_HPyModule_custom_create_returns_non_module(self):
90 """
91 Module that defines create slot that returns non module object. This
92 is, for the time being, the only supported way to implement the module
93 'create' slot. HPy intentionally does not expose direct API to create
94 a module object. Module objects are created for the extension by the
95 runtime and the extension can only populate that module object in the
96 init slots.
97 """
98 import types
99 mod = self.make_module("""
100 HPyDef_SLOT(create, HPy_mod_create)
101 static HPy create_impl(HPyContext *ctx, HPy spec)
102 {
103 HPy result = HPy_NULL, dict = HPy_NULL, ns_type = HPy_NULL;
104
105 HPy types = HPyImport_ImportModule(ctx, "types");
106 if (HPy_IsNull(types))
107 return HPy_NULL;
108
109 ns_type = HPy_GetAttr_s(ctx, types, "SimpleNamespace");
110 if (HPy_IsNull(types))
111 goto cleanup;
112 dict = HPyDict_New(ctx);
113 HPy_SetItem_s(ctx, dict, "spec", spec);
114 result = HPy_CallTupleDict(ctx, ns_type, HPy_NULL, dict);
115 if (HPy_IsNull(result))
116 goto cleanup;
117
118 cleanup:
119 HPy_Close(ctx, dict);
120 HPy_Close(ctx, types);
121 HPy_Close(ctx, ns_type);
122 return result;
123 }
124
125 static HPyDef *moduledefs[] = {
126 &create,
127 NULL
128 };
129
130 static HPyModuleDef moduledef = {
131 .doc = NULL,
132 .size = 0,
133 .legacy_methods = NULL,
134 .defines = moduledefs,
135 .globals = NULL,
136 };
137
138 @HPy_MODINIT(moduledef)
139 """)
140 assert isinstance(mod, types.SimpleNamespace)
141 assert mod.spec is mod.__spec__
142
143 def test_HPyModule_error_when_create_returns_module(self):
144 """
145 The HPy_mod_create slot cannot return a builtin module object.
146 HPy does not expose any API to create builtin module objects and, until
147 there are any actual use-cases, the purpose of the 'create' slot is to
148 create non-builtin-module objects.
149 """
150 import pytest
151 expected_message = "HPy_mod_create slot returned a builtin module " \
152 "object. This is currently not supported."
153 with pytest.raises(SystemError, match=expected_message):
154 self.make_module("""
155 HPyDef_SLOT(create, HPy_mod_create)
156 static HPy create_impl(HPyContext *ctx, HPy spec)
157 {
158 return HPyImport_ImportModule(ctx, "types");
159 }
160
161 static HPyDef *moduledefs[] = {
162 &create,
163 NULL
164 };
165
166 static HPyModuleDef moduledef = {
167 .doc = NULL,
168 .size = 0,
169 .legacy_methods = NULL,
170 .defines = moduledefs,
171 .globals = NULL,
172 };
173
174 @HPy_MODINIT(moduledef)
175 """)
176
177 def test_HPyModule_create_raises(self):
178 import pytest
179 with pytest.raises(RuntimeError, match="Test error"):
180 self.make_module("""
181 HPyDef_SLOT(create, HPy_mod_create)
182 static HPy create_impl(HPyContext *ctx, HPy spec)
183 {
184 HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error");
185 return HPy_NULL;
186 }
187
188 static HPyDef *moduledefs[] = {
189 &create,
190 NULL
191 };
192
193 static HPyModuleDef moduledef = {
194 .doc = NULL,
195 .size = 0,
196 .legacy_methods = NULL,
197 .defines = moduledefs,
198 .globals = NULL,
199 };
200
201 @HPy_MODINIT(moduledef)
202 """)
203
204 def test_HPyModule_create_and_nondefault_values(self):
205 import pytest
206 expected_message = r'^HPyModuleDef defines a HPy_mod_create slot.*'
207 with pytest.raises(SystemError, match=expected_message):
208 self.make_module("""
209 HPyDef_SLOT(create, HPy_mod_create)
210 static HPy create_impl(HPyContext *ctx, HPy spec)
211 {
212 HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error");
213 return HPy_NULL;
214 }
215
216 static HPyDef *moduledefs[] = {
217 &create,
218 NULL
219 };
220
221 static HPyModuleDef moduledef = {
222 .doc = "Some doc - this is non-default",
223 .size = 0,
224 .legacy_methods = NULL,
225 .defines = moduledefs,
226 .globals = NULL,
227 };
228
229 @HPy_MODINIT(moduledef)
230 """)
231
232 def test_HPyModule_create_and_exec_slots(self):
233 import pytest
234 expected_message = r'^HPyModuleDef defines a HPy_mod_create slot.*'
235 with pytest.raises(SystemError, match=expected_message):
236 self.make_module("""
237 HPyDef_SLOT(create, HPy_mod_create)
238 static HPy create_impl(HPyContext *ctx, HPy spec)
239 {
240 HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error");
241 return HPy_NULL;
242 }
243
244 HPyDef_SLOT(exec, HPy_mod_exec)
245 static int exec_impl(HPyContext *ctx, HPy mod)
246 {
247 return 0;
248 }
249
250 static HPyDef *moduledefs[] = {
251 &create,
252 &exec,
253 NULL
254 };
255
256 static HPyModuleDef moduledef = {
257 .doc = NULL,
258 .size = 0,
259 .legacy_methods = NULL,
260 .defines = moduledefs,
261 .globals = NULL,
262 };
263
264 @HPy_MODINIT(moduledef)
265 """)
266
267 def test_HPyModule_negative_size(self):
268 """
269 The simplest fully declarative module creation.
270 """
271 import pytest
272 expected_message = "HPy does not permit HPyModuleDef.size < 0"
273 with pytest.raises(SystemError, match=expected_message):
274 self.make_module("""
275 HPyDef_METH(f, "f", HPyFunc_NOARGS)
276 static HPy f_impl(HPyContext *ctx, HPy self)
277 {
278 return HPyLong_FromLong(ctx, 42);
279 }
280
281 static HPyDef *moduledefs[] = { &f, NULL };
282 static HPyModuleDef moduledef = {
283 .doc = "Some doc",
284 .size = -1,
285 .legacy_methods = NULL,
286 .defines = moduledefs,
287 .globals = NULL,
288 };
289
290 @HPy_MODINIT(moduledef)
291 """)