-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HPy_CallTupleDict, HPyCallable_Check and HPyTuple_Check. #147
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ffa8004
Add HPy_Call, HPy_CallObject and HPyCallable_Check.
hodgestar 6a38dfa
Reactivate unicode exceptions test.
hodgestar 5c2fc69
Add tests for HPy_Call, HPy_CallObject and HPyCallable_Check.
hodgestar def721e
Replace HPy_Call with HPy_CallTupleDict.
hodgestar cc4cdfe
Implement initial HPy_CallTupleDict.
hodgestar 3ef7267
Update UnicodeError tests to HPy_CallTupleDict.
hodgestar c14af6f
Implement HPy_CallTupleDict cases where args or kw are NULL.
hodgestar d4954f7
Update HPy_CallTupleDict tests.
hodgestar 342bfa0
Update tests to match expected HPy_CallTupleDict behaviour.
hodgestar 98f448a
Add HPyTuple_Check.
hodgestar 287e63b
Add type checking for handles used to pass arguments to HPy_CallTuple…
hodgestar b6540a2
Use keywords for argument_combination arguments.
hodgestar 085542f
Add microbenchmarks for HPy_CallTupleDict.
hodgestar 49c6b7e
Merge branch 'master' into feature/add-hpy-call
hodgestar 39da59f
Add porting docs for HPy_CallTupleDict.
hodgestar c520f3e
Add test for HPyTuple_Check.
hodgestar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from .support import HPyTest | ||
|
||
|
||
class TestCall(HPyTest): | ||
def test_hpy_call(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 f, f_args; | ||
HPy f_kw = HPy_NULL; | ||
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO|O", &f, &f_args, | ||
&f_kw)) | ||
return HPy_NULL; | ||
return HPy_Call(ctx, f, f_args, f_kw); | ||
} | ||
@EXPORT(call) | ||
@INIT | ||
""") | ||
|
||
def f(a, b): | ||
return a + b | ||
|
||
assert mod.call(f, (1, 2)) == 3 | ||
assert mod.call(f, (), {"a": 2, "b": 3}) == 5 | ||
assert mod.call(str, (2,)) == "2" | ||
assert mod.call(str, (2,), {}) == "2" | ||
with pytest.raises(TypeError): | ||
mod.call(f, (1,)) | ||
with pytest.raises(TypeError): | ||
mod.call(f, (), {"b": 2}) | ||
with pytest.raises(TypeError): | ||
mod.call(f, (), {}) | ||
with pytest.raises(TypeError): | ||
mod.call("not callable", (2,)) | ||
with pytest.raises(TypeError): | ||
mod.call("not callable", (2,), {}) | ||
|
||
def test_hpy_callobject(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 f; | ||
HPy f_args = HPy_NULL; | ||
if (!HPyArg_Parse(ctx, NULL, args, nargs, "O|O", &f, &f_args)) | ||
return HPy_NULL; | ||
return HPy_CallObject(ctx, f, f_args); | ||
} | ||
@EXPORT(call) | ||
@INIT | ||
""") | ||
|
||
def f(a, b): | ||
return a + b | ||
|
||
def g(): | ||
return "this is g" | ||
|
||
assert mod.call(f, (1, 2)) == 3 | ||
assert mod.call(g) == "this is g" | ||
assert mod.call(str, (2,)) == "2" | ||
with pytest.raises(TypeError): | ||
mod.call(f, (1,)) | ||
with pytest.raises(TypeError): | ||
mod.call("not callable", (2,)) | ||
|
||
def test_hpycallable_check(self): | ||
mod = self.make_module(""" | ||
HPyDef_METH(f, "f", f_impl, HPyFunc_O) | ||
static HPy f_impl(HPyContext ctx, HPy self, HPy arg) | ||
{ | ||
if (HPyCallable_Check(ctx, arg)) | ||
return HPy_Dup(ctx, ctx->h_True); | ||
return HPy_Dup(ctx, ctx->h_False); | ||
} | ||
@EXPORT(f) | ||
@INIT | ||
""") | ||
|
||
def f(): | ||
return "this is f" | ||
|
||
assert mod.f(f) is True | ||
assert mod.f(str) is True | ||
assert mod.f("a") is False | ||
assert mod.f(3) is False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should also test what happens if you pass something which is not a tuple and/or a dict?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I'll add some.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The answer is that C Python segfaults in some cases and works fine in others (e.g. a list of args). I've added tests and changed the HPy implementation to check the types of handles rather than segfault.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if doing these extra check costs any performance. Do you feel like adding microbenchmarks?
If they are costly, we could do the check only in debug mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing not a whole lot, but I'll add the microbenchmarks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Microbenchmarks added.