| 1 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 |
from pypy.interpreter.error import oefmt |
| 3 |
from .apiset import API |
| 4 |
|
| 5 |
def Vectorcall_NARGS(n): |
| 6 |
PY_VECTORCALL_ARGUMENTS_OFFSET = 1 << (8 * rffi.sizeof(rffi.SIZE_T) - 1) |
| 7 |
return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET |
| 8 |
|
| 9 |
def obj_and_tuple_from_h_array(handles, h_args, n_minus_1): |
| 10 |
args_w = [None] * n_minus_1 |
| 11 |
for i in range(n_minus_1): |
| 12 |
args_w[i] = handles.deref(h_args[i + 1]) |
| 13 |
return handles.deref(h_args[0]), args_w |
| 14 |
|
| 15 |
@API.func("HPy HPy_CallTupleDict(HPyContext *ctx, HPy callable, HPy args, HPy kw)") |
| 16 |
def HPy_CallTupleDict(space, handles, ctx, h_callable, h_args, h_kw): |
| 17 |
w_callable = handles.deref(h_callable) |
| 18 |
w_args = handles.deref(h_args) if h_args else None |
| 19 |
w_kw = handles.deref(h_kw) if h_kw else None |
| 20 |
|
| 21 |
|
| 22 |
if w_args and not space.isinstance_w(w_args, space.w_tuple): |
| 23 |
raise oefmt(space.w_TypeError, |
| 24 |
"HPy_CallTupleDict requires args to be a tuple or null handle") |
| 25 |
if w_kw and not space.isinstance_w(w_kw, space.w_dict): |
| 26 |
raise oefmt(space.w_TypeError, |
| 27 |
"HPy_CallTupleDict requires kw to be a dict or null handle") |
| 28 |
|
| 29 |
|
| 30 |
w_result = space.call(w_callable, w_args, w_kw) |
| 31 |
return handles.new(w_result) |
| 32 |
|
| 33 |
@API.func("HPy HPy_CallMethod(HPyContext *ctx, HPy name, const HPy* args, size_t nargs, HPy kwnames)") |
| 34 |
def HPy_CallMethod(space, handles, ctx, h_name, h_argsp, nargs, h_kwargs): |
| 35 |
|
| 36 |
if not h_kwargs: |
| 37 |
n_kwargs = 0 |
| 38 |
w_kwargs = None |
| 39 |
else: |
| 40 |
w_kwargs = handles.deref(h_kwargs) |
| 41 |
n_kwargs = space.len_w(w_kwargs) |
| 42 |
w_name = handles.deref(h_name) |
| 43 |
n = Vectorcall_NARGS(nargs) |
| 44 |
n_minus_1 = n - 1 |
| 45 |
if n_minus_1 < 0: |
| 46 |
raise oefmt(space.w_ValueError, "n<1 in call to PyObject_VectorcallMethod") |
| 47 |
w_obj, args_w = obj_and_tuple_from_h_array(handles, h_argsp, n_minus_1 + n_kwargs) |
| 48 |
if w_kwargs is None: |
| 49 |
|
| 50 |
name = space.text_w(w_name) |
| 51 |
if n_minus_1 == 0: |
| 52 |
w_result = space.call_method(w_obj, name) |
| 53 |
elif n_minus_1 == 1: |
| 54 |
w_result = space.call_method(w_obj, name, args_w[0]) |
| 55 |
elif n_minus_1 == 2: |
| 56 |
w_result = space.call_method(w_obj, name, args_w[0], args_w[1]) |
| 57 |
elif n_minus_1 == 3: |
| 58 |
w_result = space.call_method(w_obj, name, args_w[0], args_w[1], args_w[2]) |
| 59 |
else: |
| 60 |
w_meth = space.getattr(w_obj, w_name) |
| 61 |
w_result = space.call(w_meth, space.newtuple(args_w)) |
| 62 |
else: |
| 63 |
w_dict = space.newdict() |
| 64 |
for i in range(n_kwargs): |
| 65 |
w_v = args_w[i + n_minus_1] |
| 66 |
w_k = space.getitem(w_kwargs, space.newint(i)) |
| 67 |
space.setitem(w_dict, w_k, w_v) |
| 68 |
w_meth = space.getattr(w_obj, w_name) |
| 69 |
w_result = space.call(w_meth, space.newtuple(args_w[:n_minus_1]), w_dict) |
| 70 |
return handles.new(w_result) |
| 71 |
|
| 72 |
|
| 73 |
@API.func("HPy HPy_Call(HPyContext *ctx, HPy callable, const HPy *args, size_t nargs, HPy kwnames)") |
| 74 |
def HPy_Call(space, handles, ctx, h_callable, h_argsp, nargs, h_kwargs): |
| 75 |
|
| 76 |
w_callable = handles.deref(h_callable) |
| 77 |
if not h_kwargs: |
| 78 |
n_kwargs = 0 |
| 79 |
w_kwargs = None |
| 80 |
else: |
| 81 |
w_kwargs = handles.deref(h_kwargs) |
| 82 |
n_kwargs = space.len_w(w_kwargs) |
| 83 |
n = Vectorcall_NARGS(nargs) |
| 84 |
args_w = [None] * (n + n_kwargs) |
| 85 |
for i in range(n + n_kwargs): |
| 86 |
args_w[i] = handles.deref(h_argsp[i]) |
| 87 |
if w_kwargs is None: |
| 88 |
w_result = space.call(w_callable, space.newtuple(args_w)) |
| 89 |
else: |
| 90 |
w_dict = space.newdict() |
| 91 |
for i in range(n_kwargs): |
| 92 |
w_v = args_w[i + n] |
| 93 |
w_k = space.getitem(w_kwargs, space.newint(i)) |
| 94 |
space.setitem(w_dict, w_k, w_v) |
| 95 |
w_result = space.call(w_callable, space.newtuple(args_w[:n]), w_dict) |
| 96 |
return handles.new(w_result) |
| 97 |
|
| 98 |
|