forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_misc.py
More file actions
179 lines (155 loc) · 6.37 KB
/
test_misc.py
File metadata and controls
179 lines (155 loc) · 6.37 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import pytest
import sys
from ..support import SUPPORTS_SYS_EXECUTABLE, SUPPORTS_MEM_PROTECTION, IS_GRAALPY
@pytest.fixture
def hpy_abi():
return "debug"
@pytest.mark.skipif(IS_GRAALPY, reason="hangs on GraalPy")
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
def test_use_invalid_as_struct(compiler, python_subprocess):
mod = compiler.compile_module("""
typedef struct {
int value;
} DummyObject;
HPyType_HELPERS(DummyObject, HPyType_BuiltinShape_Object)
static HPyType_Spec Dummy_spec = {
.name = "mytest.Dummy",
.basicsize = sizeof(DummyObject),
.builtin_shape = HPyType_BuiltinShape_Type
};
HPyDef_METH(f, "f", HPyFunc_O)
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
{
DummyObject *data = DummyObject_AsStruct(ctx, arg);
return HPyLong_FromLong(ctx, data->value);
}
@EXPORT_TYPE("Dummy", Dummy_spec)
@EXPORT(f)
@INIT
""")
code = "assert mod.f(mod.Dummy()) == 0"
result = python_subprocess.run(mod, code)
assert result.returncode != 0
assert "Invalid usage of _HPy_AsStruct_Object" in result.stderr.decode("utf-8")
@pytest.mark.skipif(IS_GRAALPY, reason="hangs on GraalPy")
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
def test_typecheck(compiler, python_subprocess):
mod = compiler.compile_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
{
if (nargs != 2) {
HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 2 arguments");
return HPy_NULL;
}
int res = HPy_TypeCheck(ctx, args[0], args[1]);
return HPyBool_FromLong(ctx, res);
}
@EXPORT(f)
@INIT
""")
code = "assert mod.f(mod.f('hello', 2)) == 0"
result = python_subprocess.run(mod, code)
assert result.returncode != 0
assert "HPy_TypeCheck arg 2 must be a type" in result.stderr.decode("utf-8")
@pytest.mark.skipif(IS_GRAALPY, reason="hangs on GraalPy")
@pytest.mark.skipif(not SUPPORTS_MEM_PROTECTION, reason=
"Could be implemented by checking the contents on close.")
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
def test_type_getname(compiler, python_subprocess):
mod = compiler.compile_module("""
#define MODE_READ_ONLY 0
#define MODE_USE_AFTER_FREE 1
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
{
HPy type;
int mode;
const char *name;
char *buffer;
// parse arguments
if (nargs != 2) {
HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 2 arguments");
return HPy_NULL;
}
mode = HPyLong_AsLong(ctx, args[0]);
if (mode < 0) {
HPyErr_Clear(ctx);
HPyErr_SetString(ctx, ctx->h_ValueError, "invalid test mode");
return HPy_NULL;
}
type = mode == 1 ? HPy_Dup(ctx, args[1]) : args[1];
name = HPyType_GetName(ctx, type);
if (name == NULL)
return HPy_NULL;
switch (mode) {
case MODE_READ_ONLY:
// write to read-only memory
buffer = (char *)name;
buffer[0] = 'h';
break;
case MODE_USE_AFTER_FREE:
// will cause use after handle was closed
HPy_Close(ctx, type);
break;
}
return HPyUnicode_FromString(ctx, name);
}
@EXPORT(f)
@INIT
""")
result = python_subprocess.run(mod, "mod.f(0, 'hello')")
assert result.returncode != 0
assert "HPyType_GetName arg must be a type" in result.stderr.decode("utf-8")
result = python_subprocess.run(mod, "mod.f(0, str)")
assert result.returncode != 0
result = python_subprocess.run(mod, "mod.f(1, str)")
assert result.returncode != 0
@pytest.mark.skipif(IS_GRAALPY, reason="hangs on GraalPy")
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
def test_type_issubtype(compiler, python_subprocess):
mod = compiler.compile_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
{
if (nargs != 2) {
HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 2 arguments");
return HPy_NULL;
}
return HPyLong_FromLong(ctx, HPyType_IsSubtype(ctx, args[0], args[1]));
}
@EXPORT(f)
@INIT
""")
result = python_subprocess.run(mod, "mod.f(bool, 'hello')")
assert result.returncode != 0
assert "HPyType_IsSubtype arg 2 must be a type" in result.stderr.decode("utf-8")
result = python_subprocess.run(mod, "mod.f('hello', str)")
assert result.returncode != 0
assert "HPyType_IsSubtype arg 1 must be a type" in result.stderr.decode("utf-8")
@pytest.mark.skipif(IS_GRAALPY, reason="transiently fails on GraalPy")
@pytest.mark.skipif(not SUPPORTS_SYS_EXECUTABLE, reason="needs subprocess")
def test_unicode_substring(compiler, python_subprocess):
mod = compiler.compile_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
{
HPy_ssize_t start, end;
if (nargs != 3) {
HPyErr_SetString(ctx, ctx->h_TypeError, "expected exactly 3 arguments");
return HPy_NULL;
}
start = HPyLong_AsSsize_t(ctx, args[1]);
if (start == -1 && HPyErr_Occurred(ctx))
return HPy_NULL;
end = HPyLong_AsSsize_t(ctx, args[2]);
if (end == -1 && HPyErr_Occurred(ctx))
return HPy_NULL;
return HPyUnicode_Substring(ctx, args[0], start, end);
}
@EXPORT(f)
@INIT
""")
result = python_subprocess.run(mod, "mod.f(b'hello', 2, 3)")
assert result.returncode != 0
assert "HPyUnicode_Substring arg 1 must be a Unicode object" in result.stderr.decode("utf-8")