Debian Codegraph @

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

raw symbols SCIP
1 """
2 NOTE: this tests are also meant to be run as PyPy "applevel" tests.
3
4 This means that global imports will NOT be visible inside the test
5 functions. In particular, you have to "import pytest" inside the test in order
6 to be able to use e.g. pytest.raises (which on PyPy will be implemented by a
7 "fake pytest module")
8 """
9 from .support import HPyTest
10
11
12 class TestHPyStructSequence(HPyTest):
13 def test_structseq(self):
14 import pytest
15 mod = self.make_module("""
16 static HPyStructSequence_Field structseq_fields[] = {
17 { "field0", "doc0" },
18 { "field1", NULL },
19 { NULL, NULL },
20 { NULL, NULL },
21 };
22
23 static HPyStructSequence_Field structseq_empty_fields[] = {
24 { NULL, NULL },
25 };
26
27 static HPyStructSequence_Desc withfields_desc = {
28 .name = "mytest.WithFields",
29 .doc = "some doc",
30 .fields = structseq_fields
31 };
32
33 static HPyStructSequence_Desc nofields_desc = {
34 .name = "mytest.NoFields",
35 .fields = structseq_empty_fields
36 };
37
38 #define N 3
39
40 HPyDef_METH(build, "build", HPyFunc_O)
41 static HPy build_impl(HPyContext *ctx, HPy self, HPy type)
42 {
43 int i;
44 HPy elements[N];
45 elements[0] = HPyLong_FromLong(ctx, 1);
46 elements[1] = HPyFloat_FromDouble(ctx, 2.0);
47 elements[2] = HPyUnicode_FromString(ctx, "3");
48 HPy structseq = HPyStructSequence_New(ctx, type, N, elements);
49 for (i = 0; i < N; i++)
50 HPy_Close(ctx, elements[i]);
51 if (HPy_IsNull(structseq))
52 return HPy_NULL;
53 return structseq;
54 }
55
56 static void make_types(HPyContext *ctx, HPy module)
57 {
58 // cannot be done in the static initializer
59 structseq_fields[2].name = HPyStructSequence_UnnamedField;
60
61 HPy h_withfields_type = HPyStructSequence_NewType(ctx, &withfields_desc);
62 if (HPy_IsNull(h_withfields_type))
63 return;
64 HPy_SetAttr_s(ctx, module, "WithFields", h_withfields_type);
65 HPy_Close(ctx, h_withfields_type);
66
67 HPy h_nofields_type = HPyStructSequence_NewType(ctx, &nofields_desc);
68 if (HPy_IsNull(h_nofields_type)) {
69 return;
70 }
71 HPy_SetAttr_s(ctx, module, "NoFields", h_nofields_type);
72 HPy_Close(ctx, h_nofields_type);
73 }
74
75 @EXPORT(build)
76 @EXTRA_INIT_FUNC(make_types)
77 @INIT
78 """)
79 assert mod.WithFields.__name__ == "WithFields"
80 assert mod.WithFields.__doc__ == "some doc"
81 assert mod.WithFields.__module__ == "mytest"
82 assert mod.NoFields.__name__ == "NoFields"
83 # In Python 3.8 or earlier, we cannot pass a NULL pointer as docstring,
84 # so we pass the empty string. Therefore, we are testing for None or
85 # empty docstring.
86 assert not mod.NoFields.__doc__
87 assert mod.NoFields.__module__ == "mytest"
88
89 assert mod.WithFields.n_fields == 3
90 assert mod.NoFields.n_fields == 0
91
92 s0 = mod.build(mod.WithFields)
93 assert s0.field0 == 1, s0.field0
94 assert s0.field1 == 2.0, s0.field1
95 assert s0[2] == "3", s0[2]
96
97 with pytest.raises(TypeError):
98 mod.build(mod.NoFields)
99
100 with pytest.raises(TypeError):
101 mod.build(str)
102
103
104 def test_invalid_descriptor(self):
105 import pytest
106 mod = self.make_module("""
107 static HPyStructSequence_Field sentinel = { NULL, NULL };
108
109 static HPyStructSequence_Desc nofields_desc = {
110 .fields = &sentinel
111 };
112
113 HPyDef_METH(build, "build", HPyFunc_NOARGS)
114 static HPy build_impl(HPyContext *ctx, HPy self)
115 {
116 return HPyStructSequence_NewType(ctx, &nofields_desc);
117 }
118
119 @EXPORT(build)
120 @INIT
121 """)
122 with pytest.raises(SystemError):
123 mod.build()