2023-09-25 08:24:19 -03:00
|
|
|
/* Test Vectorcall in the limited API */
|
|
|
|
|
2024-03-11 11:20:04 -03:00
|
|
|
// Need limited C API version 3.12 for PyObject_Vectorcall()
|
|
|
|
#include "pyconfig.h" // Py_GIL_DISABLED
|
|
|
|
#if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API )
|
|
|
|
# define Py_LIMITED_API 0x030c0000
|
|
|
|
#endif
|
|
|
|
|
2022-08-09 04:03:11 -03:00
|
|
|
#include "parts.h"
|
2023-09-25 08:24:19 -03:00
|
|
|
#include "clinic/vectorcall_limited.c.h"
|
2022-08-09 04:03:11 -03:00
|
|
|
|
2023-09-25 08:24:19 -03:00
|
|
|
/*[clinic input]
|
2024-03-07 14:31:12 -04:00
|
|
|
module _testlimitedcapi
|
2023-09-25 08:24:19 -03:00
|
|
|
[clinic start generated code]*/
|
2024-03-07 14:31:12 -04:00
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2700057f9c1135ba]*/
|
2022-08-08 09:12:05 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
LimitedVectorCallClass_tpcall(PyObject *self, PyObject *args, PyObject *kwargs) {
|
|
|
|
return PyUnicode_FromString("tp_call called");
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
LimitedVectorCallClass_vectorcall(PyObject *callable,
|
|
|
|
PyObject *const *args,
|
|
|
|
size_t nargsf,
|
|
|
|
PyObject *kwnames) {
|
|
|
|
return PyUnicode_FromString("vectorcall called");
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
LimitedVectorCallClass_new(PyTypeObject *tp, PyTypeObject *a, PyTypeObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *self = ((allocfunc)PyType_GetSlot(tp, Py_tp_alloc))(tp, 0);
|
|
|
|
if (!self) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*(vectorcallfunc*)((char*)self + sizeof(PyObject)) = (
|
|
|
|
LimitedVectorCallClass_vectorcall);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2023-09-25 08:24:19 -03:00
|
|
|
/*[clinic input]
|
2024-03-07 14:31:12 -04:00
|
|
|
_testlimitedcapi.call_vectorcall
|
2023-09-25 08:24:19 -03:00
|
|
|
|
|
|
|
callable: object
|
|
|
|
/
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2022-10-27 06:45:42 -03:00
|
|
|
static PyObject *
|
2024-03-07 14:31:12 -04:00
|
|
|
_testlimitedcapi_call_vectorcall(PyObject *module, PyObject *callable)
|
|
|
|
/*[clinic end generated code: output=9cbb7832263a8eef input=0743636c12dccb28]*/
|
2022-10-27 06:45:42 -03:00
|
|
|
{
|
|
|
|
PyObject *args[3] = { NULL, NULL, NULL };
|
|
|
|
PyObject *kwname = NULL, *kwnames = NULL, *result = NULL;
|
|
|
|
|
|
|
|
args[1] = PyUnicode_FromString("foo");
|
|
|
|
if (!args[1]) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
args[2] = PyUnicode_FromString("bar");
|
|
|
|
if (!args[2]) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
kwname = PyUnicode_InternFromString("baz");
|
|
|
|
if (!kwname) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
kwnames = PyTuple_New(1);
|
|
|
|
if (!kwnames) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyTuple_SetItem(kwnames, 0, kwname)) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = PyObject_Vectorcall(
|
|
|
|
callable,
|
|
|
|
args + 1,
|
|
|
|
1 | PY_VECTORCALL_ARGUMENTS_OFFSET,
|
|
|
|
kwnames
|
|
|
|
);
|
|
|
|
|
|
|
|
leave:
|
|
|
|
Py_XDECREF(args[1]);
|
|
|
|
Py_XDECREF(args[2]);
|
|
|
|
Py_XDECREF(kwnames);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-25 08:24:19 -03:00
|
|
|
/*[clinic input]
|
2024-03-07 14:31:12 -04:00
|
|
|
_testlimitedcapi.call_vectorcall_method
|
2023-09-25 08:24:19 -03:00
|
|
|
|
|
|
|
callable: object
|
|
|
|
/
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2022-10-27 06:45:42 -03:00
|
|
|
static PyObject *
|
2024-03-07 14:31:12 -04:00
|
|
|
_testlimitedcapi_call_vectorcall_method(PyObject *module, PyObject *callable)
|
|
|
|
/*[clinic end generated code: output=4558323a46cc09eb input=a736f7dbf15f1be5]*/
|
2022-10-27 06:45:42 -03:00
|
|
|
{
|
|
|
|
PyObject *args[3] = { NULL, NULL, NULL };
|
|
|
|
PyObject *name = NULL, *kwname = NULL,
|
|
|
|
*kwnames = NULL, *result = NULL;
|
|
|
|
|
|
|
|
name = PyUnicode_FromString("f");
|
|
|
|
if (!name) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
args[0] = callable;
|
|
|
|
args[1] = PyUnicode_FromString("foo");
|
|
|
|
if (!args[1]) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
args[2] = PyUnicode_FromString("bar");
|
|
|
|
if (!args[2]) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
kwname = PyUnicode_InternFromString("baz");
|
|
|
|
if (!kwname) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
kwnames = PyTuple_New(1);
|
|
|
|
if (!kwnames) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyTuple_SetItem(kwnames, 0, kwname)) {
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result = PyObject_VectorcallMethod(
|
|
|
|
name,
|
|
|
|
args,
|
|
|
|
2 | PY_VECTORCALL_ARGUMENTS_OFFSET,
|
|
|
|
kwnames
|
|
|
|
);
|
|
|
|
|
|
|
|
leave:
|
|
|
|
Py_XDECREF(name);
|
|
|
|
Py_XDECREF(args[1]);
|
|
|
|
Py_XDECREF(args[2]);
|
|
|
|
Py_XDECREF(kwnames);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:12:05 -03:00
|
|
|
static PyMemberDef LimitedVectorCallClass_members[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"__vectorcalloffset__", Py_T_PYSSIZET, sizeof(PyObject), Py_READONLY},
|
2022-08-08 09:12:05 -03:00
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Slot LimitedVectorallClass_slots[] = {
|
|
|
|
{Py_tp_new, LimitedVectorCallClass_new},
|
|
|
|
{Py_tp_call, LimitedVectorCallClass_tpcall},
|
|
|
|
{Py_tp_members, LimitedVectorCallClass_members},
|
|
|
|
{0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec LimitedVectorCallClass_spec = {
|
2024-03-07 14:31:12 -04:00
|
|
|
.name = "_testlimitedcapi.LimitedVectorCallClass",
|
2022-08-08 09:12:05 -03:00
|
|
|
.basicsize = (int)(sizeof(PyObject) + sizeof(vectorcallfunc)),
|
|
|
|
.flags = Py_TPFLAGS_DEFAULT
|
|
|
|
| Py_TPFLAGS_HAVE_VECTORCALL
|
|
|
|
| Py_TPFLAGS_BASETYPE,
|
|
|
|
.slots = LimitedVectorallClass_slots,
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMethodDef TestMethods[] = {
|
2024-03-07 14:31:12 -04:00
|
|
|
_TESTLIMITEDCAPI_CALL_VECTORCALL_METHODDEF
|
|
|
|
_TESTLIMITEDCAPI_CALL_VECTORCALL_METHOD_METHODDEF
|
2022-08-08 09:12:05 -03:00
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyTestCapi_Init_VectorcallLimited(PyObject *m) {
|
|
|
|
if (PyModule_AddFunctions(m, TestMethods) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *LimitedVectorCallClass = PyType_FromModuleAndSpec(
|
|
|
|
m, &LimitedVectorCallClass_spec, NULL);
|
|
|
|
if (!LimitedVectorCallClass) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyModule_AddType(m, (PyTypeObject *)LimitedVectorCallClass) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-01-08 12:34:51 -04:00
|
|
|
Py_DECREF(LimitedVectorCallClass);
|
2022-08-08 09:12:05 -03:00
|
|
|
return 0;
|
|
|
|
}
|