Skip to content

Commit

Permalink
Implement type check for kwnames in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fangerer committed Apr 13, 2023
1 parent 49a2f66 commit d2a4c10
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
28 changes: 22 additions & 6 deletions hpy/debug/src/debug_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,17 @@ DHPy debug_ctx_Call(HPyContext *dctx, DHPy dh_callable, const DHPy *dh_args, siz
UHPy uh_callable = DHPy_unwrap(dctx, dh_callable);
UHPy uh_kwnames = DHPy_unwrap(dctx, dh_kwnames);
uctx = ctx_info->info->uctx;
HPy_ssize_t nkw = HPy_IsNull(uh_kwnames) ? 0 : HPy_Length(uctx, uh_kwnames);
if (nkw < 0) {
return HPy_NULL;
HPy_ssize_t nkw;
if (!HPy_IsNull(uh_kwnames)) {
if (!HPyTuple_Check(uctx, uh_kwnames)) {
HPy_FatalError(uctx, "HPy_Call arg 'kwnames' must be a tuple object or HPy_NULL");
}
nkw = HPy_Length(uctx, uh_kwnames);
if (nkw < 0) {
return HPy_NULL;
}
} else {
nkw = 0;
}
const size_t n_all_args = nargs + nkw;
UHPy *uh_args = (UHPy *)alloca(n_all_args * sizeof(UHPy));
Expand All @@ -652,9 +660,17 @@ DHPy debug_ctx_CallMethod(HPyContext *dctx, DHPy dh_name, const DHPy *dh_args,
UHPy uh_name = DHPy_unwrap(dctx, dh_name);
UHPy uh_kwnames = DHPy_unwrap(dctx, dh_kwnames);
uctx = ctx_info->info->uctx;
HPy_ssize_t nkw = HPy_IsNull(uh_kwnames) ? 0 : HPy_Length(uctx, uh_kwnames);
if (nkw < 0) {
return HPy_NULL;
HPy_ssize_t nkw;
if (!HPy_IsNull(uh_kwnames)) {
if (!HPyTuple_Check(uctx, uh_kwnames)) {
HPy_FatalError(uctx, "HPy_CallMethod arg 'kwnames' must be a tuple object or HPy_NULL");
}
nkw = HPy_Length(uctx, uh_kwnames);
if (nkw < 0) {
return HPy_NULL;
}
} else {
nkw = 0;
}
const size_t n_all_args = nargs + nkw;
UHPy *uh_args = (UHPy *)alloca(n_all_args * sizeof(UHPy));
Expand Down
4 changes: 4 additions & 0 deletions hpy/devel/src/runtime/ctx_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ ctx_Call(HPyContext *ctx, HPy h_callable, const HPy *h_args, size_t nargs, HPy h
n_all_args = nargs;
} else {
kwnames = _h2py(h_kwnames);
#ifdef HPY_ABI_CPYTHON
assert(kwnames != NULL);
assert(PyTuple_Check(kwnames));
#endif
n_all_args = nargs + PyTuple_GET_SIZE(kwnames);
assert(n_all_args >= nargs);
}
Expand Down Expand Up @@ -89,8 +91,10 @@ ctx_CallMethod(HPyContext *ctx, HPy h_name, const HPy *h_args, size_t nargs,
n_all_args = nargs;
} else {
kwnames = _h2py(h_kwnames);
#ifdef HPY_ABI_CPYTHON
assert(kwnames != NULL);
assert(PyTuple_Check(kwnames));
#endif
n_all_args = nargs + PyTuple_GET_SIZE(kwnames);
assert(n_all_args >= nargs);
}
Expand Down

0 comments on commit d2a4c10

Please sign in to comment.