| 1 |
/* Test Vectorcall in the limited API */ |
| 2 |
|
| 3 |
// Need limited C API version 3.12 for PyObject_Vectorcall() |
| 4 |
#include "pyconfig.h" // Py_GIL_DISABLED |
| 5 |
#if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API) |
| 6 |
# define Py_LIMITED_API 0x030c0000 |
| 7 |
#endif |
| 8 |
|
| 9 |
#include <stddef.h> // offsetof |
| 10 |
|
| 11 |
#include "parts.h" |
| 12 |
#include "clinic/vectorcall_limited.c.h" |
| 13 |
|
| 14 |
/*[clinic input] |
| 15 |
module _testlimitedcapi |
| 16 |
[clinic start generated code]*/ |
| 17 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2700057f9c1135ba]*/ |
| 18 |
|
| 19 |
static PyObject * |
| 20 |
LimitedVectorCallClass_tpcall(PyObject *self, PyObject *args, PyObject *kwargs) { |
| 21 |
return PyUnicode_FromString("tp_call called"); |
| 22 |
} |
| 23 |
|
| 24 |
static PyObject * |
| 25 |
LimitedVectorCallClass_vectorcall(PyObject *callable, |
| 26 |
PyObject *const *args, |
| 27 |
size_t nargsf, |
| 28 |
PyObject *kwnames) { |
| 29 |
return PyUnicode_FromString("vectorcall called"); |
| 30 |
} |
| 31 |
|
| 32 |
static PyObject * |
| 33 |
LimitedVectorCallClass_new(PyTypeObject *tp, PyObject *a, PyObject *kw) |
| 34 |
{ |
| 35 |
PyObject *self = ((allocfunc)PyType_GetSlot(tp, Py_tp_alloc))(tp, 0); |
| 36 |
if (!self) { |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
*(vectorcallfunc*)((char*)self + sizeof(PyObject)) = ( |
| 40 |
LimitedVectorCallClass_vectorcall); |
| 41 |
return self; |
| 42 |
} |
| 43 |
|
| 44 |
/*[clinic input] |
| 45 |
_testlimitedcapi.call_vectorcall |
| 46 |
|
| 47 |
callable: object |
| 48 |
/ |
| 49 |
[clinic start generated code]*/ |
| 50 |
|
| 51 |
static PyObject * |
| 52 |
_testlimitedcapi_call_vectorcall(PyObject *module, PyObject *callable) |
| 53 |
/*[clinic end generated code: output=9cbb7832263a8eef input=0743636c12dccb28]*/ |
| 54 |
{ |
| 55 |
PyObject *args[3] = { NULL, NULL, NULL }; |
| 56 |
PyObject *kwname = NULL, *kwnames = NULL, *result = NULL; |
| 57 |
|
| 58 |
args[1] = PyUnicode_FromString("foo"); |
| 59 |
if (!args[1]) { |
| 60 |
goto leave; |
| 61 |
} |
| 62 |
|
| 63 |
args[2] = PyUnicode_FromString("bar"); |
| 64 |
if (!args[2]) { |
| 65 |
goto leave; |
| 66 |
} |
| 67 |
|
| 68 |
kwname = PyUnicode_InternFromString("baz"); |
| 69 |
if (!kwname) { |
| 70 |
goto leave; |
| 71 |
} |
| 72 |
|
| 73 |
kwnames = PyTuple_New(1); |
| 74 |
if (!kwnames) { |
| 75 |
goto leave; |
| 76 |
} |
| 77 |
|
| 78 |
if (PyTuple_SetItem(kwnames, 0, kwname)) { |
| 79 |
goto leave; |
| 80 |
} |
| 81 |
|
| 82 |
result = PyObject_Vectorcall( |
| 83 |
callable, |
| 84 |
args + 1, |
| 85 |
1 | PY_VECTORCALL_ARGUMENTS_OFFSET, |
| 86 |
kwnames |
| 87 |
); |
| 88 |
|
| 89 |
leave: |
| 90 |
Py_XDECREF(args[1]); |
| 91 |
Py_XDECREF(args[2]); |
| 92 |
Py_XDECREF(kwnames); |
| 93 |
|
| 94 |
return result; |
| 95 |
} |
| 96 |
|
| 97 |
/*[clinic input] |
| 98 |
_testlimitedcapi.call_vectorcall_method |
| 99 |
|
| 100 |
callable: object |
| 101 |
/ |
| 102 |
[clinic start generated code]*/ |
| 103 |
|
| 104 |
static PyObject * |
| 105 |
_testlimitedcapi_call_vectorcall_method(PyObject *module, PyObject *callable) |
| 106 |
/*[clinic end generated code: output=4558323a46cc09eb input=a736f7dbf15f1be5]*/ |
| 107 |
{ |
| 108 |
PyObject *args[3] = { NULL, NULL, NULL }; |
| 109 |
PyObject *name = NULL, *kwname = NULL, |
| 110 |
*kwnames = NULL, *result = NULL; |
| 111 |
|
| 112 |
name = PyUnicode_FromString("f"); |
| 113 |
if (!name) { |
| 114 |
goto leave; |
| 115 |
} |
| 116 |
|
| 117 |
args[0] = callable; |
| 118 |
args[1] = PyUnicode_FromString("foo"); |
| 119 |
if (!args[1]) { |
| 120 |
goto leave; |
| 121 |
} |
| 122 |
|
| 123 |
args[2] = PyUnicode_FromString("bar"); |
| 124 |
if (!args[2]) { |
| 125 |
goto leave; |
| 126 |
} |
| 127 |
|
| 128 |
kwname = PyUnicode_InternFromString("baz"); |
| 129 |
if (!kwname) { |
| 130 |
goto leave; |
| 131 |
} |
| 132 |
|
| 133 |
kwnames = PyTuple_New(1); |
| 134 |
if (!kwnames) { |
| 135 |
goto leave; |
| 136 |
} |
| 137 |
|
| 138 |
if (PyTuple_SetItem(kwnames, 0, kwname)) { |
| 139 |
goto leave; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
result = PyObject_VectorcallMethod( |
| 144 |
name, |
| 145 |
args, |
| 146 |
2 | PY_VECTORCALL_ARGUMENTS_OFFSET, |
| 147 |
kwnames |
| 148 |
); |
| 149 |
|
| 150 |
leave: |
| 151 |
Py_XDECREF(name); |
| 152 |
Py_XDECREF(args[1]); |
| 153 |
Py_XDECREF(args[2]); |
| 154 |
Py_XDECREF(kwnames); |
| 155 |
|
| 156 |
return result; |
| 157 |
} |
| 158 |
|
| 159 |
static PyMemberDef LimitedVectorCallClass_members[] = { |
| 160 |
{"__vectorcalloffset__", Py_T_PYSSIZET, sizeof(PyObject), Py_READONLY}, |
| 161 |
{NULL} |
| 162 |
}; |
| 163 |
|
| 164 |
static PyType_Slot LimitedVectorallClass_slots[] = { |
| 165 |
{Py_tp_new, LimitedVectorCallClass_new}, |
| 166 |
{Py_tp_call, LimitedVectorCallClass_tpcall}, |
| 167 |
{Py_tp_members, LimitedVectorCallClass_members}, |
| 168 |
{0}, |
| 169 |
}; |
| 170 |
|
| 171 |
static PyType_Spec LimitedVectorCallClass_spec = { |
| 172 |
.name = "_testlimitedcapi.LimitedVectorCallClass", |
| 173 |
.basicsize = (int)(sizeof(PyObject) + sizeof(vectorcallfunc)), |
| 174 |
.flags = Py_TPFLAGS_DEFAULT |
| 175 |
| Py_TPFLAGS_HAVE_VECTORCALL |
| 176 |
| Py_TPFLAGS_BASETYPE, |
| 177 |
.slots = LimitedVectorallClass_slots, |
| 178 |
}; |
| 179 |
|
| 180 |
typedef struct { |
| 181 |
vectorcallfunc vfunc; |
| 182 |
} LimitedRelativeVectorCallStruct; |
| 183 |
|
| 184 |
static PyObject * |
| 185 |
LimitedRelativeVectorCallClass_new(PyTypeObject *tp, PyObject *a, PyObject *kw) |
| 186 |
{ |
| 187 |
PyObject *self = ((allocfunc)PyType_GetSlot(tp, Py_tp_alloc))(tp, 0); |
| 188 |
if (!self) { |
| 189 |
return NULL; |
| 190 |
} |
| 191 |
LimitedRelativeVectorCallStruct *data = PyObject_GetTypeData(self, tp); |
| 192 |
data->vfunc = LimitedVectorCallClass_vectorcall; |
| 193 |
return self; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
static PyType_Spec LimitedRelativeVectorCallClass_spec = { |
| 198 |
.name = "_testlimitedcapi.LimitedRelativeVectorCallClass", |
| 199 |
.basicsize = -(int)sizeof(LimitedRelativeVectorCallStruct), |
| 200 |
.flags = Py_TPFLAGS_DEFAULT |
| 201 |
| Py_TPFLAGS_HAVE_VECTORCALL, |
| 202 |
.slots = (PyType_Slot[]) { |
| 203 |
{Py_tp_new, LimitedRelativeVectorCallClass_new}, |
| 204 |
{Py_tp_call, LimitedVectorCallClass_tpcall}, |
| 205 |
{Py_tp_members, (PyMemberDef[]){ |
| 206 |
{"__vectorcalloffset__", Py_T_PYSSIZET, |
| 207 |
offsetof(LimitedRelativeVectorCallStruct, vfunc), |
| 208 |
Py_READONLY | Py_RELATIVE_OFFSET}, |
| 209 |
{NULL} |
| 210 |
}}, |
| 211 |
{0} |
| 212 |
}, |
| 213 |
}; |
| 214 |
|
| 215 |
static PyMethodDef TestMethods[] = { |
| 216 |
_TESTLIMITEDCAPI_CALL_VECTORCALL_METHODDEF |
| 217 |
_TESTLIMITEDCAPI_CALL_VECTORCALL_METHOD_METHODDEF |
| 218 |
{NULL}, |
| 219 |
}; |
| 220 |
|
| 221 |
int |
| 222 |
_PyTestLimitedCAPI_Init_VectorcallLimited(PyObject *m) |
| 223 |
{ |
| 224 |
if (PyModule_AddFunctions(m, TestMethods) < 0) { |
| 225 |
return -1; |
| 226 |
} |
| 227 |
|
| 228 |
PyObject *LimitedVectorCallClass = PyType_FromModuleAndSpec( |
| 229 |
m, &LimitedVectorCallClass_spec, NULL); |
| 230 |
if (!LimitedVectorCallClass) { |
| 231 |
return -1; |
| 232 |
} |
| 233 |
if (PyModule_AddType(m, (PyTypeObject *)LimitedVectorCallClass) < 0) { |
| 234 |
return -1; |
| 235 |
} |
| 236 |
Py_DECREF(LimitedVectorCallClass); |
| 237 |
|
| 238 |
PyObject *LimitedRelativeVectorCallClass = PyType_FromModuleAndSpec( |
| 239 |
m, &LimitedRelativeVectorCallClass_spec, NULL); |
| 240 |
if (!LimitedRelativeVectorCallClass) { |
| 241 |
return -1; |
| 242 |
} |
| 243 |
if (PyModule_AddType(m, (PyTypeObject *)LimitedRelativeVectorCallClass) < 0) { |
| 244 |
return -1; |
| 245 |
} |
| 246 |
Py_DECREF(LimitedRelativeVectorCallClass); |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |