File: test_helpers.py

package info (click to toggle)
pypy3 7.3.11%2Bdfsg-2%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 201,024 kB
  • sloc: python: 1,950,308; ansic: 517,580; sh: 21,417; asm: 14,419; cpp: 4,263; makefile: 4,228; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 11; awk: 4
file content (68 lines) | stat: -rw-r--r-- 2,221 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
"""
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", f_impl, 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", f_impl, 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