File: test_helpers.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 (144 lines) | stat: -rw-r--r-- 5,258 bytes parent folder | download | duplicates (4)
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
"""
NOTE: this tests are also meant to be run as PyPy "applevel" tests.

This means that global imports will NOT be visible inside the test
functions. In particular, you have to "import pytest" inside the test in order
to be able to use e.g. pytest.raises (which on PyPy will be implemented by a
"fake pytest module")
"""
from .support import HPyTest


class TestHPyModuleAddType(HPyTest):
    def test_with_spec_only(self):
        mod = self.make_module("""
            static HPyType_Spec dummy_spec = {
                .name = "mytest.Dummy",
            };

            HPyDef_METH(f, "f", HPyFunc_NOARGS)
            static HPy f_impl(HPyContext *ctx, HPy self)
            {
                if (!HPyHelpers_AddType(ctx, self, "Dummy", &dummy_spec, NULL))
                {
                    return HPy_NULL;
                }
                return HPy_Dup(ctx, ctx->h_None);
            }

            @EXPORT(f)
            @INIT
        """)
        assert not hasattr(mod, "Dummy")
        mod.f()
        assert isinstance(mod.Dummy, type)
        assert mod.Dummy.__name__ == "Dummy"
        assert isinstance(mod.Dummy(), mod.Dummy)

    def test_with_spec_and_params(self):
        mod = self.make_module("""
            static HPyType_Spec dummy_spec = {
                .name = "mytest.Dummy",
            };

            HPyDef_METH(f, "f", HPyFunc_NOARGS)
            static HPy f_impl(HPyContext *ctx, HPy self)
            {
                HPyType_SpecParam param[] = {
                    { HPyType_SpecParam_Base, ctx->h_LongType },
                    { (HPyType_SpecParam_Kind)0 }
                };
                if (!HPyHelpers_AddType(ctx, self, "Dummy", &dummy_spec, param))
                {
                    return HPy_NULL;
                }
                return HPy_Dup(ctx, ctx->h_None);
            }

            @EXPORT(f)
            @INIT
        """)
        assert not hasattr(mod, "Dummy")
        mod.f()
        assert isinstance(mod.Dummy, type)
        assert mod.Dummy.__name__ == "Dummy"
        assert isinstance(mod.Dummy(), mod.Dummy)
        assert isinstance(mod.Dummy(), int)
        assert mod.Dummy() == 0
        assert mod.Dummy(3) == 3

    def test_pack_args_and_keywords(self):
        import pytest
        mod = self.make_module("""
            HPyDef_METH(pack, "pack", HPyFunc_KEYWORDS)
            static HPy pack_impl(HPyContext *ctx, HPy self, const HPy *args,
                                 size_t nargs, HPy kwnames)
            {
                HPy out[] = { HPy_NULL, HPy_NULL };
                HPy result;
                if (!HPyHelpers_PackArgsAndKeywords(ctx, args, nargs, kwnames,
                         &out[0], &out[1])) {
                    return HPy_NULL;
                }
                for (int i=0; i < 2; i++) {
                    if (HPy_IsNull(out[i])) {
                        out[i] = HPy_Dup(ctx, ctx->h_None);
                    }
                }
                result = HPyTuple_FromArray(ctx, out, 2);
                for (int i=0; i < 2; i++) {
                    HPy_Close(ctx, out[i]);
                }
                return result;
            }

            HPyDef_METH(pack_error, "pack_error", HPyFunc_O)
            static HPy pack_error_impl(HPyContext *ctx, HPy self, HPy arg)
            {
                int success;
                HPy t = HPy_NULL;
                HPy d = HPy_NULL;
                const HPy args[] = { ctx->h_None, ctx->h_True, ctx->h_False };
                size_t nargs = sizeof(args);
                uint64_t mode = HPyLong_AsUInt64_t(ctx, arg);
                switch (mode) {
                case 0:
                    success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs,
                                  HPy_NULL, NULL, &d);
                    break;
                case 1:
                    success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs,
                                  HPy_NULL, &t, NULL);
                    break;
                case 2:
                    success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs,
                                  HPy_NULL, NULL, NULL);
                    break;
                case 3:
                    success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs,
                                  ctx->h_None, &t, &d);
                    break;
                default:
                    success = 0;
                    HPyErr_SetString(ctx, ctx->h_ValueError,
                        "unknown test mode");
                    break;
                }
                if (success)
                    return HPy_Dup(ctx, ctx->h_None);
                return HPy_NULL;
            }

            @EXPORT(pack)
            @EXPORT(pack_error)
            @INIT
        """)
        assert mod.pack() == (None, None)
        assert mod.pack(1, '2', b'3') == ((1, '2', b'3'), None)
        assert mod.pack(1, '2', b'3', a='b', c='d') == ((1, '2', b'3'), dict(a='b', c='d'))
        assert mod.pack(a='b', c='d') == (None, dict(a='b', c='d'))
        for mode in range(3):
            with pytest.raises(SystemError):
                mod.pack_error(mode)
        with pytest.raises(TypeError):
            mod.pack_error(3)