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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#define Py_LIMITED_API 0x030c0000 // 3.12
#include "parts.h"
#ifdef LIMITED_API_AVAILABLE
#include "structmember.h" // PyMemberDef
/* Test Vectorcall in the limited API */
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;
}
static PyObject *
call_vectorcall(PyObject* self, PyObject *callable)
{
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;
}
static PyObject *
call_vectorcall_method(PyObject* self, PyObject *callable)
{
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;
}
static PyMemberDef LimitedVectorCallClass_members[] = {
{"__vectorcalloffset__", T_PYSSIZET, sizeof(PyObject), READONLY},
{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 = {
.name = "_testcapi.LimitedVectorCallClass",
.basicsize = (int)(sizeof(PyObject) + sizeof(vectorcallfunc)),
.flags = Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_VECTORCALL
| Py_TPFLAGS_BASETYPE,
.slots = LimitedVectorallClass_slots,
};
static PyMethodDef TestMethods[] = {
{"call_vectorcall", call_vectorcall, METH_O},
{"call_vectorcall_method", call_vectorcall_method, METH_O},
{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;
}
return 0;
}
#endif // LIMITED_API_AVAILABLE
|