| 1 |
import types |
| 2 |
import pytest |
| 3 |
from .support import HPyTest |
| 4 |
|
| 5 |
class TestModule(HPyTest): |
| 6 |
def test_HPyModule_simple(self): |
| 7 |
""" |
| 8 |
The simplest fully declarative module creation. |
| 9 |
""" |
| 10 |
mod = self.make_module(""" |
| 11 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 12 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 13 |
{ |
| 14 |
return HPyLong_FromLong(ctx, 42); |
| 15 |
} |
| 16 |
|
| 17 |
static HPyDef *moduledefs[] = { &f, NULL }; |
| 18 |
static HPyModuleDef moduledef = { |
| 19 |
.doc = "Some doc", |
| 20 |
.size = 0, |
| 21 |
.legacy_methods = NULL, |
| 22 |
.defines = moduledefs, |
| 23 |
.globals = NULL, |
| 24 |
}; |
| 25 |
|
| 26 |
@HPy_MODINIT(moduledef) |
| 27 |
""") |
| 28 |
import sys |
| 29 |
ISPYPY = "__pypy__" in sys.modules |
| 30 |
if not ISPYPY: |
| 31 |
|
| 32 |
assert mod.__name__ == mod.__spec__.name |
| 33 |
assert mod.__doc__ == "Some doc" |
| 34 |
assert mod.f() == 42 |
| 35 |
|
| 36 |
def test_HPyModule_custom_exec(self): |
| 37 |
""" |
| 38 |
Module that defines several exec slots. HPy specifies that the slots |
| 39 |
will be executed in declaration order. Exec slots can add new types, |
| 40 |
and other objects into the module. They can also initialize other |
| 41 |
objects. The exec slots will be called on every new instance of the |
| 42 |
module, for example, when it is imported in several subinterpreters. |
| 43 |
""" |
| 44 |
mod = self.make_module(""" |
| 45 |
HPyDef_SLOT(exec1, HPy_mod_exec) |
| 46 |
static int exec1_impl(HPyContext *ctx, HPy mod) |
| 47 |
{ |
| 48 |
HPy list = HPyList_New(ctx, 0); |
| 49 |
if (HPy_IsNull(list)) |
| 50 |
return -1; |
| 51 |
HPy_SetAttr_s(ctx, mod, "data", list); |
| 52 |
HPy_Close(ctx, list); |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
HPyDef_SLOT(exec2, HPy_mod_exec) |
| 57 |
static int exec2_impl(HPyContext *ctx, HPy mod) |
| 58 |
{ |
| 59 |
HPy list = HPy_GetAttr_s(ctx, mod, "data"); |
| 60 |
if (HPy_IsNull(list)) |
| 61 |
return -1; |
| 62 |
if (HPy_Length(ctx, list) != 0) { |
| 63 |
HPyErr_SetString(ctx, ctx->h_RuntimeError, "Unexpected: len(list) != 0"); |
| 64 |
return -1; |
| 65 |
} |
| 66 |
HPy item = HPyLong_FromLong(ctx, 42); |
| 67 |
HPyList_Append(ctx, list, item); |
| 68 |
HPy_Close(ctx, item); |
| 69 |
HPy_Close(ctx, list); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static HPyDef *moduledefs[] = { |
| 74 |
&exec1, |
| 75 |
&exec2, |
| 76 |
NULL |
| 77 |
}; |
| 78 |
|
| 79 |
static HPyModuleDef moduledef = { |
| 80 |
.doc = "Some doc", |
| 81 |
.size = 0, |
| 82 |
.legacy_methods = NULL, |
| 83 |
.defines = moduledefs, |
| 84 |
.globals = NULL, |
| 85 |
}; |
| 86 |
|
| 87 |
@HPy_MODINIT(moduledef) |
| 88 |
""") |
| 89 |
assert mod.data == [42] |
| 90 |
|
| 91 |
def test_HPyModule_custom_create_returns_non_module(self): |
| 92 |
""" |
| 93 |
Module that defines create slot that returns non module object. This |
| 94 |
is, for the time being, the only supported way to implement the module |
| 95 |
'create' slot. HPy intentionally does not expose direct API to create |
| 96 |
a module object. Module objects are created for the extension by the |
| 97 |
runtime and the extension can only populate that module object in the |
| 98 |
init slots. |
| 99 |
""" |
| 100 |
mod = self.make_module(""" |
| 101 |
HPyDef_SLOT(create, HPy_mod_create) |
| 102 |
static HPy create_impl(HPyContext *ctx, HPy spec) |
| 103 |
{ |
| 104 |
HPy result = HPy_NULL, dict = HPy_NULL, ns_type = HPy_NULL; |
| 105 |
|
| 106 |
HPy types = HPyImport_ImportModule(ctx, "types"); |
| 107 |
if (HPy_IsNull(types)) |
| 108 |
return HPy_NULL; |
| 109 |
|
| 110 |
ns_type = HPy_GetAttr_s(ctx, types, "SimpleNamespace"); |
| 111 |
if (HPy_IsNull(ns_type)) |
| 112 |
goto cleanup; |
| 113 |
dict = HPyDict_New(ctx); |
| 114 |
HPy_SetItem_s(ctx, dict, "spec", spec); |
| 115 |
result = HPy_CallTupleDict(ctx, ns_type, HPy_NULL, dict); |
| 116 |
if (HPy_IsNull(result)) |
| 117 |
goto cleanup; |
| 118 |
|
| 119 |
cleanup: |
| 120 |
HPy_Close(ctx, dict); |
| 121 |
HPy_Close(ctx, types); |
| 122 |
HPy_Close(ctx, ns_type); |
| 123 |
return result; |
| 124 |
} |
| 125 |
|
| 126 |
static HPyDef *moduledefs[] = { |
| 127 |
&create, |
| 128 |
NULL |
| 129 |
}; |
| 130 |
|
| 131 |
static HPyModuleDef moduledef = { |
| 132 |
.doc = NULL, |
| 133 |
.size = 0, |
| 134 |
.legacy_methods = NULL, |
| 135 |
.defines = moduledefs, |
| 136 |
.globals = NULL, |
| 137 |
}; |
| 138 |
|
| 139 |
@HPy_MODINIT(moduledef) |
| 140 |
""") |
| 141 |
assert isinstance(mod, types.SimpleNamespace) |
| 142 |
assert mod.spec is mod.__spec__ |
| 143 |
|
| 144 |
def test_HPyModule_error_when_create_returns_module(self): |
| 145 |
""" |
| 146 |
The HPy_mod_create slot cannot return a builtin module object. |
| 147 |
HPy does not expose any API to create builtin module objects and, until |
| 148 |
there are any actual use-cases, the purpose of the 'create' slot is to |
| 149 |
create non-builtin-module objects. |
| 150 |
""" |
| 151 |
expected_message = "HPy_mod_create slot returned a builtin module " \ |
| 152 |
"object. This is currently not supported." |
| 153 |
with pytest.raises(SystemError, match=expected_message): |
| 154 |
self.make_module(""" |
| 155 |
HPyDef_SLOT(create, HPy_mod_create) |
| 156 |
static HPy create_impl(HPyContext *ctx, HPy spec) |
| 157 |
{ |
| 158 |
return HPyImport_ImportModule(ctx, "types"); |
| 159 |
} |
| 160 |
|
| 161 |
static HPyDef *moduledefs[] = { |
| 162 |
&create, |
| 163 |
NULL |
| 164 |
}; |
| 165 |
|
| 166 |
static HPyModuleDef moduledef = { |
| 167 |
.doc = NULL, |
| 168 |
.size = 0, |
| 169 |
.legacy_methods = NULL, |
| 170 |
.defines = moduledefs, |
| 171 |
.globals = NULL, |
| 172 |
}; |
| 173 |
|
| 174 |
@HPy_MODINIT(moduledef) |
| 175 |
""") |
| 176 |
|
| 177 |
def test_HPyModule_create_raises(self): |
| 178 |
with pytest.raises(RuntimeError, match="Test error"): |
| 179 |
self.make_module(""" |
| 180 |
HPyDef_SLOT(create, HPy_mod_create) |
| 181 |
static HPy create_impl(HPyContext *ctx, HPy spec) |
| 182 |
{ |
| 183 |
HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error"); |
| 184 |
return HPy_NULL; |
| 185 |
} |
| 186 |
|
| 187 |
static HPyDef *moduledefs[] = { |
| 188 |
&create, |
| 189 |
NULL |
| 190 |
}; |
| 191 |
|
| 192 |
static HPyModuleDef moduledef = { |
| 193 |
.doc = NULL, |
| 194 |
.size = 0, |
| 195 |
.legacy_methods = NULL, |
| 196 |
.defines = moduledefs, |
| 197 |
.globals = NULL, |
| 198 |
}; |
| 199 |
|
| 200 |
@HPy_MODINIT(moduledef) |
| 201 |
""") |
| 202 |
|
| 203 |
def test_HPyModule_create_and_nondefault_values(self): |
| 204 |
expected_message = r'^HPyModuleDef defines a HPy_mod_create slot.*' |
| 205 |
with pytest.raises(SystemError, match=expected_message): |
| 206 |
self.make_module(""" |
| 207 |
HPyDef_SLOT(create, HPy_mod_create) |
| 208 |
static HPy create_impl(HPyContext *ctx, HPy spec) |
| 209 |
{ |
| 210 |
HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error"); |
| 211 |
return HPy_NULL; |
| 212 |
} |
| 213 |
|
| 214 |
static HPyDef *moduledefs[] = { |
| 215 |
&create, |
| 216 |
NULL |
| 217 |
}; |
| 218 |
|
| 219 |
static HPyModuleDef moduledef = { |
| 220 |
.doc = "Some doc - this is non-default", |
| 221 |
.size = 0, |
| 222 |
.legacy_methods = NULL, |
| 223 |
.defines = moduledefs, |
| 224 |
.globals = NULL, |
| 225 |
}; |
| 226 |
|
| 227 |
@HPy_MODINIT(moduledef) |
| 228 |
""") |
| 229 |
|
| 230 |
def test_HPyModule_create_and_exec_slots(self): |
| 231 |
expected_message = r'^HPyModuleDef defines a HPy_mod_create slot.*' |
| 232 |
with pytest.raises(SystemError, match=expected_message): |
| 233 |
self.make_module(""" |
| 234 |
HPyDef_SLOT(create, HPy_mod_create) |
| 235 |
static HPy create_impl(HPyContext *ctx, HPy spec) |
| 236 |
{ |
| 237 |
HPyErr_SetString(ctx, ctx->h_RuntimeError, "Test error"); |
| 238 |
return HPy_NULL; |
| 239 |
} |
| 240 |
|
| 241 |
HPyDef_SLOT(exec, HPy_mod_exec) |
| 242 |
static int exec_impl(HPyContext *ctx, HPy mod) |
| 243 |
{ |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
static HPyDef *moduledefs[] = { |
| 248 |
&create, |
| 249 |
&exec, |
| 250 |
NULL |
| 251 |
}; |
| 252 |
|
| 253 |
static HPyModuleDef moduledef = { |
| 254 |
.doc = NULL, |
| 255 |
.size = 0, |
| 256 |
.legacy_methods = NULL, |
| 257 |
.defines = moduledefs, |
| 258 |
.globals = NULL, |
| 259 |
}; |
| 260 |
|
| 261 |
@HPy_MODINIT(moduledef) |
| 262 |
""") |
| 263 |
|
| 264 |
def test_HPyModule_negative_size(self): |
| 265 |
""" |
| 266 |
The simplest fully declarative module creation. |
| 267 |
""" |
| 268 |
expected_message = "HPy does not permit HPyModuleDef.size < 0" |
| 269 |
with pytest.raises(SystemError, match=expected_message): |
| 270 |
self.make_module(""" |
| 271 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 272 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 273 |
{ |
| 274 |
return HPyLong_FromLong(ctx, 42); |
| 275 |
} |
| 276 |
|
| 277 |
static HPyDef *moduledefs[] = { &f, NULL }; |
| 278 |
static HPyModuleDef moduledef = { |
| 279 |
.doc = "Some doc", |
| 280 |
.size = -1, |
| 281 |
.legacy_methods = NULL, |
| 282 |
.defines = moduledefs, |
| 283 |
.globals = NULL, |
| 284 |
}; |
| 285 |
|
| 286 |
@HPy_MODINIT(moduledef) |
| 287 |
""") |