Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/ctx_call.c

raw symbols SCIP
1 #include <Python.h>
2 #include "hpy.h"
3 #if defined(_MSC_VER)
4 # include <malloc.h> /* for alloca() */
5 #endif
6
7 #ifndef HPY_ABI_CPYTHON
8 // for _h2py and _py2h
9 # include "handles.h"
10 #endif
11
12 _HPy_HIDDEN HPy
13 ctx_CallTupleDict(HPyContext *ctx, HPy callable, HPy args, HPy kw)
14 {
15 PyObject *obj;
16 if (!HPy_IsNull(args) && !HPyTuple_Check(ctx, args)) {
17 HPyErr_SetString(ctx, ctx->h_TypeError,
18 "HPy_CallTupleDict requires args to be a tuple or null handle");
19 return HPy_NULL;
20 }
21 if (!HPy_IsNull(kw) && !HPyDict_Check(ctx, kw)) {
22 HPyErr_SetString(ctx, ctx->h_TypeError,
23 "HPy_CallTupleDict requires kw to be a dict or null handle");
24 return HPy_NULL;
25 }
26 if (HPy_IsNull(kw)) {
27 obj = PyObject_CallObject(_h2py(callable), _h2py(args));
28 }
29 else if (!HPy_IsNull(args)){
30 obj = PyObject_Call(_h2py(callable), _h2py(args), _h2py(kw));
31 }
32 else {
33 // args is null, but kw is not, so we need to create an empty args tuple
34 // for CPython's PyObject_Call
35 HPy *items = NULL;
36 HPy empty_tuple = HPyTuple_FromArray(ctx, items, 0);
37 obj = PyObject_Call(_h2py(callable), _h2py(empty_tuple), _h2py(kw));
38 HPy_Close(ctx, empty_tuple);
39 }
40 return _py2h(obj);
41 }
42
43 #if PY_VERSION_HEX < 0x03090000
44 # define PyObject_Vectorcall _PyObject_Vectorcall
45 #endif
46
47 _HPy_HIDDEN HPy
48 ctx_Call(HPyContext *ctx, HPy h_callable, const HPy *h_args, size_t nargs, HPy h_kwnames)
49 {
50 PyObject *kwnames;
51 size_t n_all_args;
52
53 if (HPy_IsNull(h_kwnames)) {
54 kwnames = NULL;
55 n_all_args = nargs;
56 } else {
57 kwnames = _h2py(h_kwnames);
58 assert(kwnames != NULL);
59 assert(PyTuple_Check(kwnames));
60 n_all_args = nargs + PyTuple_GET_SIZE(kwnames);
61 assert(n_all_args >= nargs);
62 }
63
64 /* Since we already allocate a fresh args array, we make it one element
65 larger and set PY_VECTORCALL_ARGUMENTS_OFFSET to avoid further
66 allocations from CPython. */
67 PyObject **args = (PyObject **) alloca((n_all_args + 1) * sizeof(PyObject *));
68 for (size_t i = 0; i < n_all_args; i++) {
69 args[i+1] = _h2py(h_args[i]);
70 }
71
72 return _py2h(PyObject_Vectorcall(_h2py(h_callable), args+1,
73 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames));
74 }
75
76 #if PY_VERSION_HEX < 0x03090000
77 # undef PyObject_Vectorcall
78 #endif
79
80 _HPy_HIDDEN HPy
81 ctx_CallMethod(HPyContext *ctx, HPy h_name, const HPy *h_args, size_t nargs,
82 HPy h_kwnames)
83 {
84 PyObject *result, *kwnames;
85 size_t n_all_args;
86
87 if (HPy_IsNull(h_kwnames)) {
88 kwnames = NULL;
89 n_all_args = nargs;
90 } else {
91 kwnames = _h2py(h_kwnames);
92 assert(kwnames != NULL);
93 assert(PyTuple_Check(kwnames));
94 n_all_args = nargs + PyTuple_GET_SIZE(kwnames);
95 assert(n_all_args >= nargs);
96 }
97
98 /* Since we already allocate a fresh args array, we make it one element
99 larger and set PY_VECTORCALL_ARGUMENTS_OFFSET to avoid further
100 allocations from CPython. */
101 PyObject **args = (PyObject **) alloca(
102 (n_all_args + 1) * sizeof(PyObject *));
103 for (size_t i = 0; i < n_all_args; i++) {
104 args[i+1] = _h2py(h_args[i]);
105 }
106
107 #if PY_VERSION_HEX < 0x03090000
108 PyObject *method = PyObject_GetAttr(args[1], _h2py(h_name));
109 if (method == NULL)
110 return HPy_NULL;
111 result = _PyObject_Vectorcall(method, &args[2],
112 (nargs-1) | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
113 Py_DECREF(method);
114 #else
115 result = PyObject_VectorcallMethod(_h2py(h_name), args+1,
116 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
117 #endif
118 return _py2h(result);
119 }