| 1 |
#include <Python.h> |
| 2 |
#include <structmember.h> |
| 3 |
|
| 4 |
#define USE_VECTORCALL_METHOD (PY_VERSION_HEX >= 0x03090000) |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
typedef struct { |
| 11 |
PyObject_HEAD |
| 12 |
PyObject *nested; |
| 13 |
} wrapper; |
| 14 |
|
| 15 |
PyObject* global_list = NULL; |
| 16 |
|
| 17 |
static int wrapper_init(PyObject *self, PyObject *args, PyObject *kwds) { |
| 18 |
PyList_Append(global_list, PyUnicode_FromString("wrapper tp_init called.")); |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
static void wrapper_dealloc(PyObject *self) { |
| 23 |
PyList_Append(global_list, PyUnicode_FromString("wrapper tp_dealloc called.")); |
| 24 |
PyObject_GC_UnTrack(self); |
| 25 |
PyTypeObject *tp = Py_TYPE(self); |
| 26 |
Py_CLEAR(((wrapper *) self)->nested); |
| 27 |
tp->tp_free(self); |
| 28 |
Py_DECREF(tp); |
| 29 |
} |
| 30 |
|
| 31 |
static int wrapper_traverse(PyObject *self, visitproc visit, void *arg) { |
| 32 |
PyList_Append(global_list, PyUnicode_FromString("wrapper tp_traverse called.")); |
| 33 |
Py_VISIT(((wrapper *) self)->nested); |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
static int wrapper_clear(PyObject *self) { |
| 38 |
PyList_Append(global_list, PyUnicode_FromString("wrapper tp_clear called.")); |
| 39 |
Py_CLEAR(((wrapper *) self)->nested); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
static PyMemberDef wrapper_members[] = { |
| 44 |
{ "nested", T_OBJECT, offsetof(wrapper, nested), 0, NULL }, |
| 45 |
{ NULL , 0, 0, 0, NULL } |
| 46 |
}; |
| 47 |
|
| 48 |
static PyType_Slot wrapper_slots[] = { |
| 49 |
{ Py_tp_init, wrapper_init }, |
| 50 |
{ Py_tp_dealloc, wrapper_dealloc }, |
| 51 |
{ Py_tp_traverse, wrapper_traverse }, |
| 52 |
{ Py_tp_clear, wrapper_clear }, |
| 53 |
{ Py_tp_members, wrapper_members }, |
| 54 |
{ 0, NULL } |
| 55 |
}; |
| 56 |
|
| 57 |
static PyType_Spec wrapper_spec = { |
| 58 |
.name = "nanobind2.wrapper", |
| 59 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 60 |
.slots = wrapper_slots, |
| 61 |
.basicsize = (int) sizeof(wrapper), |
| 62 |
.itemsize = 0 |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static int func_init(PyObject *self, PyObject *args, PyObject *kwds) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static PyObject *func_get_name(PyObject *self, void* unused) { |
| 74 |
return PyUnicode_FromString("my_name"); |
| 75 |
} |
| 76 |
|
| 77 |
static PyObject *func_get_qualname(PyObject *self, void* unused) { |
| 78 |
return PyUnicode_FromString("my_qualname"); |
| 79 |
} |
| 80 |
|
| 81 |
static PyObject *func_get_module(PyObject *self, void* unused) { |
| 82 |
return PyUnicode_FromString("my_module"); |
| 83 |
} |
| 84 |
|
| 85 |
static PyGetSetDef func_getset[] = { |
| 86 |
{ "__name__", func_get_name, NULL, NULL, NULL }, |
| 87 |
{ "__qualname__", func_get_qualname, NULL, NULL, NULL }, |
| 88 |
{ "__module__", func_get_module, NULL, NULL, NULL }, |
| 89 |
{ NULL, NULL, NULL, NULL, NULL } |
| 90 |
}; |
| 91 |
|
| 92 |
static PyType_Slot func_slots[] = { |
| 93 |
{ Py_tp_init, func_init }, |
| 94 |
{ Py_tp_getset, func_getset }, |
| 95 |
{ 0, NULL } |
| 96 |
}; |
| 97 |
|
| 98 |
static PyType_Spec func_spec = { |
| 99 |
.name = "nanobind2.func", |
| 100 |
.flags = Py_TPFLAGS_DEFAULT, |
| 101 |
.slots = func_slots, |
| 102 |
.basicsize = (int) sizeof(PyObject), |
| 103 |
.itemsize = 0 |
| 104 |
}; |
| 105 |
|
| 106 |
#if USE_VECTORCALL_METHOD == 1 |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
static PyObject* method_call(PyObject* self, PyObject* arg) { |
| 112 |
PyObject *value = PyLong_FromLong(1234); |
| 113 |
if (!value) |
| 114 |
return NULL; |
| 115 |
|
| 116 |
PyObject *name = PyUnicode_FromString("my_method"); |
| 117 |
if (!name) { |
| 118 |
Py_DECREF(value); |
| 119 |
return NULL; |
| 120 |
} |
| 121 |
|
| 122 |
PyObject *args[2] = { arg, value }; |
| 123 |
size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET; |
| 124 |
|
| 125 |
PyObject *result = PyObject_VectorcallMethod( |
| 126 |
name, args, nargsf, NULL); |
| 127 |
|
| 128 |
Py_DECREF(value); |
| 129 |
Py_DECREF(name); |
| 130 |
|
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
static PyObject* method_call_kw(PyObject* self, PyObject* arg) { |
| 135 |
PyObject *value = NULL, |
| 136 |
*name = NULL, |
| 137 |
*kwnames = NULL, |
| 138 |
*kw_name = NULL, |
| 139 |
*kw_value = NULL, |
| 140 |
*result = NULL; |
| 141 |
|
| 142 |
value = PyLong_FromLong(1234); |
| 143 |
if (!value) |
| 144 |
goto leave; |
| 145 |
|
| 146 |
name = PyUnicode_FromString("my_method"); |
| 147 |
if (!name) |
| 148 |
goto leave; |
| 149 |
|
| 150 |
kwnames = PyTuple_New(1); |
| 151 |
if (!kwnames) |
| 152 |
goto leave; |
| 153 |
|
| 154 |
kw_name = PyUnicode_InternFromString("foo"); |
| 155 |
if (!kw_name) |
| 156 |
goto leave; |
| 157 |
PyTuple_SET_ITEM(kwnames, 0, kw_name); |
| 158 |
|
| 159 |
kw_value = PyUnicode_FromString("bar"); |
| 160 |
if (!kw_value) |
| 161 |
goto leave; |
| 162 |
|
| 163 |
PyObject *args[3] = { arg, value, kw_value }; |
| 164 |
size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET; |
| 165 |
|
| 166 |
result = PyObject_VectorcallMethod( |
| 167 |
name, args, nargsf, kwnames); |
| 168 |
|
| 169 |
leave: |
| 170 |
Py_XDECREF(value); |
| 171 |
Py_XDECREF(name); |
| 172 |
Py_XDECREF(kwnames); |
| 173 |
Py_XDECREF(kw_value); |
| 174 |
|
| 175 |
return result; |
| 176 |
} |
| 177 |
|
| 178 |
struct PyMethodDef pypy_issues_methods[] = { |
| 179 |
{ "method_call", (PyCFunction) method_call, METH_O, NULL }, |
| 180 |
{ "method_call_kw", (PyCFunction) method_call_kw, METH_O, NULL }, |
| 181 |
{ NULL, NULL, 0, NULL}, |
| 182 |
}; |
| 183 |
#endif |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static PyModuleDef pypy_issues_module = { |
| 188 |
PyModuleDef_HEAD_INIT, |
| 189 |
.m_name = "nanobind2", |
| 190 |
.m_doc = "Reproducer for miscellaneous PyPy issues", |
| 191 |
.m_size = -1, |
| 192 |
#if USE_VECTORCALL_METHOD == 1 |
| 193 |
.m_methods = pypy_issues_methods |
| 194 |
#endif |
| 195 |
}; |
| 196 |
|
| 197 |
PyMODINIT_FUNC |
| 198 |
PyInit_nanobind2(void) |
| 199 |
{ |
| 200 |
PyObject *m = PyModule_Create(&pypy_issues_module); |
| 201 |
if (m == NULL) { |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
|
| 205 |
global_list = PyList_New(0); |
| 206 |
|
| 207 |
PyObject *wrapper = PyType_FromSpec(&wrapper_spec); |
| 208 |
|
| 209 |
if (!wrapper || PyModule_AddObject(m, "global_list", global_list) < 0) { |
| 210 |
Py_XDECREF(wrapper); |
| 211 |
Py_DECREF(m); |
| 212 |
return NULL; |
| 213 |
} |
| 214 |
|
| 215 |
if (!wrapper || PyModule_AddObject(m, "wrapper", wrapper) < 0) { |
| 216 |
Py_XDECREF(wrapper); |
| 217 |
Py_DECREF(m); |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
|
| 221 |
PyObject *func = PyType_FromSpec(&func_spec); |
| 222 |
|
| 223 |
if (!func || PyModule_AddObject(m, "func", func) < 0) { |
| 224 |
Py_XDECREF(func); |
| 225 |
Py_DECREF(m); |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
|
| 229 |
return m; |
| 230 |
} |