File: test_hpymodule.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (291 lines) | stat: -rw-r--r-- 10,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from .support import HPyTest

class TestModule(HPyTest):
    def test_HPyModule_simple(self):
        """
        The simplest fully declarative module creation.
        """
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_NOARGS)
            static HPy f_impl(HPyContext *ctx, HPy self)
            {
                return HPyLong_FromLong(ctx, 42);
            }

            static HPyDef *moduledefs[] = { &f, NULL };
            static HPyModuleDef moduledef = {
                .doc = "Some doc",
                .size = 0,
                .legacy_methods = NULL,
                .defines = moduledefs,
                .globals = NULL,
            };

            @HPy_MODINIT(moduledef)
        """)
        import sys
        ISPYPY = "__pypy__" in sys.modules
        if not ISPYPY:
            # see https://github.com/hpyproject/hpy/issues/456
            assert mod.__name__ == mod.__spec__.name
        assert mod.__doc__ == "Some doc"
        assert mod.f() == 42

    def test_HPyModule_custom_exec(self):
        """
        Module that defines several exec slots. HPy specifies that the slots
        will be executed in declaration order. Exec slots can add new types,
        and other objects into the module. They can also initialize other
        objects. The exec slots will be called on every new instance of the
        module, for example, when it is imported in several subinterpreters.
        """
        mod = self.make_module("""
            HPyDef_SLOT(exec1, HPy_mod_exec)
            static int exec1_impl(HPyContext *ctx, HPy mod)
            {
                HPy list = HPyList_New(ctx, 0);
                if (HPy_IsNull(list))
                    return -1;
                HPy_SetAttr_s(ctx, mod, "data", list);
                HPy_Close(ctx, list);
                return 0;
            }

            HPyDef_SLOT(exec2, HPy_mod_exec)
            static int exec2_impl(HPyContext *ctx, HPy mod)
            {
                HPy list = HPy_GetAttr_s(ctx, mod, "data");
                if (HPy_IsNull(list))
                    return -1;
                if (HPy_Length(ctx, list) != 0) {
                    HPyErr_SetString(ctx, ctx->h_RuntimeError, "Unexpected: len(list) != 0");
                    return -1;
                }
                HPy item = HPyLong_FromLong(ctx, 42);
                HPyList_Append(ctx, list, item);
                HPy_Close(ctx, item);
                HPy_Close(ctx, list);
                return 0;
            }

            static HPyDef *moduledefs[] = {
                &exec1,
                &exec2,
                NULL
            };

            static HPyModuleDef moduledef = {
                .doc = "Some doc",
                .size = 0,
                .legacy_methods = NULL,
                .defines = moduledefs,
                .globals = NULL,
            };

            @HPy_MODINIT(moduledef)
        """)
        assert mod.data == [42]

    def test_HPyModule_custom_create_returns_non_module(self):
        """
        Module that defines create slot that returns non module object. This
        is, for the time being, the only supported way to implement the module
        'create' slot. HPy intentionally does not expose direct API to create
        a module object. Module objects are created for the extension by the
        runtime and the extension can only populate that module object in the
        init slots.
        """
        import types
        mod = self.make_module("""
            HPyDef_SLOT(create, HPy_mod_create)
            static HPy create_impl(HPyContext *ctx, HPy spec)
            {
                HPy result = HPy_NULL, dict = HPy_NULL, ns_type = HPy_NULL;

                HPy types = HPyImport_ImportModule(ctx, "types");
                if (HPy_IsNull(types))
                    return HPy_NULL;

                ns_type = HPy_GetAttr_s(ctx, types, "SimpleNamespace");
                if (HPy_IsNull(types))
                    goto cleanup;
                dict = HPyDict_New(ctx);
                HPy_SetItem_s(ctx, dict, "spec", spec);
                result = HPy_CallTupleDict(ctx, ns_type, HPy_NULL, dict);
                if (HPy_IsNull(result))
                    goto cleanup;

            cleanup:
                HPy_Close(ctx, dict);
                HPy_Close(ctx, types);
                HPy_Close(ctx, ns_type);
                return result;
            }

            static HPyDef *moduledefs[] = {
                &create,
                NULL
            };

            static HPyModuleDef moduledef = {
                .doc = NULL,
                .size = 0,
                .legacy_methods = NULL,
                .defines = moduledefs,
                .globals = NULL,
            };

            @HPy_MODINIT(moduledef)
        """)
        assert isinstance(mod, types.SimpleNamespace)
        assert mod.spec is mod.__spec__

    def test_HPyModule_error_when_create_returns_module(self):
        """
        The HPy_mod_create slot cannot return a builtin module object.
        HPy does not expose any API to create builtin module objects and, until
        there are any actual use-cases, the purpose of the 'create' slot is to
        create non-builtin-module objects.
        """
        import pytest
        expected_message = "HPy_mod_create slot returned a builtin module " \
                           "object. This is currently not supported."
        with pytest.raises(SystemError, match=expected_message):
            self.make_module("""
                HPyDef_SLOT(create, HPy_mod_create)
                static HPy create_impl(HPyContext *ctx, HPy spec)
                {
                    return HPyImport_ImportModule(ctx, "types");
                }

                static HPyDef *moduledefs[] = {
                    &create,
                    NULL
                };

                static HPyModuleDef moduledef = {
                    .doc = NULL,
                    .size = 0,
                    .legacy_methods = NULL,
                    .defines = moduledefs,
                    .globals = NULL,
                };

                @HPy_MODINIT(moduledef)
            """)

    def test_HPyModule_create_raises(self):
        import pytest
        with pytest.raises(RuntimeError, match="Test error"):
            self.make_module("""
                HPyDef_SLOT(create, HPy_mod_create)
                static HPy create_impl(HPyContext *ctx, HPy spec)
                {
                    HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error");
                    return HPy_NULL;
                }

                static HPyDef *moduledefs[] = {
                    &create,
                    NULL
                };

                static HPyModuleDef moduledef = {
                    .doc = NULL,
                    .size = 0,
                    .legacy_methods = NULL,
                    .defines = moduledefs,
                    .globals = NULL,
                };

                @HPy_MODINIT(moduledef)
            """)

    def test_HPyModule_create_and_nondefault_values(self):
        import pytest
        expected_message = r'^HPyModuleDef defines a HPy_mod_create slot.*'
        with pytest.raises(SystemError, match=expected_message):
            self.make_module("""
                HPyDef_SLOT(create, HPy_mod_create)
                static HPy create_impl(HPyContext *ctx, HPy spec)
                {
                    HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error");
                    return HPy_NULL;
                }

                static HPyDef *moduledefs[] = {
                    &create,
                    NULL
                };

                static HPyModuleDef moduledef = {
                    .doc = "Some doc - this is non-default",
                    .size = 0,
                    .legacy_methods = NULL,
                    .defines = moduledefs,
                    .globals = NULL,
                };

                @HPy_MODINIT(moduledef)
            """)

    def test_HPyModule_create_and_exec_slots(self):
        import pytest
        expected_message = r'^HPyModuleDef defines a HPy_mod_create slot.*'
        with pytest.raises(SystemError, match=expected_message):
            self.make_module("""
                HPyDef_SLOT(create, HPy_mod_create)
                static HPy create_impl(HPyContext *ctx, HPy spec)
                {
                    HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error");
                    return HPy_NULL;
                }

                HPyDef_SLOT(exec, HPy_mod_exec)
                static int exec_impl(HPyContext *ctx, HPy mod)
                {
                    return 0;
                }

                static HPyDef *moduledefs[] = {
                    &create,
                    &exec,
                    NULL
                };

                static HPyModuleDef moduledef = {
                    .doc = NULL,
                    .size = 0,
                    .legacy_methods = NULL,
                    .defines = moduledefs,
                    .globals = NULL,
                };

                @HPy_MODINIT(moduledef)
            """)

    def test_HPyModule_negative_size(self):
        """
        The simplest fully declarative module creation.
        """
        import pytest
        expected_message = "HPy does not permit HPyModuleDef.size < 0"
        with pytest.raises(SystemError, match=expected_message):
            self.make_module("""
                HPyDef_METH(f, "f", HPyFunc_NOARGS)
                static HPy f_impl(HPyContext *ctx, HPy self)
                {
                    return HPyLong_FromLong(ctx, 42);
                }

                static HPyDef *moduledefs[] = { &f, NULL };
                static HPyModuleDef moduledef = {
                    .doc = "Some doc",
                    .size = -1,
                    .legacy_methods = NULL,
                    .defines = moduledefs,
                    .globals = NULL,
                };

                @HPy_MODINIT(moduledef)
            """)