Debian Codegraph @

extra_tests/hpy_tests/_vendored/test_legacy_forbidden.py

raw symbols SCIP
1 """
2 In this file we check that if we use legacy features in universal mode, we
3 get the expected compile time errors
4 """
5
6 from .support import HPyTest, make_hpy_abi_fixture, ONLY_LINUX
7
8 # this is not strictly correct, we should check whether the actual compiler
9 # is GCC. But for the CI and most cases, it's enough to assume that if we are
10 # on linux we are using GCC.
11 #
12 # We need this because some of the nice compilation errors (such as the ones
13 # causes by _HPY_LEGACY) are triggered only by gcc. Would be nice to have them
14 # also for other compilers
15 ONLY_GCC = ONLY_LINUX
16
17
18 class TestLegacyForbidden(HPyTest):
19
20 hpy_abi = make_hpy_abi_fixture(['universal'], class_fixture=True)
21
22 LEGACY_ERROR = "Cannot use legacy functions when targeting the HPy Universal ABI"
23
24 def test_expect_make_error(self):
25 src = """
26 #error "this is a compile time error"
27 """
28 self.expect_make_error(src, "this is a compile time error")
29
30 def test_Python_h_forbidden(self, capfd):
31 src = """
32 #include <Python.h>
33 @INIT
34 """
35 self.expect_make_error(src,
36 "It is forbidden to #include <Python.h> "
37 "when targeting the HPy Universal ABI")
38
39 @ONLY_GCC
40 def test_HPy_AsPyObject(self, capfd):
41 # NOTE: in this test we don't include Python.h. We want to test that
42 # we get a nice compile-time error by just calling HPy_AsPyObject.
43 # that's why we use "cpy_PyObject" (which is available because defined
44 # by hpy.h)
45 src = """
46 HPyDef_METH(f, "f", HPyFunc_NOARGS)
47 static HPy f_impl(HPyContext *ctx, HPy self)
48 {
49 cpy_PyObject *pyobj = HPy_AsPyObject(ctx, self);
50 (void)pyobj; // silence the warning about unused variable
51 return HPy_NULL;
52 }
53 @EXPORT(f)
54 @INIT
55 """
56 self.expect_make_error(src, self.LEGACY_ERROR)
57
58 @ONLY_GCC
59 def test_HPy_FromPyObject(self, capfd):
60 # NOTE: in this test we don't include Python.h. We want to test that
61 # we get a nice compile-time error by just calling HPy_AsPyObject.
62 # that's why we use "cpy_PyObject" (which is available because defined
63 # by hpy.h)
64 src = """
65 HPyDef_METH(f, "f", HPyFunc_NOARGS)
66 static HPy f_impl(HPyContext *ctx, HPy self)
67 {
68 cpy_PyObject *pyobj = NULL;
69 return HPy_FromPyObject(ctx, pyobj);
70 }
71 @EXPORT(f)
72 @INIT
73 """
74 self.expect_make_error(src, self.LEGACY_ERROR)