Debian Codegraph @

extra_tests/hpy_tests/_vendored/debug/test_misc.py

raw symbols SCIP
1 import pytest
2 from ..support import SUPPORTS_SYS_EXECUTABLE, SUPPORTS_MEM_PROTECTION
3
4 @pytest.fixture
5 def hpy_abi():
6 return "debug"
7
8
9 @pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
10 def test_use_invalid_as_struct(compiler, python_subprocess):
11 mod = compiler.compile_module("""
12 typedef struct {
13 int value;
14 } DummyObject;
15 HPyType_HELPERS(DummyObject, HPyType_BuiltinShape_Object)
16
17 static HPyType_Spec Dummy_spec = {
18 .name = "mytest.Dummy",
19 .basicsize = sizeof(DummyObject),
20 .builtin_shape = HPyType_BuiltinShape_Type
21 };
22
23
24 HPyDef_METH(f, "f", HPyFunc_O)
25 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
26 {
27 DummyObject *data = DummyObject_AsStruct(ctx, arg);
28 return HPyLong_FromLong(ctx, data->value);
29 }
30
31 @EXPORT_TYPE("Dummy", Dummy_spec)
32 @EXPORT(f)
33 @INIT
34 """)
35 code = "assert mod.f(mod.Dummy()) == 0"
36 result = python_subprocess.run(mod, code)
37 assert result.returncode != 0
38 assert "Invalid usage of _HPy_AsStruct_Object" in result.stderr.decode("utf-8")
39
40
41 @pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
42 def test_typecheck(compiler, python_subprocess):
43 mod = compiler.compile_module("""
44 HPyDef_METH(f, "f", HPyFunc_VARARGS)
45 static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
46 {
47 if (nargs != 2) {
48 HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 2 arguments");
49 return HPy_NULL;
50 }
51 int res = HPy_TypeCheck(ctx, args[0], args[1]);
52 return HPyBool_FromLong(ctx, res);
53 }
54 @EXPORT(f)
55 @INIT
56 """)
57 code = "assert mod.f(mod.f('hello', 2)) == 0"
58 result = python_subprocess.run(mod, code)
59 assert result.returncode != 0
60 assert "HPy_TypeCheck arg 2 must be a type" in result.stderr.decode("utf-8")
61
62
63 @pytest.mark.skipif(not SUPPORTS_MEM_PROTECTION, reason=
64 "Could be implemented by checking the contents on close.")
65 @pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
66 def test_type_getname(compiler, python_subprocess):
67 mod = compiler.compile_module("""
68 #define MODE_READ_ONLY 0
69 #define MODE_USE_AFTER_FREE 1
70 HPyDef_METH(f, "f", HPyFunc_VARARGS)
71 static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
72 {
73 HPy type;
74 int mode;
75 const char *name;
76 char *buffer;
77
78 // parse arguments
79 if (nargs != 2) {
80 HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 2 arguments");
81 return HPy_NULL;
82 }
83 mode = HPyLong_AsLong(ctx, args[0]);
84 if (mode < 0) {
85 HPyErr_Clear(ctx);
86 HPyErr_SetString(ctx, ctx->h_ValueError, "invalid test mode");
87 return HPy_NULL;
88 }
89 type = mode == 1 ? HPy_Dup(ctx, args[1]) : args[1];
90
91 name = HPyType_GetName(ctx, type);
92 if (name == NULL)
93 return HPy_NULL;
94
95 switch (mode) {
96 case MODE_READ_ONLY:
97 // write to read-only memory
98 buffer = (char *)name;
99 buffer[0] = 'h';
100 break;
101 case MODE_USE_AFTER_FREE:
102 // will cause use after handle was closed
103 HPy_Close(ctx, type);
104 break;
105 }
106 return HPyUnicode_FromString(ctx, name);
107 }
108 @EXPORT(f)
109 @INIT
110 """)
111 result = python_subprocess.run(mod, "mod.f(0, 'hello')")
112 assert result.returncode != 0
113 assert "HPyType_GetName arg must be a type" in result.stderr.decode("utf-8")
114
115 result = python_subprocess.run(mod, "mod.f(0, str)")
116 assert result.returncode != 0
117
118 result = python_subprocess.run(mod, "mod.f(1, str)")
119 assert result.returncode != 0
120
121
122 @pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
123 def test_type_issubtype(compiler, python_subprocess):
124 mod = compiler.compile_module("""
125 HPyDef_METH(f, "f", HPyFunc_VARARGS)
126 static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
127 {
128 if (nargs != 2) {
129 HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 2 arguments");
130 return HPy_NULL;
131 }
132 return HPyLong_FromLong(ctx, HPyType_IsSubtype(ctx, args[0], args[1]));
133 }
134 @EXPORT(f)
135 @INIT
136 """)
137 result = python_subprocess.run(mod, "mod.f(bool, 'hello')")
138 assert result.returncode != 0
139 assert "HPyType_IsSubtype arg 2 must be a type" in result.stderr.decode("utf-8")
140
141 result = python_subprocess.run(mod, "mod.f('hello', str)")
142 assert result.returncode != 0
143 assert "HPyType_IsSubtype arg 1 must be a type" in result.stderr.decode("utf-8")
144
145
146 @pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
147 def test_unicode_substring(compiler, python_subprocess):
148 mod = compiler.compile_module("""
149 HPyDef_METH(f, "f", HPyFunc_VARARGS)
150 static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
151 {
152 HPy_ssize_t start, end;
153 if (nargs != 3) {
154 HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 3 arguments");
155 return HPy_NULL;
156 }
157
158 start = HPyLong_AsSsize_t(ctx, args[1]);
159 if (start == -1 && HPyErr_Occurred(ctx))
160 return HPy_NULL;
161
162 end = HPyLong_AsSsize_t(ctx, args[2]);
163 if (end == -1 && HPyErr_Occurred(ctx))
164 return HPy_NULL;
165
166 return HPyUnicode_Substring(ctx, args[0], start, end);
167 }
168 @EXPORT(f)
169 @INIT
170 """)
171 result = python_subprocess.run(mod, "mod.f(b'hello', 2, 3)")
172 assert result.returncode != 0
173 assert "HPyUnicode_Substring arg 1 must be a Unicode object" in result.stderr.decode("utf-8")