| 1 |
from .support import HPyTest |
| 2 |
|
| 3 |
|
| 4 |
class TestCall(HPyTest): |
| 5 |
def argument_combinations(self, **items): |
| 6 |
""" Returns all possible ways of expressing the given items as |
| 7 |
arguments to a function. |
| 8 |
""" |
| 9 |
items = list(items.items()) |
| 10 |
for i in range(len(items) + 1): |
| 11 |
args = tuple(item[1] for item in items[:i]) |
| 12 |
kw = dict(items[i:]) |
| 13 |
yield {"args": args, "kw": kw} |
| 14 |
if not args: |
| 15 |
yield {"kw": kw} |
| 16 |
if not kw: |
| 17 |
yield {"args": args} |
| 18 |
if not args and not kw: |
| 19 |
yield {} |
| 20 |
|
| 21 |
def argument_combinations_tuple(self, **items): |
| 22 |
""" Same as 'argument_combinations' but returns a tuple where |
| 23 |
the first element is the argument tuple and the second is |
| 24 |
a dict that may contain the keywords dict. |
| 25 |
""" |
| 26 |
items = list(items.items()) |
| 27 |
for i in range(len(items) + 1): |
| 28 |
args = tuple(item[1] for item in items[:i]) |
| 29 |
kw = dict(items[i:]) |
| 30 |
yield args, kw |
| 31 |
if not args: |
| 32 |
yield tuple(), kw |
| 33 |
if not kw: |
| 34 |
yield args, {} |
| 35 |
if not args and not kw: |
| 36 |
yield tuple(), {} |
| 37 |
|
| 38 |
def test_hpy_calltupledict(self): |
| 39 |
import pytest |
| 40 |
mod = self.make_module(""" |
| 41 |
HPyDef_METH(call, "call", HPyFunc_KEYWORDS) |
| 42 |
static HPy call_impl(HPyContext *ctx, HPy self, const HPy *args, |
| 43 |
size_t nargs, HPy kwnames) |
| 44 |
{ |
| 45 |
|
| 46 |
HPy f, result; |
| 47 |
HPy f_args = HPy_NULL; |
| 48 |
HPy f_kw = HPy_NULL; |
| 49 |
HPyTracker ht; |
| 50 |
static const char *kwlist[] = { "f", "args", "kw", NULL }; |
| 51 |
if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, kwnames, |
| 52 |
"O|OO", kwlist, &f, &f_args, &f_kw)) { |
| 53 |
return HPy_NULL; |
| 54 |
} |
| 55 |
result = HPy_CallTupleDict(ctx, f, f_args, f_kw); |
| 56 |
HPyTracker_Close(ctx, ht); |
| 57 |
return result; |
| 58 |
} |
| 59 |
@EXPORT(call) |
| 60 |
@INIT |
| 61 |
""") |
| 62 |
|
| 63 |
def f(a, b): |
| 64 |
return a + b |
| 65 |
|
| 66 |
def g(): |
| 67 |
return "this is g" |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
for d in self.argument_combinations(a=1, b=2): |
| 72 |
assert mod.call(f, **d) == 3 |
| 73 |
for d in self.argument_combinations(a=1): |
| 74 |
with pytest.raises(TypeError): |
| 75 |
mod.call(f, **d) |
| 76 |
for d in self.argument_combinations(): |
| 77 |
with pytest.raises(TypeError): |
| 78 |
mod.call(f, **d) |
| 79 |
for d in self.argument_combinations(): |
| 80 |
assert mod.call(g, **d) == "this is g" |
| 81 |
for d in self.argument_combinations(object=2): |
| 82 |
assert mod.call(str, **d) == "2" |
| 83 |
for d in self.argument_combinations(): |
| 84 |
with pytest.raises(TypeError): |
| 85 |
mod.call("not callable", **d) |
| 86 |
for d in self.argument_combinations(unknown=2): |
| 87 |
with pytest.raises(TypeError): |
| 88 |
mod.call("not callable", **d) |
| 89 |
|
| 90 |
|
| 91 |
with pytest.raises(TypeError): |
| 92 |
mod.call(f, args=[1, 2]) |
| 93 |
with pytest.raises(TypeError): |
| 94 |
mod.call(f, args="string") |
| 95 |
with pytest.raises(TypeError): |
| 96 |
mod.call(f, args=1) |
| 97 |
with pytest.raises(TypeError): |
| 98 |
mod.call(f, args=None) |
| 99 |
with pytest.raises(TypeError): |
| 100 |
mod.call(f, kw=[1, 2]) |
| 101 |
with pytest.raises(TypeError): |
| 102 |
mod.call(f, kw="string") |
| 103 |
with pytest.raises(TypeError): |
| 104 |
mod.call(f, kw=1) |
| 105 |
with pytest.raises(TypeError): |
| 106 |
mod.call(f, kw=None) |
| 107 |
|
| 108 |
def test_hpy_callmethodtupledict(self): |
| 109 |
import pytest |
| 110 |
mod = self.make_module(""" |
| 111 |
HPyDef_METH(call, "call", HPyFunc_KEYWORDS) |
| 112 |
static HPy call_impl(HPyContext *ctx, HPy self, |
| 113 |
const HPy *args, size_t nargs, HPy kwnames) |
| 114 |
{ |
| 115 |
HPy result, result_0, result_1; |
| 116 |
HPy receiver = HPy_NULL; |
| 117 |
HPy h_name = HPy_NULL; |
| 118 |
HPy m_args = HPy_NULL; |
| 119 |
const char *s_name = ""; |
| 120 |
HPyTracker ht; |
| 121 |
static const char *kwlist[] = { "receiver", "name", "args", NULL }; |
| 122 |
if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, kwnames, "OO|O", |
| 123 |
kwlist, &receiver, &h_name, &m_args)) { |
| 124 |
return HPy_NULL; |
| 125 |
} |
| 126 |
s_name = HPyUnicode_AsUTF8AndSize(ctx, h_name, NULL); |
| 127 |
if (s_name == NULL) { |
| 128 |
HPyTracker_Close(ctx, ht); |
| 129 |
return HPy_NULL; |
| 130 |
} |
| 131 |
|
| 132 |
result_0 = HPy_CallMethodTupleDict(ctx, h_name, receiver, m_args, HPy_NULL); |
| 133 |
if (HPy_IsNull(result_0)) { |
| 134 |
HPyTracker_Close(ctx, ht); |
| 135 |
return HPy_NULL; |
| 136 |
} |
| 137 |
|
| 138 |
result_1 = HPy_CallMethodTupleDict_s(ctx, s_name, receiver, m_args, HPy_NULL); |
| 139 |
if (HPy_IsNull(result_1)) { |
| 140 |
HPyTracker_Close(ctx, ht); |
| 141 |
HPy_Close(ctx, result_0); |
| 142 |
return HPy_NULL; |
| 143 |
} |
| 144 |
|
| 145 |
HPyTracker_Close(ctx, ht); |
| 146 |
result = HPyTuple_Pack(ctx, 2, result_0, result_1); |
| 147 |
HPy_Close(ctx, result_0); |
| 148 |
HPy_Close(ctx, result_1); |
| 149 |
return result; |
| 150 |
} |
| 151 |
@EXPORT(call) |
| 152 |
@INIT |
| 153 |
""") |
| 154 |
|
| 155 |
test_args = ( |
| 156 |
|
| 157 |
dict(receiver={"hello": 1, "world": 2}, name="keys", args=tuple()), |
| 158 |
dict(receiver="Hello, World", name="find", args=("Wo", )), |
| 159 |
) |
| 160 |
|
| 161 |
for kw in test_args: |
| 162 |
res = getattr(kw["receiver"], kw["name"])(*kw["args"]) |
| 163 |
assert mod.call(**kw) == (res, res) |
| 164 |
|
| 165 |
with pytest.raises(AttributeError): |
| 166 |
mod.call(receiver=dict(), name="asdf", args=tuple()) |
| 167 |
|
| 168 |
with pytest.raises(TypeError): |
| 169 |
mod.call(receiver="Hello, World", name="find") |
| 170 |
|
| 171 |
with pytest.raises(TypeError): |
| 172 |
mod.call(receiver="Hello, World", name="find", args=("1", ) * 100) |
| 173 |
|
| 174 |
def test_hpy_call(self): |
| 175 |
import pytest |
| 176 |
mod = self.make_module(""" |
| 177 |
#define SELF 1 |
| 178 |
|
| 179 |
HPyDef_METH(call, "call", HPyFunc_KEYWORDS) |
| 180 |
static HPy call_impl(HPyContext *ctx, HPy self, |
| 181 |
const HPy *args, size_t nargs, HPy kwnames) |
| 182 |
{ |
| 183 |
if (nargs < SELF) { |
| 184 |
HPyErr_SetString(ctx, ctx->h_TypeError, "HPy_Call requires a receiver"); |
| 185 |
return HPy_NULL; |
| 186 |
} |
| 187 |
return HPy_Call(ctx, args[0], args + SELF, nargs - SELF, kwnames); |
| 188 |
} |
| 189 |
@EXPORT(call) |
| 190 |
@INIT |
| 191 |
""") |
| 192 |
|
| 193 |
def foo(): |
| 194 |
raise ValueError |
| 195 |
|
| 196 |
def listify(*args): |
| 197 |
return args |
| 198 |
|
| 199 |
def f(a, b): |
| 200 |
return a + b |
| 201 |
|
| 202 |
def g(): |
| 203 |
return "this is g" |
| 204 |
|
| 205 |
class KwDict(dict): |
| 206 |
def __getitem__(self, key): |
| 207 |
return "key=" + str(key); |
| 208 |
|
| 209 |
test_args = ( |
| 210 |
|
| 211 |
(dict, (dict(a=0, b=1), ), {}), |
| 212 |
(dict, tuple(), dict(a=0, b=1)), |
| 213 |
) |
| 214 |
for receiver, args_tuple, kwd in test_args: |
| 215 |
assert mod.call(receiver, *args_tuple, **kwd) == receiver(*args_tuple, **kwd) |
| 216 |
|
| 217 |
|
| 218 |
mod.call(dict) |
| 219 |
|
| 220 |
|
| 221 |
kwdict = KwDict(x=11, y=12, z=13) |
| 222 |
assert mod.call(dict, **kwdict) == dict(kwdict) |
| 223 |
|
| 224 |
with pytest.raises(ValueError): |
| 225 |
mod.call(foo) |
| 226 |
with pytest.raises(TypeError): |
| 227 |
mod.call() |
| 228 |
|
| 229 |
|
| 230 |
r = range(1000) |
| 231 |
assert mod.call(listify, *r) == listify(*r) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
for args, kwd in self.argument_combinations_tuple(a=1, b=2): |
| 236 |
assert mod.call(f, *args, **kwd) == 3 |
| 237 |
for args, kwd in self.argument_combinations_tuple(a=1): |
| 238 |
with pytest.raises(TypeError): |
| 239 |
mod.call(f, *args, **kwd) |
| 240 |
for args, kwd in self.argument_combinations_tuple(): |
| 241 |
with pytest.raises(TypeError): |
| 242 |
mod.call(f, *args, **kwd) |
| 243 |
for args, kwd in self.argument_combinations_tuple(): |
| 244 |
assert mod.call(g, *args, **kwd) == "this is g" |
| 245 |
for args, kwd in self.argument_combinations_tuple(object=2): |
| 246 |
assert mod.call(str, *args, **kwd) == "2" |
| 247 |
for args, kwd in self.argument_combinations_tuple(): |
| 248 |
with pytest.raises(TypeError): |
| 249 |
mod.call("not callable", *args, **kwd) |
| 250 |
for args, kwd in self.argument_combinations_tuple(unknown=2): |
| 251 |
with pytest.raises(TypeError): |
| 252 |
mod.call("not callable", *args, **kwd) |
| 253 |
|
| 254 |
def test_hpy_callmethod(self): |
| 255 |
import pytest |
| 256 |
mod = self.make_module(""" |
| 257 |
#define NAME 1 |
| 258 |
|
| 259 |
HPyDef_METH(call, "call", HPyFunc_KEYWORDS) |
| 260 |
static HPy call_impl(HPyContext *ctx, HPy self, |
| 261 |
const HPy *args, size_t nargs, HPy kwnames) |
| 262 |
{ |
| 263 |
if (nargs < NAME) { |
| 264 |
HPyErr_SetString(ctx, ctx->h_TypeError, "HPy_CallMethod requires a receiver and a method name"); |
| 265 |
return HPy_NULL; |
| 266 |
} |
| 267 |
// 'args[0]' is the name |
| 268 |
return HPy_CallMethod(ctx, args[0], args + NAME, nargs - NAME, kwnames); |
| 269 |
} |
| 270 |
@EXPORT(call) |
| 271 |
@INIT |
| 272 |
""") |
| 273 |
|
| 274 |
class Dummy: |
| 275 |
not_callable = 123 |
| 276 |
|
| 277 |
def f(self, a, b): |
| 278 |
return a + b |
| 279 |
|
| 280 |
def g(self): |
| 281 |
return 'this is g' |
| 282 |
|
| 283 |
test_obj = Dummy() |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
for args, kwd in self.argument_combinations_tuple(a=1, b=2): |
| 288 |
assert mod.call('f', test_obj, *args, **kwd) == 3 |
| 289 |
for args, kwd in self.argument_combinations_tuple(a=1): |
| 290 |
with pytest.raises(TypeError): |
| 291 |
mod.call('f', test_obj, *args, **kwd) |
| 292 |
for args, kwd in self.argument_combinations_tuple(a=1, b=2, c=3): |
| 293 |
with pytest.raises(TypeError): |
| 294 |
mod.call('f', test_obj, *args, **kwd) |
| 295 |
for args, kwd in self.argument_combinations_tuple(): |
| 296 |
with pytest.raises(TypeError): |
| 297 |
mod.call('f', test_obj, *args, **kwd) |
| 298 |
for args, kwd in self.argument_combinations_tuple(): |
| 299 |
assert mod.call('g', test_obj, *args, **kwd) == 'this is g' |
| 300 |
for args, kwd in self.argument_combinations_tuple(): |
| 301 |
with pytest.raises(TypeError): |
| 302 |
mod.call('not_callable', test_obj, *args, **kwd) |
| 303 |
for args, kwd in self.argument_combinations_tuple(unknown=2): |
| 304 |
with pytest.raises(TypeError): |
| 305 |
mod.call('not_callable', test_obj, *args, **kwd) |
| 306 |
for args, kwd in self.argument_combinations_tuple(): |
| 307 |
with pytest.raises(AttributeError): |
| 308 |
mod.call('embedded null byte', test_obj, *args, **kwd) |
| 309 |
|
| 310 |
def test_hpycallable_check(self): |
| 311 |
mod = self.make_module(""" |
| 312 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 313 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 314 |
{ |
| 315 |
if (HPyCallable_Check(ctx, arg)) |
| 316 |
return HPy_Dup(ctx, ctx->h_True); |
| 317 |
return HPy_Dup(ctx, ctx->h_False); |
| 318 |
} |
| 319 |
@EXPORT(f) |
| 320 |
@INIT |
| 321 |
""") |
| 322 |
|
| 323 |
def f(): |
| 324 |
return "this is f" |
| 325 |
|
| 326 |
assert mod.f(f) is True |
| 327 |
assert mod.f(str) is True |
| 328 |
assert mod.f("a") is False |
| 329 |
assert mod.f(3) is False |