File: interp_call.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (98 lines) | stat: -rw-r--r-- 4,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from rpython.rtyper.lltypesystem import lltype, rffi
from pypy.interpreter.error import oefmt
from .apiset import API

def Vectorcall_NARGS(n):
    PY_VECTORCALL_ARGUMENTS_OFFSET = 1 << (8 * rffi.sizeof(rffi.SIZE_T) - 1)
    return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET

def obj_and_tuple_from_h_array(handles, h_args, n_minus_1):
    args_w = [None] * n_minus_1
    for i in range(n_minus_1):
        args_w[i] = handles.deref(h_args[i + 1])
    return handles.deref(h_args[0]), args_w

@API.func("HPy HPy_CallTupleDict(HPyContext *ctx, HPy callable, HPy args, HPy kw)")
def HPy_CallTupleDict(space, handles, ctx, h_callable, h_args, h_kw):
    w_callable = handles.deref(h_callable)
    w_args = handles.deref(h_args) if h_args else None
    w_kw = handles.deref(h_kw) if h_kw else None

    # Check the types here, as space.call would allow any iterable/mapping
    if w_args and not space.isinstance_w(w_args, space.w_tuple):
        raise oefmt(space.w_TypeError,
            "HPy_CallTupleDict requires args to be a tuple or null handle")
    if w_kw and not space.isinstance_w(w_kw, space.w_dict):
        raise oefmt(space.w_TypeError,
            "HPy_CallTupleDict requires kw to be a dict or null handle")

    # Note: both w_args and w_kw are allowed to be None
    w_result = space.call(w_callable, w_args, w_kw)
    return handles.new(w_result)

@API.func("HPy HPy_CallMethod(HPyContext *ctx, HPy name, const HPy* args, size_t nargs, HPy kwnames)")
def HPy_CallMethod(space, handles, ctx, h_name, h_argsp, nargs, h_kwargs):
    # h_kwargs is a list of names, h_argsp has nargs plus len(h_kwargs) values
    if not h_kwargs:
        n_kwargs = 0
        w_kwargs = None
    else:
        w_kwargs = handles.deref(h_kwargs)
        n_kwargs = space.len_w(w_kwargs)
    w_name = handles.deref(h_name)
    n = Vectorcall_NARGS(nargs)
    n_minus_1 = n - 1
    if n_minus_1 < 0:
        raise oefmt(space.w_ValueError, "n<1 in call to PyObject_VectorcallMethod")
    w_obj, args_w = obj_and_tuple_from_h_array(handles, h_argsp, n_minus_1 + n_kwargs)
    if w_kwargs is None:
        # fast path. Cannot use call_method(... *args_w)
        name = space.text_w(w_name)
        if n_minus_1 == 0:
            w_result = space.call_method(w_obj, name)
        elif n_minus_1 == 1:
            w_result = space.call_method(w_obj, name, args_w[0])
        elif n_minus_1 == 2:
            w_result = space.call_method(w_obj, name, args_w[0], args_w[1])
        elif n_minus_1 == 3:
            w_result = space.call_method(w_obj, name, args_w[0], args_w[1], args_w[2])
        else:
            w_meth = space.getattr(w_obj, w_name)
            w_result = space.call(w_meth, space.newtuple(args_w))
    else:
        w_dict = space.newdict()
        for i in range(n_kwargs):
            w_v =  args_w[i + n_minus_1]
            w_k = space.getitem(w_kwargs, space.newint(i))
            space.setitem(w_dict, w_k, w_v)
        w_meth = space.getattr(w_obj, w_name)
        w_result = space.call(w_meth, space.newtuple(args_w[:n_minus_1]), w_dict)
    return handles.new(w_result)


@API.func("HPy HPy_Call(HPyContext *ctx, HPy callable, const HPy *args, size_t nargs, HPy kwnames)")
def HPy_Call(space, handles, ctx, h_callable, h_argsp, nargs, h_kwargs):
    # h_kwargs is a list of names
    w_callable = handles.deref(h_callable)
    if not h_kwargs:
        n_kwargs = 0
        w_kwargs = None
    else:
        w_kwargs = handles.deref(h_kwargs)
        n_kwargs = space.len_w(w_kwargs)
    n = Vectorcall_NARGS(nargs)
    args_w = [None] * (n + n_kwargs)
    for i in range(n + n_kwargs):
        args_w[i] = handles.deref(h_argsp[i])
    if w_kwargs is None:
        w_result = space.call(w_callable, space.newtuple(args_w))
    else:
        w_dict = space.newdict()
        for i in range(n_kwargs):
            w_v =  args_w[i + n]
            w_k = space.getitem(w_kwargs, space.newint(i))
            space.setitem(w_dict, w_k, w_v)
        w_result = space.call(w_callable, space.newtuple(args_w[:n]), w_dict)
    return handles.new(w_result)