Skip to content

Commit

Permalink
Add test for HPy_CallMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
fangerer committed Oct 14, 2021
1 parent 6e75782 commit 9fa9f02
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ def g():
with pytest.raises(TypeError):
mod.call(f, kw=None)

def test_hpy_callmethod(self):
import pytest
mod = self.make_module("""
HPyDef_METH(call, "call", call_impl, HPyFunc_VARARGS)
static HPy call_impl(HPyContext *ctx, HPy self,
HPy *args, HPy_ssize_t nargs)
{
HPy receiver, result;
const char *m_name = NULL;
HPy *m_args = args + 2;
HPy_ssize_t m_nargs = nargs - 2;
HPyTracker ht;
static const char *kwlist[] = { "receiver", "name", NULL };
if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, HPy_NULL, "Os",
kwlist, &receiver, &m_name)) {
return HPy_NULL;
}
result = HPy_CallMethod(ctx, receiver, m_name, m_args, m_nargs);
HPyTracker_Close(ctx, ht);
return result;
}
@EXPORT(call)
@INIT
""")

test_args = (
# (receiver, method, args_tuple)
({"hello": 1, "world": 2}, "keys", tuple()),
("Hello, World", "find", ("Wo", )),
)
for receiver, method, args_tuple in test_args:
assert mod.call(receiver, method, *args_tuple) == getattr(receiver, method)(*args_tuple)
with pytest.raises(TypeError):
mod.call("Receiver", "find", 1, 2, 3, 4)
with pytest.raises(AttributeError):
mod.call(None, "blah")

def test_hpycallable_check(self):
mod = self.make_module("""
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
Expand Down

0 comments on commit 9fa9f02

Please sign in to comment.