|
| 1 | +#define Py_LIMITED_API 0x030c0000 // 3.12 |
| 2 | +#include "parts.h" |
| 3 | +#include "structmember.h" // PyMemberDef |
| 4 | + |
| 5 | +/* Test Vectorcall in the limited API */ |
| 6 | + |
| 7 | +static PyObject * |
| 8 | +LimitedVectorCallClass_tpcall(PyObject *self, PyObject *args, PyObject *kwargs) { |
| 9 | + return PyUnicode_FromString("tp_call called"); |
| 10 | +} |
| 11 | + |
| 12 | +static PyObject * |
| 13 | +LimitedVectorCallClass_vectorcall(PyObject *callable, |
| 14 | + PyObject *const *args, |
| 15 | + size_t nargsf, |
| 16 | + PyObject *kwnames) { |
| 17 | + return PyUnicode_FromString("vectorcall called"); |
| 18 | +} |
| 19 | + |
| 20 | +static PyObject * |
| 21 | +LimitedVectorCallClass_new(PyTypeObject *tp, PyTypeObject *a, PyTypeObject *kw) |
| 22 | +{ |
| 23 | + PyObject *self = ((allocfunc)PyType_GetSlot(tp, Py_tp_alloc))(tp, 0); |
| 24 | + if (!self) { |
| 25 | + return NULL; |
| 26 | + } |
| 27 | + *(vectorcallfunc*)((char*)self + sizeof(PyObject)) = ( |
| 28 | + LimitedVectorCallClass_vectorcall); |
| 29 | + return self; |
| 30 | +} |
| 31 | + |
| 32 | +static PyMemberDef LimitedVectorCallClass_members[] = { |
| 33 | + {"__vectorcalloffset__", T_PYSSIZET, sizeof(PyObject), READONLY}, |
| 34 | + {NULL} |
| 35 | +}; |
| 36 | + |
| 37 | +static PyType_Slot LimitedVectorallClass_slots[] = { |
| 38 | + {Py_tp_new, LimitedVectorCallClass_new}, |
| 39 | + {Py_tp_call, LimitedVectorCallClass_tpcall}, |
| 40 | + {Py_tp_members, LimitedVectorCallClass_members}, |
| 41 | + {0}, |
| 42 | +}; |
| 43 | + |
| 44 | +static PyType_Spec LimitedVectorCallClass_spec = { |
| 45 | + .name = "_testcapi.LimitedVectorCallClass", |
| 46 | + .basicsize = (int)(sizeof(PyObject) + sizeof(vectorcallfunc)), |
| 47 | + .flags = Py_TPFLAGS_DEFAULT |
| 48 | + | Py_TPFLAGS_HAVE_VECTORCALL |
| 49 | + | Py_TPFLAGS_BASETYPE, |
| 50 | + .slots = LimitedVectorallClass_slots, |
| 51 | +}; |
| 52 | + |
| 53 | +static PyMethodDef TestMethods[] = { |
| 54 | + /* Add module methods here. |
| 55 | + * (Empty list left here as template/example, since using |
| 56 | + * PyModule_AddFunctions isn't very common.) |
| 57 | + */ |
| 58 | + {NULL}, |
| 59 | +}; |
| 60 | + |
| 61 | +int |
| 62 | +_PyTestCapi_Init_VectorcallLimited(PyObject *m) { |
| 63 | + if (PyModule_AddFunctions(m, TestMethods) < 0) { |
| 64 | + return -1; |
| 65 | + } |
| 66 | + |
| 67 | + PyObject *LimitedVectorCallClass = PyType_FromModuleAndSpec( |
| 68 | + m, &LimitedVectorCallClass_spec, NULL); |
| 69 | + if (!LimitedVectorCallClass) { |
| 70 | + return -1; |
| 71 | + } |
| 72 | + if (PyModule_AddType(m, (PyTypeObject *)LimitedVectorCallClass) < 0) { |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
0 commit comments