| 1 |
// Need limited C API version 3.12 for PyType_FromMetaclass() |
| 2 |
#include "pyconfig.h" // Py_GIL_DISABLED |
| 3 |
#if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API) |
| 4 |
# define Py_LIMITED_API 0x030c0000 |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "parts.h" |
| 8 |
#include <stddef.h> // max_align_t |
| 9 |
#include <string.h> // memset |
| 10 |
|
| 11 |
#include "clinic/heaptype_relative.c.h" |
| 12 |
|
| 13 |
static PyType_Slot empty_slots[] = { |
| 14 |
{0, NULL}, |
| 15 |
}; |
| 16 |
|
| 17 |
static PyObject * |
| 18 |
make_sized_heaptypes(PyObject *module, PyObject *args) |
| 19 |
{ |
| 20 |
PyObject *base = NULL; |
| 21 |
PyObject *sub = NULL; |
| 22 |
PyObject *instance = NULL; |
| 23 |
PyObject *result = NULL; |
| 24 |
|
| 25 |
int extra_base_size, basicsize; |
| 26 |
|
| 27 |
int r = PyArg_ParseTuple(args, "ii", &extra_base_size, &basicsize); |
| 28 |
if (!r) { |
| 29 |
goto finally; |
| 30 |
} |
| 31 |
|
| 32 |
PyType_Spec base_spec = { |
| 33 |
.name = "_testcapi.Base", |
| 34 |
.basicsize = sizeof(PyObject) + extra_base_size, |
| 35 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 36 |
.slots = empty_slots, |
| 37 |
}; |
| 38 |
PyType_Spec sub_spec = { |
| 39 |
.name = "_testcapi.Sub", |
| 40 |
.basicsize = basicsize, |
| 41 |
.flags = Py_TPFLAGS_DEFAULT, |
| 42 |
.slots = empty_slots, |
| 43 |
}; |
| 44 |
|
| 45 |
base = PyType_FromMetaclass(NULL, module, &base_spec, NULL); |
| 46 |
if (!base) { |
| 47 |
goto finally; |
| 48 |
} |
| 49 |
sub = PyType_FromMetaclass(NULL, module, &sub_spec, base); |
| 50 |
if (!sub) { |
| 51 |
goto finally; |
| 52 |
} |
| 53 |
instance = PyObject_CallNoArgs(sub); |
| 54 |
if (!instance) { |
| 55 |
goto finally; |
| 56 |
} |
| 57 |
char *data_ptr = PyObject_GetTypeData(instance, (PyTypeObject *)sub); |
| 58 |
if (!data_ptr) { |
| 59 |
goto finally; |
| 60 |
} |
| 61 |
Py_ssize_t data_size = PyType_GetTypeDataSize((PyTypeObject *)sub); |
| 62 |
if (data_size < 0) { |
| 63 |
goto finally; |
| 64 |
} |
| 65 |
|
| 66 |
result = Py_BuildValue("OOOKnn", base, sub, instance, |
| 67 |
(unsigned long long)data_ptr, |
| 68 |
(Py_ssize_t)(data_ptr - (char*)instance), |
| 69 |
data_size); |
| 70 |
finally: |
| 71 |
Py_XDECREF(base); |
| 72 |
Py_XDECREF(sub); |
| 73 |
Py_XDECREF(instance); |
| 74 |
return result; |
| 75 |
} |
| 76 |
|
| 77 |
static PyObject * |
| 78 |
var_heaptype_set_data_to_3s( |
| 79 |
PyObject *self, PyTypeObject *defining_class, |
| 80 |
PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 81 |
{ |
| 82 |
void *data_ptr = PyObject_GetTypeData(self, defining_class); |
| 83 |
if (!data_ptr) { |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
Py_ssize_t data_size = PyType_GetTypeDataSize(defining_class); |
| 87 |
if (data_size < 0) { |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
memset(data_ptr, 3, data_size); |
| 91 |
Py_RETURN_NONE; |
| 92 |
} |
| 93 |
|
| 94 |
static PyObject * |
| 95 |
var_heaptype_get_data(PyObject *self, PyTypeObject *defining_class, |
| 96 |
PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 97 |
{ |
| 98 |
void *data_ptr = PyObject_GetTypeData(self, defining_class); |
| 99 |
if (!data_ptr) { |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
Py_ssize_t data_size = PyType_GetTypeDataSize(defining_class); |
| 103 |
if (data_size < 0) { |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
return PyBytes_FromStringAndSize(data_ptr, data_size); |
| 107 |
} |
| 108 |
|
| 109 |
static PyMethodDef var_heaptype_methods[] = { |
| 110 |
{"set_data_to_3s", _PyCFunction_CAST(var_heaptype_set_data_to_3s), |
| 111 |
METH_METHOD | METH_FASTCALL | METH_KEYWORDS}, |
| 112 |
{"get_data", _PyCFunction_CAST(var_heaptype_get_data), |
| 113 |
METH_METHOD | METH_FASTCALL | METH_KEYWORDS}, |
| 114 |
{NULL}, |
| 115 |
}; |
| 116 |
|
| 117 |
static PyObject * |
| 118 |
subclass_var_heaptype(PyObject *module, PyObject *args) |
| 119 |
{ |
| 120 |
PyObject *result = NULL; |
| 121 |
|
| 122 |
PyObject *base; // borrowed from args |
| 123 |
int basicsize, itemsize; |
| 124 |
long pfunc; |
| 125 |
|
| 126 |
int r = PyArg_ParseTuple(args, "Oiil", &base, &basicsize, &itemsize, &pfunc); |
| 127 |
if (!r) { |
| 128 |
goto finally; |
| 129 |
} |
| 130 |
|
| 131 |
PyType_Slot slots[] = { |
| 132 |
{Py_tp_methods, var_heaptype_methods}, |
| 133 |
{0, NULL}, |
| 134 |
}; |
| 135 |
|
| 136 |
PyType_Spec sub_spec = { |
| 137 |
.name = "_testcapi.Sub", |
| 138 |
.basicsize = basicsize, |
| 139 |
.itemsize = itemsize, |
| 140 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_ITEMS_AT_END, |
| 141 |
.slots = slots, |
| 142 |
}; |
| 143 |
|
| 144 |
result = PyType_FromMetaclass(NULL, module, &sub_spec, base); |
| 145 |
finally: |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
static PyObject * |
| 150 |
subclass_heaptype(PyObject *module, PyObject *args) |
| 151 |
{ |
| 152 |
PyObject *result = NULL; |
| 153 |
|
| 154 |
PyObject *base; // borrowed from args |
| 155 |
int basicsize, itemsize; |
| 156 |
|
| 157 |
int r = PyArg_ParseTuple(args, "Oii", &base, &basicsize, &itemsize); |
| 158 |
if (!r) { |
| 159 |
goto finally; |
| 160 |
} |
| 161 |
|
| 162 |
PyType_Slot slots[] = { |
| 163 |
{Py_tp_methods, var_heaptype_methods}, |
| 164 |
{0, NULL}, |
| 165 |
}; |
| 166 |
|
| 167 |
PyType_Spec sub_spec = { |
| 168 |
.name = "_testcapi.Sub", |
| 169 |
.basicsize = basicsize, |
| 170 |
.itemsize = itemsize, |
| 171 |
.flags = Py_TPFLAGS_DEFAULT, |
| 172 |
.slots = slots, |
| 173 |
}; |
| 174 |
|
| 175 |
result = PyType_FromMetaclass(NULL, module, &sub_spec, base); |
| 176 |
finally: |
| 177 |
return result; |
| 178 |
} |
| 179 |
|
| 180 |
static PyMemberDef * |
| 181 |
heaptype_with_member_extract_and_check_memb(PyObject *self) |
| 182 |
{ |
| 183 |
PyMemberDef *def = PyType_GetSlot(Py_TYPE(self), Py_tp_members); |
| 184 |
if (!def) { |
| 185 |
if (!PyErr_Occurred()) { |
| 186 |
PyErr_SetString(PyExc_ValueError, "tp_members is NULL"); |
| 187 |
} |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
if (!def[0].name) { |
| 191 |
PyErr_SetString(PyExc_ValueError, "tp_members[0] is NULL"); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
if (def[1].name) { |
| 195 |
PyErr_SetString(PyExc_ValueError, "tp_members[1] is not NULL"); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
if (strcmp(def[0].name, "memb")) { |
| 199 |
PyErr_SetString(PyExc_ValueError, "tp_members[0] is not for `memb`"); |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
if (def[0].flags) { |
| 203 |
PyErr_SetString(PyExc_ValueError, "tp_members[0] has flags set"); |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
return def; |
| 207 |
} |
| 208 |
|
| 209 |
static PyObject * |
| 210 |
heaptype_with_member_get_memb(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 211 |
{ |
| 212 |
PyMemberDef *def = heaptype_with_member_extract_and_check_memb(self); |
| 213 |
return PyMember_GetOne((const char *)self, def); |
| 214 |
} |
| 215 |
|
| 216 |
static PyObject * |
| 217 |
heaptype_with_member_set_memb(PyObject *self, PyObject *value) |
| 218 |
{ |
| 219 |
PyMemberDef *def = heaptype_with_member_extract_and_check_memb(self); |
| 220 |
int r = PyMember_SetOne((char *)self, def, value); |
| 221 |
if (r < 0) { |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
Py_RETURN_NONE; |
| 225 |
} |
| 226 |
|
| 227 |
static PyObject * |
| 228 |
get_memb_offset(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 229 |
{ |
| 230 |
PyMemberDef *def = heaptype_with_member_extract_and_check_memb(self); |
| 231 |
return PyLong_FromSsize_t(def->offset); |
| 232 |
} |
| 233 |
|
| 234 |
static PyObject * |
| 235 |
heaptype_with_member_get_memb_relative(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 236 |
{ |
| 237 |
PyMemberDef def = {"memb", Py_T_BYTE, sizeof(PyObject), Py_RELATIVE_OFFSET}; |
| 238 |
return PyMember_GetOne((const char *)self, &def); |
| 239 |
} |
| 240 |
|
| 241 |
static PyObject * |
| 242 |
heaptype_with_member_set_memb_relative(PyObject *self, PyObject *value) |
| 243 |
{ |
| 244 |
PyMemberDef def = {"memb", Py_T_BYTE, sizeof(PyObject), Py_RELATIVE_OFFSET}; |
| 245 |
int r = PyMember_SetOne((char *)self, &def, value); |
| 246 |
if (r < 0) { |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
Py_RETURN_NONE; |
| 250 |
} |
| 251 |
|
| 252 |
typedef struct { |
| 253 |
int padding; // just so the offset isn't 0 |
| 254 |
PyObject *dict; |
| 255 |
} HeapCTypeWithDictStruct; |
| 256 |
|
| 257 |
static void |
| 258 |
heapctypewithrelativedict_dealloc(PyObject* self) |
| 259 |
{ |
| 260 |
PyTypeObject *tp = Py_TYPE(self); |
| 261 |
HeapCTypeWithDictStruct *data = PyObject_GetTypeData(self, tp); |
| 262 |
Py_XDECREF(data->dict); |
| 263 |
PyObject_Free(self); |
| 264 |
Py_DECREF(tp); |
| 265 |
} |
| 266 |
|
| 267 |
static PyType_Spec HeapCTypeWithRelativeDict_spec = { |
| 268 |
.name = "_testcapi.HeapCTypeWithRelativeDict", |
| 269 |
.basicsize = -(int)sizeof(HeapCTypeWithDictStruct), |
| 270 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 271 |
.slots = (PyType_Slot[]) { |
| 272 |
{Py_tp_dealloc, heapctypewithrelativedict_dealloc}, |
| 273 |
{Py_tp_getset, (PyGetSetDef[]) { |
| 274 |
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
| 275 |
{NULL} /* Sentinel */ |
| 276 |
}}, |
| 277 |
{Py_tp_members, (PyMemberDef[]) { |
| 278 |
{"dictobj", _Py_T_OBJECT, |
| 279 |
offsetof(HeapCTypeWithDictStruct, dict), |
| 280 |
Py_RELATIVE_OFFSET}, |
| 281 |
{"__dictoffset__", Py_T_PYSSIZET, |
| 282 |
offsetof(HeapCTypeWithDictStruct, dict), |
| 283 |
Py_READONLY | Py_RELATIVE_OFFSET}, |
| 284 |
{NULL} /* Sentinel */ |
| 285 |
}}, |
| 286 |
{0, 0}, |
| 287 |
} |
| 288 |
}; |
| 289 |
|
| 290 |
typedef struct { |
| 291 |
char padding; // just so the offset isn't 0 |
| 292 |
PyObject *weakreflist; |
| 293 |
} HeapCTypeWithWeakrefStruct; |
| 294 |
|
| 295 |
static void |
| 296 |
heapctypewithrelativeweakref_dealloc(PyObject* self) |
| 297 |
{ |
| 298 |
PyTypeObject *tp = Py_TYPE(self); |
| 299 |
HeapCTypeWithWeakrefStruct *data = PyObject_GetTypeData(self, tp); |
| 300 |
if (data->weakreflist != NULL) { |
| 301 |
PyObject_ClearWeakRefs(self); |
| 302 |
} |
| 303 |
Py_XDECREF(data->weakreflist); |
| 304 |
PyObject_Free(self); |
| 305 |
Py_DECREF(tp); |
| 306 |
} |
| 307 |
|
| 308 |
static PyType_Spec HeapCTypeWithRelativeWeakref_spec = { |
| 309 |
.name = "_testcapi.HeapCTypeWithRelativeWeakref", |
| 310 |
.basicsize = -(int)sizeof(HeapCTypeWithWeakrefStruct), |
| 311 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 312 |
.slots = (PyType_Slot[]) { |
| 313 |
{Py_tp_dealloc, heapctypewithrelativeweakref_dealloc}, |
| 314 |
{Py_tp_members, (PyMemberDef[]) { |
| 315 |
{"weakreflist", _Py_T_OBJECT, |
| 316 |
offsetof(HeapCTypeWithWeakrefStruct, weakreflist), |
| 317 |
Py_RELATIVE_OFFSET}, |
| 318 |
{"__weaklistoffset__", Py_T_PYSSIZET, |
| 319 |
offsetof(HeapCTypeWithWeakrefStruct, weakreflist), |
| 320 |
Py_READONLY | Py_RELATIVE_OFFSET}, |
| 321 |
{NULL} /* Sentinel */ |
| 322 |
}}, |
| 323 |
{0, 0}, |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
static PyMethodDef heaptype_with_member_methods[] = { |
| 328 |
{"get_memb", heaptype_with_member_get_memb, METH_NOARGS}, |
| 329 |
{"set_memb", heaptype_with_member_set_memb, METH_O}, |
| 330 |
{"get_memb_offset", get_memb_offset, METH_NOARGS}, |
| 331 |
{"get_memb_relative", heaptype_with_member_get_memb_relative, METH_NOARGS}, |
| 332 |
{"set_memb_relative", heaptype_with_member_set_memb_relative, METH_O}, |
| 333 |
{NULL}, |
| 334 |
}; |
| 335 |
|
| 336 |
/*[clinic input] |
| 337 |
make_heaptype_with_member |
| 338 |
|
| 339 |
extra_base_size: int = 0 |
| 340 |
basicsize: int = 0 |
| 341 |
member_offset: int = 0 |
| 342 |
add_relative_flag: bool = False |
| 343 |
* |
| 344 |
member_name: str = "memb" |
| 345 |
member_flags: int = 0 |
| 346 |
member_type: int(c_default="Py_T_BYTE") = -1 |
| 347 |
|
| 348 |
[clinic start generated code]*/ |
| 349 |
|
| 350 |
static PyObject * |
| 351 |
make_heaptype_with_member_impl(PyObject *module, int extra_base_size, |
| 352 |
int basicsize, int member_offset, |
| 353 |
int add_relative_flag, |
| 354 |
const char *member_name, int member_flags, |
| 355 |
int member_type) |
| 356 |
/*[clinic end generated code: output=7005db9a07396997 input=007e29cdbe1d3390]*/ |
| 357 |
{ |
| 358 |
PyObject *base = NULL; |
| 359 |
PyObject *result = NULL; |
| 360 |
|
| 361 |
PyType_Spec base_spec = { |
| 362 |
.name = "_testcapi.Base", |
| 363 |
.basicsize = sizeof(PyObject) + extra_base_size, |
| 364 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 365 |
.slots = empty_slots, |
| 366 |
}; |
| 367 |
base = PyType_FromMetaclass(NULL, module, &base_spec, NULL); |
| 368 |
if (!base) { |
| 369 |
goto finally; |
| 370 |
} |
| 371 |
|
| 372 |
PyMemberDef members[] = { |
| 373 |
{member_name, member_type, member_offset, |
| 374 |
member_flags | (add_relative_flag ? Py_RELATIVE_OFFSET : 0)}, |
| 375 |
{0}, |
| 376 |
}; |
| 377 |
PyType_Slot slots[] = { |
| 378 |
{Py_tp_members, members}, |
| 379 |
{Py_tp_methods, heaptype_with_member_methods}, |
| 380 |
{0, NULL}, |
| 381 |
}; |
| 382 |
|
| 383 |
PyType_Spec sub_spec = { |
| 384 |
.name = "_testcapi.Sub", |
| 385 |
.basicsize = basicsize, |
| 386 |
.flags = Py_TPFLAGS_DEFAULT, |
| 387 |
.slots = slots, |
| 388 |
}; |
| 389 |
|
| 390 |
result = PyType_FromMetaclass(NULL, module, &sub_spec, base); |
| 391 |
finally: |
| 392 |
Py_XDECREF(base); |
| 393 |
return result; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
static PyObject * |
| 398 |
test_alignof_max_align_t(PyObject *module, PyObject *Py_UNUSED(ignored)) |
| 399 |
{ |
| 400 |
// We define ALIGNOF_MAX_ALIGN_T even if the compiler doesn't support |
| 401 |
// max_align_t. Double-check that it's correct. |
| 402 |
assert(ALIGNOF_MAX_ALIGN_T > 0); |
| 403 |
assert(ALIGNOF_MAX_ALIGN_T >= _Alignof(long long)); |
| 404 |
assert(ALIGNOF_MAX_ALIGN_T >= _Alignof(long double)); |
| 405 |
assert(ALIGNOF_MAX_ALIGN_T >= _Alignof(void*)); |
| 406 |
assert(ALIGNOF_MAX_ALIGN_T >= _Alignof(void (*)(void))); |
| 407 |
|
| 408 |
// Ensure it's a power of two |
| 409 |
assert((ALIGNOF_MAX_ALIGN_T & (ALIGNOF_MAX_ALIGN_T - 1)) == 0); |
| 410 |
|
| 411 |
Py_RETURN_NONE; |
| 412 |
} |
| 413 |
|
| 414 |
static PyMethodDef TestMethods[] = { |
| 415 |
{"make_sized_heaptypes", make_sized_heaptypes, METH_VARARGS}, |
| 416 |
{"subclass_var_heaptype", subclass_var_heaptype, METH_VARARGS}, |
| 417 |
{"subclass_heaptype", subclass_heaptype, METH_VARARGS}, |
| 418 |
MAKE_HEAPTYPE_WITH_MEMBER_METHODDEF |
| 419 |
{"test_alignof_max_align_t", test_alignof_max_align_t, METH_NOARGS}, |
| 420 |
{NULL}, |
| 421 |
}; |
| 422 |
|
| 423 |
int |
| 424 |
_PyTestLimitedCAPI_Init_HeaptypeRelative(PyObject *m) |
| 425 |
{ |
| 426 |
if (PyModule_AddFunctions(m, TestMethods) < 0) { |
| 427 |
return -1; |
| 428 |
} |
| 429 |
|
| 430 |
if (PyModule_AddIntMacro(m, ALIGNOF_MAX_ALIGN_T) < 0) { |
| 431 |
return -1; |
| 432 |
} |
| 433 |
|
| 434 |
#define ADD_FROM_SPEC(SPEC) do { \ |
| 435 |
PyObject *tp = PyType_FromSpec(SPEC); \ |
| 436 |
if (!tp) { \ |
| 437 |
return -1; \ |
| 438 |
} \ |
| 439 |
if (PyModule_AddType(m, (PyTypeObject *)tp) < 0) { \ |
| 440 |
return -1; \ |
| 441 |
} \ |
| 442 |
} while (0) |
| 443 |
|
| 444 |
PyObject *tp; |
| 445 |
|
| 446 |
tp = PyType_FromSpec(&HeapCTypeWithRelativeDict_spec); |
| 447 |
if (!tp) { |
| 448 |
return -1; |
| 449 |
} |
| 450 |
if (PyModule_AddType(m, (PyTypeObject *)tp) < 0) { |
| 451 |
return -1; |
| 452 |
} |
| 453 |
Py_DECREF(tp); |
| 454 |
|
| 455 |
tp = PyType_FromSpec(&HeapCTypeWithRelativeWeakref_spec); |
| 456 |
if (!tp) { |
| 457 |
return -1; |
| 458 |
} |
| 459 |
if (PyModule_AddType(m, (PyTypeObject *)tp) < 0) { |
| 460 |
return -1; |
| 461 |
} |
| 462 |
Py_DECREF(tp); |
| 463 |
|
| 464 |
if (PyModule_AddIntMacro(m, Py_T_PYSSIZET) < 0) { |
| 465 |
return -1; |
| 466 |
} |
| 467 |
if (PyModule_AddIntMacro(m, Py_READONLY) < 0) { |
| 468 |
return -1; |
| 469 |
} |
| 470 |
|
| 471 |
return 0; |
| 472 |
} |