| 1 |
#include "parts.h" |
| 2 |
#include <stddef.h> // offsetof() |
| 3 |
|
| 4 |
|
| 5 |
static struct PyModuleDef *_testcapimodule = NULL; // set at initialization |
| 6 |
|
| 7 |
/* Tests for heap types (PyType_From*) */ |
| 8 |
|
| 9 |
static PyObject *pytype_fromspec_meta(PyObject* self, PyObject *meta) |
| 10 |
{ |
| 11 |
if (!PyType_Check(meta)) { |
| 12 |
PyErr_SetString( |
| 13 |
PyExc_TypeError, |
| 14 |
"pytype_fromspec_meta: must be invoked with a type argument!"); |
| 15 |
return NULL; |
| 16 |
} |
| 17 |
|
| 18 |
PyType_Slot HeapCTypeViaMetaclass_slots[] = { |
| 19 |
{0}, |
| 20 |
}; |
| 21 |
|
| 22 |
PyType_Spec HeapCTypeViaMetaclass_spec = { |
| 23 |
"_testcapi.HeapCTypeViaMetaclass", |
| 24 |
sizeof(PyObject), |
| 25 |
0, |
| 26 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 27 |
HeapCTypeViaMetaclass_slots |
| 28 |
}; |
| 29 |
|
| 30 |
return PyType_FromMetaclass( |
| 31 |
(PyTypeObject *) meta, NULL, &HeapCTypeViaMetaclass_spec, NULL); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
static PyType_Slot empty_type_slots[] = { |
| 36 |
{0, 0}, |
| 37 |
}; |
| 38 |
|
| 39 |
static PyType_Spec MinimalMetaclass_spec = { |
| 40 |
.name = "_testcapi.MinimalMetaclass", |
| 41 |
.basicsize = sizeof(PyHeapTypeObject), |
| 42 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 43 |
.slots = empty_type_slots, |
| 44 |
}; |
| 45 |
|
| 46 |
static PyType_Spec MinimalType_spec = { |
| 47 |
.name = "_testcapi.MinimalSpecType", |
| 48 |
.basicsize = 0, // Updated later |
| 49 |
.flags = Py_TPFLAGS_DEFAULT, |
| 50 |
.slots = empty_type_slots, |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
static PyObject * |
| 55 |
test_from_spec_metatype_inheritance(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 56 |
{ |
| 57 |
PyObject *metaclass = NULL; |
| 58 |
PyObject *class = NULL; |
| 59 |
PyObject *new = NULL; |
| 60 |
PyObject *subclasses = NULL; |
| 61 |
PyObject *result = NULL; |
| 62 |
int r; |
| 63 |
|
| 64 |
metaclass = PyType_FromSpecWithBases(&MinimalMetaclass_spec, (PyObject*)&PyType_Type); |
| 65 |
if (metaclass == NULL) { |
| 66 |
goto finally; |
| 67 |
} |
| 68 |
class = PyObject_CallFunction(metaclass, "s(){}", "TestClass"); |
| 69 |
if (class == NULL) { |
| 70 |
goto finally; |
| 71 |
} |
| 72 |
|
| 73 |
MinimalType_spec.basicsize = (int)(((PyTypeObject*)class)->tp_basicsize); |
| 74 |
new = PyType_FromSpecWithBases(&MinimalType_spec, class); |
| 75 |
if (new == NULL) { |
| 76 |
goto finally; |
| 77 |
} |
| 78 |
if (Py_TYPE(new) != (PyTypeObject*)metaclass) { |
| 79 |
PyErr_SetString(PyExc_AssertionError, |
| 80 |
"Metaclass not set properly!"); |
| 81 |
goto finally; |
| 82 |
} |
| 83 |
|
| 84 |
/* Assert that __subclasses__ is updated */ |
| 85 |
subclasses = PyObject_CallMethod(class, "__subclasses__", ""); |
| 86 |
if (!subclasses) { |
| 87 |
goto finally; |
| 88 |
} |
| 89 |
r = PySequence_Contains(subclasses, new); |
| 90 |
if (r < 0) { |
| 91 |
goto finally; |
| 92 |
} |
| 93 |
if (r == 0) { |
| 94 |
PyErr_SetString(PyExc_AssertionError, |
| 95 |
"subclasses not set properly!"); |
| 96 |
goto finally; |
| 97 |
} |
| 98 |
|
| 99 |
result = Py_NewRef(Py_None); |
| 100 |
|
| 101 |
finally: |
| 102 |
Py_XDECREF(metaclass); |
| 103 |
Py_XDECREF(class); |
| 104 |
Py_XDECREF(new); |
| 105 |
Py_XDECREF(subclasses); |
| 106 |
return result; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
static PyObject * |
| 111 |
test_from_spec_invalid_metatype_inheritance(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 112 |
{ |
| 113 |
PyObject *metaclass_a = NULL; |
| 114 |
PyObject *metaclass_b = NULL; |
| 115 |
PyObject *class_a = NULL; |
| 116 |
PyObject *class_b = NULL; |
| 117 |
PyObject *bases = NULL; |
| 118 |
PyObject *new = NULL; |
| 119 |
PyObject *meta_error_string = NULL; |
| 120 |
PyObject *exc = NULL; |
| 121 |
PyObject *result = NULL; |
| 122 |
PyObject *message = NULL; |
| 123 |
PyObject *args = NULL; |
| 124 |
|
| 125 |
metaclass_a = PyType_FromSpecWithBases(&MinimalMetaclass_spec, (PyObject*)&PyType_Type); |
| 126 |
if (metaclass_a == NULL) { |
| 127 |
goto finally; |
| 128 |
} |
| 129 |
metaclass_b = PyType_FromSpecWithBases(&MinimalMetaclass_spec, (PyObject*)&PyType_Type); |
| 130 |
if (metaclass_b == NULL) { |
| 131 |
goto finally; |
| 132 |
} |
| 133 |
class_a = PyObject_CallFunction(metaclass_a, "s(){}", "TestClassA"); |
| 134 |
if (class_a == NULL) { |
| 135 |
goto finally; |
| 136 |
} |
| 137 |
|
| 138 |
class_b = PyObject_CallFunction(metaclass_b, "s(){}", "TestClassB"); |
| 139 |
if (class_b == NULL) { |
| 140 |
goto finally; |
| 141 |
} |
| 142 |
|
| 143 |
bases = PyTuple_Pack(2, class_a, class_b); |
| 144 |
if (bases == NULL) { |
| 145 |
goto finally; |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* The following should raise a TypeError due to a MetaClass conflict. |
| 150 |
*/ |
| 151 |
new = PyType_FromSpecWithBases(&MinimalType_spec, bases); |
| 152 |
if (new != NULL) { |
| 153 |
PyErr_SetString(PyExc_AssertionError, |
| 154 |
"MetaType conflict not recognized by PyType_FromSpecWithBases"); |
| 155 |
goto finally; |
| 156 |
} |
| 157 |
|
| 158 |
// Assert that the correct exception was raised |
| 159 |
if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 160 |
exc = PyErr_GetRaisedException(); |
| 161 |
args = PyException_GetArgs(exc); |
| 162 |
if (!PyTuple_Check(args) || PyTuple_Size(args) != 1) { |
| 163 |
PyErr_SetString(PyExc_AssertionError, |
| 164 |
"TypeError args are not a one-tuple"); |
| 165 |
goto finally; |
| 166 |
} |
| 167 |
message = Py_NewRef(PyTuple_GET_ITEM(args, 0)); |
| 168 |
meta_error_string = PyUnicode_FromString("metaclass conflict:"); |
| 169 |
if (meta_error_string == NULL) { |
| 170 |
goto finally; |
| 171 |
} |
| 172 |
int res = PyUnicode_Contains(message, meta_error_string); |
| 173 |
if (res < 0) { |
| 174 |
goto finally; |
| 175 |
} |
| 176 |
if (res == 0) { |
| 177 |
PyErr_SetString(PyExc_AssertionError, |
| 178 |
"TypeError did not include expected message."); |
| 179 |
goto finally; |
| 180 |
} |
| 181 |
result = Py_NewRef(Py_None); |
| 182 |
} |
| 183 |
finally: |
| 184 |
Py_XDECREF(metaclass_a); |
| 185 |
Py_XDECREF(metaclass_b); |
| 186 |
Py_XDECREF(bases); |
| 187 |
Py_XDECREF(new); |
| 188 |
Py_XDECREF(meta_error_string); |
| 189 |
Py_XDECREF(exc); |
| 190 |
Py_XDECREF(message); |
| 191 |
Py_XDECREF(class_a); |
| 192 |
Py_XDECREF(class_b); |
| 193 |
Py_XDECREF(args); |
| 194 |
return result; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
static PyObject * |
| 199 |
simple_str(PyObject *self) { |
| 200 |
return PyUnicode_FromString("<test>"); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
static PyObject * |
| 205 |
test_type_from_ephemeral_spec(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 206 |
{ |
| 207 |
// Test that a heap type can be created from a spec that's later deleted |
| 208 |
// (along with all its contents). |
| 209 |
// All necessary data must be copied and held by the class |
| 210 |
PyType_Spec *spec = NULL; |
| 211 |
char *name = NULL; |
| 212 |
char *doc = NULL; |
| 213 |
PyType_Slot *slots = NULL; |
| 214 |
PyObject *class = NULL; |
| 215 |
PyObject *instance = NULL; |
| 216 |
PyObject *obj = NULL; |
| 217 |
PyObject *result = NULL; |
| 218 |
|
| 219 |
/* create a spec (and all its contents) on the heap */ |
| 220 |
|
| 221 |
const char NAME[] = "testcapi._Test"; |
| 222 |
const char DOC[] = "a test class"; |
| 223 |
|
| 224 |
spec = PyMem_New(PyType_Spec, 1); |
| 225 |
if (spec == NULL) { |
| 226 |
PyErr_NoMemory(); |
| 227 |
goto finally; |
| 228 |
} |
| 229 |
name = PyMem_New(char, sizeof(NAME)); |
| 230 |
if (name == NULL) { |
| 231 |
PyErr_NoMemory(); |
| 232 |
goto finally; |
| 233 |
} |
| 234 |
memcpy(name, NAME, sizeof(NAME)); |
| 235 |
|
| 236 |
doc = PyMem_New(char, sizeof(DOC)); |
| 237 |
if (doc == NULL) { |
| 238 |
PyErr_NoMemory(); |
| 239 |
goto finally; |
| 240 |
} |
| 241 |
memcpy(doc, DOC, sizeof(DOC)); |
| 242 |
|
| 243 |
spec->name = name; |
| 244 |
spec->basicsize = sizeof(PyObject); |
| 245 |
spec->itemsize = 0; |
| 246 |
spec->flags = Py_TPFLAGS_DEFAULT; |
| 247 |
slots = PyMem_New(PyType_Slot, 3); |
| 248 |
if (slots == NULL) { |
| 249 |
PyErr_NoMemory(); |
| 250 |
goto finally; |
| 251 |
} |
| 252 |
slots[0].slot = Py_tp_str; |
| 253 |
slots[0].pfunc = simple_str; |
| 254 |
slots[1].slot = Py_tp_doc; |
| 255 |
slots[1].pfunc = doc; |
| 256 |
slots[2].slot = 0; |
| 257 |
slots[2].pfunc = NULL; |
| 258 |
spec->slots = slots; |
| 259 |
|
| 260 |
/* create the class */ |
| 261 |
|
| 262 |
class = PyType_FromSpec(spec); |
| 263 |
if (class == NULL) { |
| 264 |
goto finally; |
| 265 |
} |
| 266 |
|
| 267 |
/* deallocate the spec (and all contents) */ |
| 268 |
|
| 269 |
// (Explicitly overwrite memory before freeing, |
| 270 |
// so bugs show themselves even without the debug allocator's help.) |
| 271 |
memset(spec, 0xdd, sizeof(PyType_Spec)); |
| 272 |
PyMem_Free(spec); |
| 273 |
spec = NULL; |
| 274 |
memset(name, 0xdd, sizeof(NAME)); |
| 275 |
PyMem_Free(name); |
| 276 |
name = NULL; |
| 277 |
memset(doc, 0xdd, sizeof(DOC)); |
| 278 |
PyMem_Free(doc); |
| 279 |
doc = NULL; |
| 280 |
memset(slots, 0xdd, 3 * sizeof(PyType_Slot)); |
| 281 |
PyMem_Free(slots); |
| 282 |
slots = NULL; |
| 283 |
|
| 284 |
/* check that everything works */ |
| 285 |
|
| 286 |
PyTypeObject *class_tp = (PyTypeObject *)class; |
| 287 |
PyHeapTypeObject *class_ht = (PyHeapTypeObject *)class; |
| 288 |
assert(strcmp(class_tp->tp_name, "testcapi._Test") == 0); |
| 289 |
assert(strcmp(PyUnicode_AsUTF8(class_ht->ht_name), "_Test") == 0); |
| 290 |
assert(strcmp(PyUnicode_AsUTF8(class_ht->ht_qualname), "_Test") == 0); |
| 291 |
assert(strcmp(class_tp->tp_doc, "a test class") == 0); |
| 292 |
|
| 293 |
// call and check __str__ |
| 294 |
instance = PyObject_CallNoArgs(class); |
| 295 |
if (instance == NULL) { |
| 296 |
goto finally; |
| 297 |
} |
| 298 |
obj = PyObject_Str(instance); |
| 299 |
if (obj == NULL) { |
| 300 |
goto finally; |
| 301 |
} |
| 302 |
assert(strcmp(PyUnicode_AsUTF8(obj), "<test>") == 0); |
| 303 |
Py_CLEAR(obj); |
| 304 |
|
| 305 |
result = Py_NewRef(Py_None); |
| 306 |
finally: |
| 307 |
PyMem_Free(spec); |
| 308 |
PyMem_Free(name); |
| 309 |
PyMem_Free(doc); |
| 310 |
PyMem_Free(slots); |
| 311 |
Py_XDECREF(class); |
| 312 |
Py_XDECREF(instance); |
| 313 |
Py_XDECREF(obj); |
| 314 |
return result; |
| 315 |
} |
| 316 |
|
| 317 |
PyType_Slot repeated_doc_slots[] = { |
| 318 |
{Py_tp_doc, "A class used for tests·"}, |
| 319 |
{Py_tp_doc, "A class used for tests"}, |
| 320 |
{0, 0}, |
| 321 |
}; |
| 322 |
|
| 323 |
PyType_Spec repeated_doc_slots_spec = { |
| 324 |
.name = "RepeatedDocSlotClass", |
| 325 |
.basicsize = sizeof(PyObject), |
| 326 |
.slots = repeated_doc_slots, |
| 327 |
}; |
| 328 |
|
| 329 |
typedef struct { |
| 330 |
PyObject_HEAD |
| 331 |
int data; |
| 332 |
} HeapCTypeWithDataObject; |
| 333 |
|
| 334 |
|
| 335 |
static struct PyMemberDef members_to_repeat[] = { |
| 336 |
{"Py_T_INT", Py_T_INT, offsetof(HeapCTypeWithDataObject, data), 0, NULL}, |
| 337 |
{NULL} |
| 338 |
}; |
| 339 |
|
| 340 |
PyType_Slot repeated_members_slots[] = { |
| 341 |
{Py_tp_members, members_to_repeat}, |
| 342 |
{Py_tp_members, members_to_repeat}, |
| 343 |
{0, 0}, |
| 344 |
}; |
| 345 |
|
| 346 |
PyType_Spec repeated_members_slots_spec = { |
| 347 |
.name = "RepeatedMembersSlotClass", |
| 348 |
.basicsize = sizeof(HeapCTypeWithDataObject), |
| 349 |
.slots = repeated_members_slots, |
| 350 |
}; |
| 351 |
|
| 352 |
static PyObject * |
| 353 |
create_type_from_repeated_slots(PyObject *self, PyObject *variant_obj) |
| 354 |
{ |
| 355 |
PyObject *class = NULL; |
| 356 |
int variant = PyLong_AsLong(variant_obj); |
| 357 |
if (PyErr_Occurred()) { |
| 358 |
return NULL; |
| 359 |
} |
| 360 |
switch (variant) { |
| 361 |
case 0: |
| 362 |
class = PyType_FromSpec(&repeated_doc_slots_spec); |
| 363 |
break; |
| 364 |
case 1: |
| 365 |
class = PyType_FromSpec(&repeated_members_slots_spec); |
| 366 |
break; |
| 367 |
default: |
| 368 |
PyErr_SetString(PyExc_ValueError, "bad test variant"); |
| 369 |
break; |
| 370 |
} |
| 371 |
return class; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
static PyObject * |
| 376 |
make_immutable_type_with_base(PyObject *self, PyObject *base) |
| 377 |
{ |
| 378 |
assert(PyType_Check(base)); |
| 379 |
PyType_Spec ImmutableSubclass_spec = { |
| 380 |
.name = "ImmutableSubclass", |
| 381 |
.basicsize = (int)((PyTypeObject*)base)->tp_basicsize, |
| 382 |
.slots = empty_type_slots, |
| 383 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, |
| 384 |
}; |
| 385 |
return PyType_FromSpecWithBases(&ImmutableSubclass_spec, base); |
| 386 |
} |
| 387 |
|
| 388 |
static PyObject * |
| 389 |
make_type_with_base(PyObject *self, PyObject *base) |
| 390 |
{ |
| 391 |
assert(PyType_Check(base)); |
| 392 |
PyType_Spec ImmutableSubclass_spec = { |
| 393 |
.name = "_testcapi.Subclass", |
| 394 |
.basicsize = (int)((PyTypeObject*)base)->tp_basicsize, |
| 395 |
.slots = empty_type_slots, |
| 396 |
.flags = Py_TPFLAGS_DEFAULT, |
| 397 |
}; |
| 398 |
return PyType_FromSpecWithBases(&ImmutableSubclass_spec, base); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
static PyObject * |
| 403 |
pyobject_getitemdata(PyObject *self, PyObject *o) |
| 404 |
{ |
| 405 |
void *pointer = PyObject_GetItemData(o); |
| 406 |
if (pointer == NULL) { |
| 407 |
return NULL; |
| 408 |
} |
| 409 |
return PyLong_FromVoidPtr(pointer); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
static PyObject * |
| 414 |
create_type_with_token(PyObject *module, PyObject *args) |
| 415 |
{ |
| 416 |
const char *name; |
| 417 |
PyObject *py_token; |
| 418 |
if (!PyArg_ParseTuple(args, "sO", &name, &py_token)) { |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
void *token = PyLong_AsVoidPtr(py_token); |
| 422 |
if (token == Py_TP_USE_SPEC) { |
| 423 |
// Py_TP_USE_SPEC requires the spec that at least outlives the class |
| 424 |
static PyType_Slot slots[] = { |
| 425 |
{Py_tp_token, Py_TP_USE_SPEC}, |
| 426 |
{0}, |
| 427 |
}; |
| 428 |
static PyType_Spec spec = { |
| 429 |
.name = "_testcapi.DefaultTokenTest", |
| 430 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 431 |
.slots = slots, |
| 432 |
}; |
| 433 |
PyObject *type = PyType_FromMetaclass(NULL, NULL, &spec, NULL); |
| 434 |
if (!type) { |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
token = PyType_GetSlot((PyTypeObject *)type, Py_tp_token); |
| 438 |
assert(!PyErr_Occurred()); |
| 439 |
Py_DECREF(type); |
| 440 |
if (token != &spec) { |
| 441 |
PyErr_SetString(PyExc_AssertionError, |
| 442 |
"failed to convert token from Py_TP_USE_SPEC"); |
| 443 |
return NULL; |
| 444 |
} |
| 445 |
} |
| 446 |
// Test non-NULL token that must also outlive the class |
| 447 |
PyType_Slot slots[] = { |
| 448 |
{Py_tp_token, token}, |
| 449 |
{0}, |
| 450 |
}; |
| 451 |
PyType_Spec spec = { |
| 452 |
.name = name, |
| 453 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 454 |
.slots = slots, |
| 455 |
}; |
| 456 |
return PyType_FromMetaclass(NULL, module, &spec, NULL); |
| 457 |
} |
| 458 |
|
| 459 |
static PyObject * |
| 460 |
get_tp_token(PyObject *self, PyObject *type) |
| 461 |
{ |
| 462 |
void *token = PyType_GetSlot((PyTypeObject *)type, Py_tp_token); |
| 463 |
if (PyErr_Occurred()) { |
| 464 |
return NULL; |
| 465 |
} |
| 466 |
return PyLong_FromVoidPtr(token); |
| 467 |
} |
| 468 |
|
| 469 |
static PyObject * |
| 470 |
pytype_getbasebytoken(PyObject *self, PyObject *args) |
| 471 |
{ |
| 472 |
PyTypeObject *type; |
| 473 |
PyObject *py_token, *use_mro, *need_result; |
| 474 |
if (!PyArg_ParseTuple(args, "OOOO", |
| 475 |
&type, &py_token, &use_mro, &need_result)) { |
| 476 |
return NULL; |
| 477 |
} |
| 478 |
|
| 479 |
PyObject *mro_save = NULL; |
| 480 |
if (use_mro != Py_True) { |
| 481 |
// Test internal detail: PyType_GetBaseByToken works even with |
| 482 |
// types that are only partially initialized (or torn down): |
| 483 |
// if tp_mro=NULL we fall back to tp_bases. |
| 484 |
assert(PyType_Check(type)); |
| 485 |
mro_save = type->tp_mro; |
| 486 |
type->tp_mro = NULL; |
| 487 |
} |
| 488 |
|
| 489 |
void *token = PyLong_AsVoidPtr(py_token); |
| 490 |
PyObject *result; |
| 491 |
int ret; |
| 492 |
if (need_result == Py_True) { |
| 493 |
ret = PyType_GetBaseByToken(type, token, (PyTypeObject **)&result); |
| 494 |
} |
| 495 |
else { |
| 496 |
result = NULL; |
| 497 |
ret = PyType_GetBaseByToken(type, token, NULL); |
| 498 |
} |
| 499 |
|
| 500 |
if (use_mro != Py_True) { |
| 501 |
type->tp_mro = mro_save; |
| 502 |
} |
| 503 |
if (ret < 0) { |
| 504 |
assert(result == NULL); |
| 505 |
return NULL; |
| 506 |
} |
| 507 |
PyObject *py_ret = PyLong_FromLong(ret); |
| 508 |
if (py_ret == NULL) { |
| 509 |
goto error; |
| 510 |
} |
| 511 |
PyObject *tuple = PyTuple_New(2); |
| 512 |
if (tuple == NULL) { |
| 513 |
goto error; |
| 514 |
} |
| 515 |
PyTuple_SET_ITEM(tuple, 0, py_ret); |
| 516 |
PyTuple_SET_ITEM(tuple, 1, result ? result : Py_None); |
| 517 |
return tuple; |
| 518 |
error: |
| 519 |
Py_XDECREF(py_ret); |
| 520 |
Py_XDECREF(result); |
| 521 |
return NULL; |
| 522 |
} |
| 523 |
|
| 524 |
static PyObject * |
| 525 |
pytype_getmodulebydef(PyObject *self, PyObject *type) |
| 526 |
{ |
| 527 |
PyObject *mod = PyType_GetModuleByDef((PyTypeObject *)type, _testcapimodule); |
| 528 |
return Py_XNewRef(mod); |
| 529 |
} |
| 530 |
|
| 531 |
static PyType_Slot HeapCTypeWithBasesSlotNone_slots[] = { |
| 532 |
{Py_tp_bases, NULL}, /* filled out with Py_None in runtime */ |
| 533 |
{0, 0}, |
| 534 |
}; |
| 535 |
|
| 536 |
static PyType_Spec HeapCTypeWithBasesSlotNone_spec = { |
| 537 |
.name = "_testcapi.HeapCTypeWithBasesSlotNone", |
| 538 |
.basicsize = sizeof(PyObject), |
| 539 |
.flags = Py_TPFLAGS_DEFAULT, |
| 540 |
.slots = HeapCTypeWithBasesSlotNone_slots |
| 541 |
}; |
| 542 |
|
| 543 |
static PyObject * |
| 544 |
create_heapctype_with_none_bases_slot(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 545 |
{ |
| 546 |
HeapCTypeWithBasesSlotNone_slots[0].pfunc = Py_None; |
| 547 |
return PyType_FromSpec(&HeapCTypeWithBasesSlotNone_spec); |
| 548 |
} |
| 549 |
|
| 550 |
static PyMethodDef TestMethods[] = { |
| 551 |
{"pytype_fromspec_meta", pytype_fromspec_meta, METH_O}, |
| 552 |
{"test_type_from_ephemeral_spec", test_type_from_ephemeral_spec, METH_NOARGS}, |
| 553 |
{"create_type_from_repeated_slots", |
| 554 |
create_type_from_repeated_slots, METH_O}, |
| 555 |
{"test_from_spec_metatype_inheritance", test_from_spec_metatype_inheritance, |
| 556 |
METH_NOARGS}, |
| 557 |
{"test_from_spec_invalid_metatype_inheritance", |
| 558 |
test_from_spec_invalid_metatype_inheritance, |
| 559 |
METH_NOARGS}, |
| 560 |
{"make_immutable_type_with_base", make_immutable_type_with_base, METH_O}, |
| 561 |
{"make_type_with_base", make_type_with_base, METH_O}, |
| 562 |
{"pyobject_getitemdata", pyobject_getitemdata, METH_O}, |
| 563 |
{"create_type_with_token", create_type_with_token, METH_VARARGS}, |
| 564 |
{"get_tp_token", get_tp_token, METH_O}, |
| 565 |
{"pytype_getbasebytoken", pytype_getbasebytoken, METH_VARARGS}, |
| 566 |
{"pytype_getmodulebydef", pytype_getmodulebydef, METH_O}, |
| 567 |
{"create_heapctype_with_none_bases_slot", |
| 568 |
create_heapctype_with_none_bases_slot, METH_NOARGS}, |
| 569 |
{NULL}, |
| 570 |
}; |
| 571 |
|
| 572 |
|
| 573 |
PyDoc_STRVAR(heapdocctype__doc__, |
| 574 |
"HeapDocCType(arg1, arg2)\n" |
| 575 |
"--\n" |
| 576 |
"\n" |
| 577 |
"somedoc"); |
| 578 |
|
| 579 |
typedef struct { |
| 580 |
PyObject_HEAD |
| 581 |
} HeapDocCTypeObject; |
| 582 |
|
| 583 |
static PyType_Slot HeapDocCType_slots[] = { |
| 584 |
{Py_tp_doc, (char*)heapdocctype__doc__}, |
| 585 |
{0}, |
| 586 |
}; |
| 587 |
|
| 588 |
static PyType_Spec HeapDocCType_spec = { |
| 589 |
"_testcapi.HeapDocCType", |
| 590 |
sizeof(HeapDocCTypeObject), |
| 591 |
0, |
| 592 |
Py_TPFLAGS_DEFAULT, |
| 593 |
HeapDocCType_slots |
| 594 |
}; |
| 595 |
|
| 596 |
typedef struct { |
| 597 |
PyObject_HEAD |
| 598 |
} NullTpDocTypeObject; |
| 599 |
|
| 600 |
static PyType_Slot NullTpDocType_slots[] = { |
| 601 |
{Py_tp_doc, NULL}, |
| 602 |
{0, 0}, |
| 603 |
}; |
| 604 |
|
| 605 |
static PyType_Spec NullTpDocType_spec = { |
| 606 |
"_testcapi.NullTpDocType", |
| 607 |
sizeof(NullTpDocTypeObject), |
| 608 |
0, |
| 609 |
Py_TPFLAGS_DEFAULT, |
| 610 |
NullTpDocType_slots |
| 611 |
}; |
| 612 |
|
| 613 |
|
| 614 |
PyDoc_STRVAR(heapgctype__doc__, |
| 615 |
"A heap type with GC, and with overridden dealloc.\n\n" |
| 616 |
"The 'value' attribute is set to 10 in __init__."); |
| 617 |
|
| 618 |
typedef struct { |
| 619 |
PyObject_HEAD |
| 620 |
int value; |
| 621 |
} HeapCTypeObject; |
| 622 |
|
| 623 |
static struct PyMemberDef heapctype_members[] = { |
| 624 |
{"value", Py_T_INT, offsetof(HeapCTypeObject, value)}, |
| 625 |
{NULL} /* Sentinel */ |
| 626 |
}; |
| 627 |
|
| 628 |
static int |
| 629 |
heapctype_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 630 |
{ |
| 631 |
((HeapCTypeObject *)self)->value = 10; |
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
static int |
| 636 |
heapgcctype_traverse(PyObject *op, visitproc visit, void *arg) |
| 637 |
{ |
| 638 |
HeapCTypeObject *self = (HeapCTypeObject*)op; |
| 639 |
Py_VISIT(Py_TYPE(self)); |
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
static void |
| 644 |
heapgcctype_dealloc(PyObject *op) |
| 645 |
{ |
| 646 |
HeapCTypeObject *self = (HeapCTypeObject*)op; |
| 647 |
PyTypeObject *tp = Py_TYPE(self); |
| 648 |
PyObject_GC_UnTrack(self); |
| 649 |
PyObject_GC_Del(self); |
| 650 |
Py_DECREF(tp); |
| 651 |
} |
| 652 |
|
| 653 |
static PyType_Slot HeapGcCType_slots[] = { |
| 654 |
{Py_tp_init, heapctype_init}, |
| 655 |
{Py_tp_members, heapctype_members}, |
| 656 |
{Py_tp_dealloc, heapgcctype_dealloc}, |
| 657 |
{Py_tp_traverse, heapgcctype_traverse}, |
| 658 |
{Py_tp_doc, (char*)heapgctype__doc__}, |
| 659 |
{0, 0}, |
| 660 |
}; |
| 661 |
|
| 662 |
static PyType_Spec HeapGcCType_spec = { |
| 663 |
"_testcapi.HeapGcCType", |
| 664 |
sizeof(HeapCTypeObject), |
| 665 |
0, |
| 666 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 667 |
HeapGcCType_slots |
| 668 |
}; |
| 669 |
|
| 670 |
PyDoc_STRVAR(heapctype__doc__, |
| 671 |
"A heap type without GC, but with overridden dealloc.\n\n" |
| 672 |
"The 'value' attribute is set to 10 in __init__."); |
| 673 |
|
| 674 |
static void |
| 675 |
heapctype_dealloc(PyObject *op) |
| 676 |
{ |
| 677 |
HeapCTypeObject *self = (HeapCTypeObject*)op; |
| 678 |
PyTypeObject *tp = Py_TYPE(self); |
| 679 |
PyObject_Free(self); |
| 680 |
Py_DECREF(tp); |
| 681 |
} |
| 682 |
|
| 683 |
static PyType_Slot HeapCType_slots[] = { |
| 684 |
{Py_tp_init, heapctype_init}, |
| 685 |
{Py_tp_members, heapctype_members}, |
| 686 |
{Py_tp_dealloc, heapctype_dealloc}, |
| 687 |
{Py_tp_doc, (char*)heapctype__doc__}, |
| 688 |
{0, 0}, |
| 689 |
}; |
| 690 |
|
| 691 |
static PyType_Spec HeapCType_spec = { |
| 692 |
"_testcapi.HeapCType", |
| 693 |
sizeof(HeapCTypeObject), |
| 694 |
0, |
| 695 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 696 |
HeapCType_slots |
| 697 |
}; |
| 698 |
|
| 699 |
PyDoc_STRVAR(heapctypesubclass__doc__, |
| 700 |
"Subclass of HeapCType, without GC.\n\n" |
| 701 |
"__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 702 |
|
| 703 |
typedef struct { |
| 704 |
HeapCTypeObject base; |
| 705 |
int value2; |
| 706 |
} HeapCTypeSubclassObject; |
| 707 |
|
| 708 |
static int |
| 709 |
heapctypesubclass_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 710 |
{ |
| 711 |
/* Call __init__ of the superclass */ |
| 712 |
if (heapctype_init(self, args, kwargs) < 0) { |
| 713 |
return -1; |
| 714 |
} |
| 715 |
/* Initialize additional element */ |
| 716 |
((HeapCTypeSubclassObject *)self)->value2 = 20; |
| 717 |
return 0; |
| 718 |
} |
| 719 |
|
| 720 |
static struct PyMemberDef heapctypesubclass_members[] = { |
| 721 |
{"value2", Py_T_INT, offsetof(HeapCTypeSubclassObject, value2)}, |
| 722 |
{NULL} /* Sentinel */ |
| 723 |
}; |
| 724 |
|
| 725 |
static PyType_Slot HeapCTypeSubclass_slots[] = { |
| 726 |
{Py_tp_init, heapctypesubclass_init}, |
| 727 |
{Py_tp_members, heapctypesubclass_members}, |
| 728 |
{Py_tp_doc, (char*)heapctypesubclass__doc__}, |
| 729 |
{0, 0}, |
| 730 |
}; |
| 731 |
|
| 732 |
static PyType_Spec HeapCTypeSubclass_spec = { |
| 733 |
"_testcapi.HeapCTypeSubclass", |
| 734 |
sizeof(HeapCTypeSubclassObject), |
| 735 |
0, |
| 736 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 737 |
HeapCTypeSubclass_slots |
| 738 |
}; |
| 739 |
|
| 740 |
PyDoc_STRVAR(heapctypewithbuffer__doc__, |
| 741 |
"Heap type with buffer support.\n\n" |
| 742 |
"The buffer is set to [b'1', b'2', b'3', b'4']"); |
| 743 |
|
| 744 |
typedef struct { |
| 745 |
HeapCTypeObject base; |
| 746 |
char buffer[4]; |
| 747 |
} HeapCTypeWithBufferObject; |
| 748 |
|
| 749 |
static int |
| 750 |
heapctypewithbuffer_getbuffer(PyObject *op, Py_buffer *view, int flags) |
| 751 |
{ |
| 752 |
HeapCTypeWithBufferObject *self = (HeapCTypeWithBufferObject*)op; |
| 753 |
self->buffer[0] = '1'; |
| 754 |
self->buffer[1] = '2'; |
| 755 |
self->buffer[2] = '3'; |
| 756 |
self->buffer[3] = '4'; |
| 757 |
return PyBuffer_FillInfo( |
| 758 |
view, (PyObject*)self, (void *)self->buffer, 4, 1, flags); |
| 759 |
} |
| 760 |
|
| 761 |
static void |
| 762 |
heapctypewithbuffer_releasebuffer(PyObject *op, Py_buffer *view) |
| 763 |
{ |
| 764 |
HeapCTypeWithBufferObject *self = (HeapCTypeWithBufferObject*)op; |
| 765 |
assert(view->obj == (void*) self); |
| 766 |
} |
| 767 |
|
| 768 |
static PyType_Slot HeapCTypeWithBuffer_slots[] = { |
| 769 |
{Py_bf_getbuffer, heapctypewithbuffer_getbuffer}, |
| 770 |
{Py_bf_releasebuffer, heapctypewithbuffer_releasebuffer}, |
| 771 |
{Py_tp_doc, (char*)heapctypewithbuffer__doc__}, |
| 772 |
{0, 0}, |
| 773 |
}; |
| 774 |
|
| 775 |
static PyType_Spec HeapCTypeWithBuffer_spec = { |
| 776 |
"_testcapi.HeapCTypeWithBuffer", |
| 777 |
sizeof(HeapCTypeWithBufferObject), |
| 778 |
0, |
| 779 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 780 |
HeapCTypeWithBuffer_slots |
| 781 |
}; |
| 782 |
|
| 783 |
PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__, |
| 784 |
"Subclass of HeapCType with a finalizer that reassigns __class__.\n\n" |
| 785 |
"__class__ is set to plain HeapCTypeSubclass during finalization.\n" |
| 786 |
"__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 787 |
|
| 788 |
static int |
| 789 |
heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 790 |
{ |
| 791 |
PyTypeObject *base = (PyTypeObject *)PyType_GetSlot(Py_TYPE(self), Py_tp_base); |
| 792 |
initproc base_init = PyType_GetSlot(base, Py_tp_init); |
| 793 |
base_init(self, args, kwargs); |
| 794 |
return 0; |
| 795 |
} |
| 796 |
|
| 797 |
static void |
| 798 |
heapctypesubclasswithfinalizer_finalize(PyObject *self) |
| 799 |
{ |
| 800 |
PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL; |
| 801 |
|
| 802 |
/* Save the current exception, if any. */ |
| 803 |
PyObject *exc = PyErr_GetRaisedException(); |
| 804 |
|
| 805 |
if (_testcapimodule == NULL) { |
| 806 |
goto cleanup_finalize; |
| 807 |
} |
| 808 |
PyObject *m = PyState_FindModule(_testcapimodule); |
| 809 |
if (m == NULL) { |
| 810 |
goto cleanup_finalize; |
| 811 |
} |
| 812 |
oldtype = PyObject_GetAttrString(m, "HeapCTypeSubclassWithFinalizer"); |
| 813 |
if (oldtype == NULL) { |
| 814 |
goto cleanup_finalize; |
| 815 |
} |
| 816 |
newtype = PyObject_GetAttrString(m, "HeapCTypeSubclass"); |
| 817 |
if (newtype == NULL) { |
| 818 |
goto cleanup_finalize; |
| 819 |
} |
| 820 |
|
| 821 |
if (PyObject_SetAttrString(self, "__class__", newtype) < 0) { |
| 822 |
goto cleanup_finalize; |
| 823 |
} |
| 824 |
refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype)); |
| 825 |
if (refcnt == NULL) { |
| 826 |
goto cleanup_finalize; |
| 827 |
} |
| 828 |
if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) { |
| 829 |
goto cleanup_finalize; |
| 830 |
} |
| 831 |
Py_DECREF(refcnt); |
| 832 |
refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype)); |
| 833 |
if (refcnt == NULL) { |
| 834 |
goto cleanup_finalize; |
| 835 |
} |
| 836 |
if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) { |
| 837 |
goto cleanup_finalize; |
| 838 |
} |
| 839 |
|
| 840 |
cleanup_finalize: |
| 841 |
Py_XDECREF(oldtype); |
| 842 |
Py_XDECREF(newtype); |
| 843 |
Py_XDECREF(refcnt); |
| 844 |
|
| 845 |
/* Restore the saved exception. */ |
| 846 |
PyErr_SetRaisedException(exc); |
| 847 |
} |
| 848 |
|
| 849 |
static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = { |
| 850 |
{Py_tp_init, heapctypesubclasswithfinalizer_init}, |
| 851 |
{Py_tp_members, heapctypesubclass_members}, |
| 852 |
{Py_tp_finalize, heapctypesubclasswithfinalizer_finalize}, |
| 853 |
{Py_tp_doc, (char*)heapctypesubclasswithfinalizer__doc__}, |
| 854 |
{0, 0}, |
| 855 |
}; |
| 856 |
|
| 857 |
static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = { |
| 858 |
"_testcapi.HeapCTypeSubclassWithFinalizer", |
| 859 |
sizeof(HeapCTypeSubclassObject), |
| 860 |
0, |
| 861 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 862 |
HeapCTypeSubclassWithFinalizer_slots |
| 863 |
}; |
| 864 |
|
| 865 |
static PyType_Slot HeapCTypeMetaclass_slots[] = { |
| 866 |
{0}, |
| 867 |
}; |
| 868 |
|
| 869 |
static PyType_Spec HeapCTypeMetaclass_spec = { |
| 870 |
"_testcapi.HeapCTypeMetaclass", |
| 871 |
sizeof(PyHeapTypeObject), |
| 872 |
sizeof(PyMemberDef), |
| 873 |
Py_TPFLAGS_DEFAULT, |
| 874 |
HeapCTypeMetaclass_slots |
| 875 |
}; |
| 876 |
|
| 877 |
static PyObject * |
| 878 |
heap_ctype_metaclass_custom_tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs) |
| 879 |
{ |
| 880 |
return PyType_Type.tp_new(tp, args, kwargs); |
| 881 |
} |
| 882 |
|
| 883 |
static PyType_Slot HeapCTypeMetaclassCustomNew_slots[] = { |
| 884 |
{ Py_tp_new, heap_ctype_metaclass_custom_tp_new }, |
| 885 |
{0}, |
| 886 |
}; |
| 887 |
|
| 888 |
static PyType_Spec HeapCTypeMetaclassCustomNew_spec = { |
| 889 |
"_testcapi.HeapCTypeMetaclassCustomNew", |
| 890 |
sizeof(PyHeapTypeObject), |
| 891 |
sizeof(PyMemberDef), |
| 892 |
Py_TPFLAGS_DEFAULT, |
| 893 |
HeapCTypeMetaclassCustomNew_slots |
| 894 |
}; |
| 895 |
|
| 896 |
static PyType_Spec HeapCTypeMetaclassNullNew_spec = { |
| 897 |
.name = "_testcapi.HeapCTypeMetaclassNullNew", |
| 898 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, |
| 899 |
.slots = empty_type_slots |
| 900 |
}; |
| 901 |
|
| 902 |
static PyType_Slot HeapCTypeWithBasesSlot_slots[] = { |
| 903 |
{Py_tp_bases, NULL}, /* filled out in module init function */ |
| 904 |
{0, 0}, |
| 905 |
}; |
| 906 |
|
| 907 |
static PyType_Spec HeapCTypeWithBasesSlot_spec = { |
| 908 |
.name = "_testcapi.HeapCTypeWithBasesSlot", |
| 909 |
.basicsize = sizeof(PyLongObject), |
| 910 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 911 |
.slots = HeapCTypeWithBasesSlot_slots |
| 912 |
}; |
| 913 |
|
| 914 |
|
| 915 |
typedef struct { |
| 916 |
PyObject_HEAD |
| 917 |
PyObject *dict; |
| 918 |
} HeapCTypeWithDictObject; |
| 919 |
|
| 920 |
static void |
| 921 |
heapctypewithdict_dealloc(PyObject *op) |
| 922 |
{ |
| 923 |
HeapCTypeWithDictObject *self = (HeapCTypeWithDictObject*)op; |
| 924 |
PyTypeObject *tp = Py_TYPE(self); |
| 925 |
Py_XDECREF(self->dict); |
| 926 |
PyObject_Free(self); |
| 927 |
Py_DECREF(tp); |
| 928 |
} |
| 929 |
|
| 930 |
static PyGetSetDef heapctypewithdict_getsetlist[] = { |
| 931 |
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
| 932 |
{NULL} /* Sentinel */ |
| 933 |
}; |
| 934 |
|
| 935 |
static struct PyMemberDef heapctypewithdict_members[] = { |
| 936 |
{"dictobj", _Py_T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
| 937 |
{"__dictoffset__", Py_T_PYSSIZET, offsetof(HeapCTypeWithDictObject, dict), Py_READONLY}, |
| 938 |
{NULL} /* Sentinel */ |
| 939 |
}; |
| 940 |
|
| 941 |
static PyType_Slot HeapCTypeWithDict_slots[] = { |
| 942 |
{Py_tp_members, heapctypewithdict_members}, |
| 943 |
{Py_tp_getset, heapctypewithdict_getsetlist}, |
| 944 |
{Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 945 |
{0, 0}, |
| 946 |
}; |
| 947 |
|
| 948 |
static PyType_Spec HeapCTypeWithDict_spec = { |
| 949 |
"_testcapi.HeapCTypeWithDict", |
| 950 |
sizeof(HeapCTypeWithDictObject), |
| 951 |
0, |
| 952 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 953 |
HeapCTypeWithDict_slots |
| 954 |
}; |
| 955 |
|
| 956 |
static PyType_Spec HeapCTypeWithDict2_spec = { |
| 957 |
"_testcapi.HeapCTypeWithDict2", |
| 958 |
sizeof(HeapCTypeWithDictObject), |
| 959 |
0, |
| 960 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 961 |
HeapCTypeWithDict_slots |
| 962 |
}; |
| 963 |
|
| 964 |
static int |
| 965 |
heapmanaged_traverse(PyObject *self, visitproc visit, void *arg) |
| 966 |
{ |
| 967 |
Py_VISIT(Py_TYPE(self)); |
| 968 |
return PyObject_VisitManagedDict((PyObject *)self, visit, arg); |
| 969 |
} |
| 970 |
|
| 971 |
static int |
| 972 |
heapmanaged_clear(PyObject *self) |
| 973 |
{ |
| 974 |
PyObject_ClearManagedDict(self); |
| 975 |
return 0; |
| 976 |
} |
| 977 |
|
| 978 |
static void |
| 979 |
heapmanaged_dealloc(PyObject *op) |
| 980 |
{ |
| 981 |
HeapCTypeObject *self = (HeapCTypeObject*)op; |
| 982 |
PyTypeObject *tp = Py_TYPE(self); |
| 983 |
PyObject_ClearManagedDict((PyObject *)self); |
| 984 |
PyObject_GC_UnTrack(self); |
| 985 |
PyObject_GC_Del(self); |
| 986 |
Py_DECREF(tp); |
| 987 |
} |
| 988 |
|
| 989 |
static PyType_Slot HeapCTypeWithManagedDict_slots[] = { |
| 990 |
{Py_tp_traverse, heapmanaged_traverse}, |
| 991 |
{Py_tp_getset, heapctypewithdict_getsetlist}, |
| 992 |
{Py_tp_clear, heapmanaged_clear}, |
| 993 |
{Py_tp_dealloc, heapmanaged_dealloc}, |
| 994 |
{0, 0}, |
| 995 |
}; |
| 996 |
|
| 997 |
static PyType_Spec HeapCTypeWithManagedDict_spec = { |
| 998 |
"_testcapi.HeapCTypeWithManagedDict", |
| 999 |
sizeof(PyObject), |
| 1000 |
0, |
| 1001 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MANAGED_DICT, |
| 1002 |
HeapCTypeWithManagedDict_slots |
| 1003 |
}; |
| 1004 |
|
| 1005 |
static void |
| 1006 |
heapctypewithmanagedweakref_dealloc(PyObject* self) |
| 1007 |
{ |
| 1008 |
|
| 1009 |
PyTypeObject *tp = Py_TYPE(self); |
| 1010 |
PyObject_ClearWeakRefs(self); |
| 1011 |
PyObject_GC_UnTrack(self); |
| 1012 |
PyObject_GC_Del(self); |
| 1013 |
Py_DECREF(tp); |
| 1014 |
} |
| 1015 |
|
| 1016 |
static PyType_Slot HeapCTypeWithManagedWeakref_slots[] = { |
| 1017 |
{Py_tp_traverse, heapgcctype_traverse}, |
| 1018 |
{Py_tp_getset, heapctypewithdict_getsetlist}, |
| 1019 |
{Py_tp_dealloc, heapctypewithmanagedweakref_dealloc}, |
| 1020 |
{0, 0}, |
| 1021 |
}; |
| 1022 |
|
| 1023 |
static PyType_Spec HeapCTypeWithManagedWeakref_spec = { |
| 1024 |
"_testcapi.HeapCTypeWithManagedWeakref", |
| 1025 |
sizeof(PyObject), |
| 1026 |
0, |
| 1027 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MANAGED_WEAKREF, |
| 1028 |
HeapCTypeWithManagedWeakref_slots |
| 1029 |
}; |
| 1030 |
|
| 1031 |
static struct PyMemberDef heapctypewithnegativedict_members[] = { |
| 1032 |
{"dictobj", _Py_T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
| 1033 |
{"__dictoffset__", Py_T_PYSSIZET, -(Py_ssize_t)sizeof(void*), Py_READONLY}, |
| 1034 |
{NULL} /* Sentinel */ |
| 1035 |
}; |
| 1036 |
|
| 1037 |
static PyType_Slot HeapCTypeWithNegativeDict_slots[] = { |
| 1038 |
{Py_tp_members, heapctypewithnegativedict_members}, |
| 1039 |
{Py_tp_getset, heapctypewithdict_getsetlist}, |
| 1040 |
{Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 1041 |
{0, 0}, |
| 1042 |
}; |
| 1043 |
|
| 1044 |
static PyType_Spec HeapCTypeWithNegativeDict_spec = { |
| 1045 |
"_testcapi.HeapCTypeWithNegativeDict", |
| 1046 |
sizeof(HeapCTypeWithDictObject), |
| 1047 |
0, |
| 1048 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 1049 |
HeapCTypeWithNegativeDict_slots |
| 1050 |
}; |
| 1051 |
|
| 1052 |
typedef struct { |
| 1053 |
PyObject_HEAD |
| 1054 |
PyObject *weakreflist; |
| 1055 |
} HeapCTypeWithWeakrefObject; |
| 1056 |
|
| 1057 |
static struct PyMemberDef heapctypewithweakref_members[] = { |
| 1058 |
{"weakreflist", _Py_T_OBJECT, offsetof(HeapCTypeWithWeakrefObject, weakreflist)}, |
| 1059 |
{"__weaklistoffset__", Py_T_PYSSIZET, |
| 1060 |
offsetof(HeapCTypeWithWeakrefObject, weakreflist), Py_READONLY}, |
| 1061 |
{NULL} /* Sentinel */ |
| 1062 |
}; |
| 1063 |
|
| 1064 |
static void |
| 1065 |
heapctypewithweakref_dealloc(PyObject *op) |
| 1066 |
{ |
| 1067 |
HeapCTypeWithWeakrefObject *self = (HeapCTypeWithWeakrefObject*)op; |
| 1068 |
PyTypeObject *tp = Py_TYPE(self); |
| 1069 |
if (self->weakreflist != NULL) |
| 1070 |
PyObject_ClearWeakRefs((PyObject *) self); |
| 1071 |
Py_XDECREF(self->weakreflist); |
| 1072 |
PyObject_Free(self); |
| 1073 |
Py_DECREF(tp); |
| 1074 |
} |
| 1075 |
|
| 1076 |
static PyType_Slot HeapCTypeWithWeakref_slots[] = { |
| 1077 |
{Py_tp_members, heapctypewithweakref_members}, |
| 1078 |
{Py_tp_dealloc, heapctypewithweakref_dealloc}, |
| 1079 |
{0, 0}, |
| 1080 |
}; |
| 1081 |
|
| 1082 |
static PyType_Spec HeapCTypeWithWeakref_spec = { |
| 1083 |
"_testcapi.HeapCTypeWithWeakref", |
| 1084 |
sizeof(HeapCTypeWithWeakrefObject), |
| 1085 |
0, |
| 1086 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 1087 |
HeapCTypeWithWeakref_slots |
| 1088 |
}; |
| 1089 |
|
| 1090 |
static PyType_Spec HeapCTypeWithWeakref2_spec = { |
| 1091 |
"_testcapi.HeapCTypeWithWeakref2", |
| 1092 |
sizeof(HeapCTypeWithWeakrefObject), |
| 1093 |
0, |
| 1094 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 1095 |
HeapCTypeWithWeakref_slots |
| 1096 |
}; |
| 1097 |
|
| 1098 |
PyDoc_STRVAR(heapctypesetattr__doc__, |
| 1099 |
"A heap type without GC, but with overridden __setattr__.\n\n" |
| 1100 |
"The 'value' attribute is set to 10 in __init__ and updated via attribute setting."); |
| 1101 |
|
| 1102 |
typedef struct { |
| 1103 |
PyObject_HEAD |
| 1104 |
long value; |
| 1105 |
} HeapCTypeSetattrObject; |
| 1106 |
|
| 1107 |
static struct PyMemberDef heapctypesetattr_members[] = { |
| 1108 |
{"pvalue", Py_T_LONG, offsetof(HeapCTypeSetattrObject, value)}, |
| 1109 |
{NULL} /* Sentinel */ |
| 1110 |
}; |
| 1111 |
|
| 1112 |
static int |
| 1113 |
heapctypesetattr_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1114 |
{ |
| 1115 |
((HeapCTypeSetattrObject *)self)->value = 10; |
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
|
| 1119 |
static void |
| 1120 |
heapctypesetattr_dealloc(PyObject *op) |
| 1121 |
{ |
| 1122 |
HeapCTypeSetattrObject *self = (HeapCTypeSetattrObject*)op; |
| 1123 |
PyTypeObject *tp = Py_TYPE(self); |
| 1124 |
PyObject_Free(self); |
| 1125 |
Py_DECREF(tp); |
| 1126 |
} |
| 1127 |
|
| 1128 |
static int |
| 1129 |
heapctypesetattr_setattro(PyObject *op, PyObject *attr, PyObject *value) |
| 1130 |
{ |
| 1131 |
HeapCTypeSetattrObject *self = (HeapCTypeSetattrObject*)op; |
| 1132 |
PyObject *svalue = PyUnicode_FromString("value"); |
| 1133 |
if (svalue == NULL) |
| 1134 |
return -1; |
| 1135 |
int eq = PyObject_RichCompareBool(svalue, attr, Py_EQ); |
| 1136 |
Py_DECREF(svalue); |
| 1137 |
if (eq < 0) |
| 1138 |
return -1; |
| 1139 |
if (!eq) { |
| 1140 |
return PyObject_GenericSetAttr((PyObject*) self, attr, value); |
| 1141 |
} |
| 1142 |
if (value == NULL) { |
| 1143 |
self->value = 0; |
| 1144 |
return 0; |
| 1145 |
} |
| 1146 |
PyObject *ivalue = PyNumber_Long(value); |
| 1147 |
if (ivalue == NULL) |
| 1148 |
return -1; |
| 1149 |
long v = PyLong_AsLong(ivalue); |
| 1150 |
Py_DECREF(ivalue); |
| 1151 |
if (v == -1 && PyErr_Occurred()) |
| 1152 |
return -1; |
| 1153 |
self->value = v; |
| 1154 |
return 0; |
| 1155 |
} |
| 1156 |
|
| 1157 |
static PyType_Slot HeapCTypeSetattr_slots[] = { |
| 1158 |
{Py_tp_init, heapctypesetattr_init}, |
| 1159 |
{Py_tp_members, heapctypesetattr_members}, |
| 1160 |
{Py_tp_setattro, heapctypesetattr_setattro}, |
| 1161 |
{Py_tp_dealloc, heapctypesetattr_dealloc}, |
| 1162 |
{Py_tp_doc, (char*)heapctypesetattr__doc__}, |
| 1163 |
{0, 0}, |
| 1164 |
}; |
| 1165 |
|
| 1166 |
static PyType_Spec HeapCTypeSetattr_spec = { |
| 1167 |
"_testcapi.HeapCTypeSetattr", |
| 1168 |
sizeof(HeapCTypeSetattrObject), |
| 1169 |
0, |
| 1170 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 1171 |
HeapCTypeSetattr_slots |
| 1172 |
}; |
| 1173 |
|
| 1174 |
/* |
| 1175 |
* The code below is for a test that uses PyType_FromSpec API to create a heap |
| 1176 |
* type that simultaneously exposes |
| 1177 |
* |
| 1178 |
* - A regular __new__ / __init__ constructor pair |
| 1179 |
* - A vector call handler in the type object |
| 1180 |
* |
| 1181 |
* A general requirement of vector call implementations is that they should |
| 1182 |
* behave identically (except being potentially faster). The example below |
| 1183 |
* deviates from this rule by initializing the instance with a different value. |
| 1184 |
* This is only done here only so that we can see which path was taken and is |
| 1185 |
* strongly discouraged in other cases. |
| 1186 |
*/ |
| 1187 |
|
| 1188 |
typedef struct { |
| 1189 |
PyObject_HEAD |
| 1190 |
long value; |
| 1191 |
} HeapCTypeVectorcallObject; |
| 1192 |
|
| 1193 |
static PyObject *heapctype_vectorcall_vectorcall(PyObject *self, |
| 1194 |
PyObject *const *args_in, |
| 1195 |
size_t nargsf, |
| 1196 |
PyObject *kwargs_in) |
| 1197 |
{ |
| 1198 |
if (kwargs_in || PyVectorcall_NARGS(nargsf)) { |
| 1199 |
return PyErr_Format(PyExc_IndexError, "HeapCTypeVectorcall() takes no arguments!"); |
| 1200 |
} |
| 1201 |
|
| 1202 |
HeapCTypeVectorcallObject *r = |
| 1203 |
PyObject_New(HeapCTypeVectorcallObject, (PyTypeObject *) self); |
| 1204 |
|
| 1205 |
if (!r) { |
| 1206 |
return NULL; |
| 1207 |
} |
| 1208 |
|
| 1209 |
r->value = 1; |
| 1210 |
|
| 1211 |
return (PyObject *) r; |
| 1212 |
} |
| 1213 |
|
| 1214 |
static PyObject * |
| 1215 |
heapctype_vectorcall_new(PyTypeObject* type, PyObject* args, PyObject *kwargs) |
| 1216 |
{ |
| 1217 |
if (PyTuple_GET_SIZE(args) || kwargs) { |
| 1218 |
return PyErr_Format(PyExc_IndexError, "HeapCTypeVectorcall() takes no arguments!"); |
| 1219 |
} |
| 1220 |
|
| 1221 |
return (PyObject *) PyObject_New(HeapCTypeVectorcallObject, type); |
| 1222 |
} |
| 1223 |
|
| 1224 |
static int |
| 1225 |
heapctype_vectorcall_init(PyObject *self, PyObject *args, PyObject *kwargs) { |
| 1226 |
if (PyTuple_GET_SIZE(args) || kwargs) { |
| 1227 |
PyErr_Format(PyExc_IndexError, "HeapCTypeVectorcall() takes no arguments!"); |
| 1228 |
return -1; |
| 1229 |
} |
| 1230 |
|
| 1231 |
HeapCTypeVectorcallObject *o = (HeapCTypeVectorcallObject *) self; |
| 1232 |
o->value = 2; |
| 1233 |
return 0; |
| 1234 |
} |
| 1235 |
|
| 1236 |
static struct PyMemberDef heapctype_vectorcall_members[] = { |
| 1237 |
{"value", Py_T_LONG, offsetof(HeapCTypeVectorcallObject, value), 0, NULL}, |
| 1238 |
{NULL} |
| 1239 |
}; |
| 1240 |
|
| 1241 |
static PyType_Slot HeapCTypeVectorcall_slots[] = { |
| 1242 |
{Py_tp_new, heapctype_vectorcall_new}, |
| 1243 |
{Py_tp_init, heapctype_vectorcall_init}, |
| 1244 |
{Py_tp_vectorcall, heapctype_vectorcall_vectorcall}, |
| 1245 |
{Py_tp_members, heapctype_vectorcall_members}, |
| 1246 |
{0, 0}, |
| 1247 |
}; |
| 1248 |
|
| 1249 |
static PyType_Spec HeapCTypeVectorcall_spec = { |
| 1250 |
"_testcapi.HeapCTypeVectorcall", |
| 1251 |
sizeof(HeapCTypeVectorcallObject), |
| 1252 |
0, |
| 1253 |
Py_TPFLAGS_DEFAULT, |
| 1254 |
HeapCTypeVectorcall_slots |
| 1255 |
}; |
| 1256 |
|
| 1257 |
PyDoc_STRVAR(HeapCCollection_doc, |
| 1258 |
"Tuple-like heap type that uses PyObject_GetItemData for items."); |
| 1259 |
|
| 1260 |
static PyObject* |
| 1261 |
HeapCCollection_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
| 1262 |
{ |
| 1263 |
PyObject *self = NULL; |
| 1264 |
PyObject *result = NULL; |
| 1265 |
|
| 1266 |
Py_ssize_t size = PyTuple_GET_SIZE(args); |
| 1267 |
self = subtype->tp_alloc(subtype, size); |
| 1268 |
if (!self) { |
| 1269 |
goto finally; |
| 1270 |
} |
| 1271 |
PyObject **data = PyObject_GetItemData(self); |
| 1272 |
if (!data) { |
| 1273 |
goto finally; |
| 1274 |
} |
| 1275 |
|
| 1276 |
for (Py_ssize_t i = 0; i < size; i++) { |
| 1277 |
data[i] = Py_NewRef(PyTuple_GET_ITEM(args, i)); |
| 1278 |
} |
| 1279 |
|
| 1280 |
result = self; |
| 1281 |
self = NULL; |
| 1282 |
finally: |
| 1283 |
Py_XDECREF(self); |
| 1284 |
return result; |
| 1285 |
} |
| 1286 |
|
| 1287 |
static Py_ssize_t |
| 1288 |
HeapCCollection_length(PyObject *op) |
| 1289 |
{ |
| 1290 |
PyVarObject *self = (PyVarObject*)op; |
| 1291 |
return Py_SIZE(self); |
| 1292 |
} |
| 1293 |
|
| 1294 |
static PyObject* |
| 1295 |
HeapCCollection_item(PyObject *self, Py_ssize_t i) |
| 1296 |
{ |
| 1297 |
if (i < 0 || i >= Py_SIZE(self)) { |
| 1298 |
return PyErr_Format(PyExc_IndexError, "index %zd out of range", i); |
| 1299 |
} |
| 1300 |
PyObject **data = PyObject_GetItemData(self); |
| 1301 |
if (!data) { |
| 1302 |
return NULL; |
| 1303 |
} |
| 1304 |
return Py_NewRef(data[i]); |
| 1305 |
} |
| 1306 |
|
| 1307 |
static int |
| 1308 |
HeapCCollection_traverse(PyObject *self, visitproc visit, void *arg) |
| 1309 |
{ |
| 1310 |
PyObject **data = PyObject_GetItemData(self); |
| 1311 |
if (!data) { |
| 1312 |
return -1; |
| 1313 |
} |
| 1314 |
for (Py_ssize_t i = 0; i < Py_SIZE(self); i++) { |
| 1315 |
Py_VISIT(data[i]); |
| 1316 |
} |
| 1317 |
return 0; |
| 1318 |
} |
| 1319 |
|
| 1320 |
static int |
| 1321 |
HeapCCollection_clear(PyObject *self) |
| 1322 |
{ |
| 1323 |
PyObject **data = PyObject_GetItemData(self); |
| 1324 |
if (!data) { |
| 1325 |
return -1; |
| 1326 |
} |
| 1327 |
Py_ssize_t size = Py_SIZE(self); |
| 1328 |
Py_SET_SIZE(self, 0); |
| 1329 |
for (Py_ssize_t i = 0; i < size; i++) { |
| 1330 |
Py_CLEAR(data[i]); |
| 1331 |
} |
| 1332 |
return 0; |
| 1333 |
} |
| 1334 |
|
| 1335 |
static void |
| 1336 |
HeapCCollection_dealloc(PyObject *self) |
| 1337 |
{ |
| 1338 |
PyTypeObject *tp = Py_TYPE(self); |
| 1339 |
HeapCCollection_clear(self); |
| 1340 |
PyObject_GC_UnTrack(self); |
| 1341 |
tp->tp_free(self); |
| 1342 |
Py_DECREF(tp); |
| 1343 |
} |
| 1344 |
|
| 1345 |
static PyType_Slot HeapCCollection_slots[] = { |
| 1346 |
{Py_tp_new, HeapCCollection_new}, |
| 1347 |
{Py_sq_length, HeapCCollection_length}, |
| 1348 |
{Py_sq_item, HeapCCollection_item}, |
| 1349 |
{Py_tp_traverse, HeapCCollection_traverse}, |
| 1350 |
{Py_tp_clear, HeapCCollection_clear}, |
| 1351 |
{Py_tp_dealloc, HeapCCollection_dealloc}, |
| 1352 |
{Py_tp_doc, (void *)HeapCCollection_doc}, |
| 1353 |
{0, 0}, |
| 1354 |
}; |
| 1355 |
|
| 1356 |
static PyType_Spec HeapCCollection_spec = { |
| 1357 |
.name = "_testcapi.HeapCCollection", |
| 1358 |
.basicsize = sizeof(PyVarObject), |
| 1359 |
.itemsize = sizeof(PyObject*), |
| 1360 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 1361 |
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_ITEMS_AT_END), |
| 1362 |
.slots = HeapCCollection_slots, |
| 1363 |
}; |
| 1364 |
|
| 1365 |
int |
| 1366 |
_PyTestCapi_Init_Heaptype(PyObject *m) { |
| 1367 |
_testcapimodule = PyModule_GetDef(m); |
| 1368 |
|
| 1369 |
if (PyModule_AddFunctions(m, TestMethods) < 0) { |
| 1370 |
return -1; |
| 1371 |
} |
| 1372 |
|
| 1373 |
#define ADD(name, value) do { \ |
| 1374 |
if (PyModule_Add(m, name, value) < 0) { \ |
| 1375 |
return -1; \ |
| 1376 |
} \ |
| 1377 |
} while (0) |
| 1378 |
|
| 1379 |
PyObject *HeapDocCType = PyType_FromSpec(&HeapDocCType_spec); |
| 1380 |
ADD("HeapDocCType", HeapDocCType); |
| 1381 |
|
| 1382 |
/* bpo-41832: Add a new type to test PyType_FromSpec() |
| 1383 |
now can accept a NULL tp_doc slot. */ |
| 1384 |
PyObject *NullTpDocType = PyType_FromSpec(&NullTpDocType_spec); |
| 1385 |
ADD("NullTpDocType", NullTpDocType); |
| 1386 |
|
| 1387 |
PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec); |
| 1388 |
ADD("HeapGcCType", HeapGcCType); |
| 1389 |
|
| 1390 |
PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec); |
| 1391 |
if (HeapCType == NULL) { |
| 1392 |
return -1; |
| 1393 |
} |
| 1394 |
PyObject *subclass_bases = PyTuple_Pack(1, HeapCType); |
| 1395 |
Py_DECREF(HeapCType); |
| 1396 |
if (subclass_bases == NULL) { |
| 1397 |
return -1; |
| 1398 |
} |
| 1399 |
PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases); |
| 1400 |
Py_DECREF(subclass_bases); |
| 1401 |
ADD("HeapCTypeSubclass", HeapCTypeSubclass); |
| 1402 |
|
| 1403 |
PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec); |
| 1404 |
ADD("HeapCTypeWithDict", HeapCTypeWithDict); |
| 1405 |
|
| 1406 |
PyObject *HeapCTypeWithDict2 = PyType_FromSpec(&HeapCTypeWithDict2_spec); |
| 1407 |
ADD("HeapCTypeWithDict2", HeapCTypeWithDict2); |
| 1408 |
|
| 1409 |
PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec); |
| 1410 |
ADD("HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict); |
| 1411 |
|
| 1412 |
PyObject *HeapCTypeWithManagedDict = PyType_FromSpec(&HeapCTypeWithManagedDict_spec); |
| 1413 |
ADD("HeapCTypeWithManagedDict", HeapCTypeWithManagedDict); |
| 1414 |
|
| 1415 |
PyObject *HeapCTypeWithManagedWeakref = PyType_FromSpec(&HeapCTypeWithManagedWeakref_spec); |
| 1416 |
ADD("HeapCTypeWithManagedWeakref", HeapCTypeWithManagedWeakref); |
| 1417 |
|
| 1418 |
PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec); |
| 1419 |
ADD("HeapCTypeWithWeakref", HeapCTypeWithWeakref); |
| 1420 |
|
| 1421 |
PyObject *HeapCTypeWithWeakref2 = PyType_FromSpec(&HeapCTypeWithWeakref2_spec); |
| 1422 |
ADD("HeapCTypeWithWeakref2", HeapCTypeWithWeakref2); |
| 1423 |
|
| 1424 |
PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec); |
| 1425 |
ADD("HeapCTypeWithBuffer", HeapCTypeWithBuffer); |
| 1426 |
|
| 1427 |
PyObject *HeapCTypeSetattr = PyType_FromSpec(&HeapCTypeSetattr_spec); |
| 1428 |
ADD("HeapCTypeSetattr", HeapCTypeSetattr); |
| 1429 |
|
| 1430 |
PyObject *HeapCTypeVectorcall = PyType_FromSpec(&HeapCTypeVectorcall_spec); |
| 1431 |
ADD("HeapCTypeVectorcall", HeapCTypeVectorcall); |
| 1432 |
|
| 1433 |
PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass); |
| 1434 |
if (subclass_with_finalizer_bases == NULL) { |
| 1435 |
return -1; |
| 1436 |
} |
| 1437 |
PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases( |
| 1438 |
&HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases); |
| 1439 |
Py_DECREF(subclass_with_finalizer_bases); |
| 1440 |
ADD("HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer); |
| 1441 |
|
| 1442 |
PyObject *HeapCTypeMetaclass = PyType_FromMetaclass( |
| 1443 |
&PyType_Type, m, &HeapCTypeMetaclass_spec, (PyObject *) &PyType_Type); |
| 1444 |
ADD("HeapCTypeMetaclass", HeapCTypeMetaclass); |
| 1445 |
|
| 1446 |
PyObject *HeapCTypeMetaclassCustomNew = PyType_FromMetaclass( |
| 1447 |
&PyType_Type, m, &HeapCTypeMetaclassCustomNew_spec, (PyObject *) &PyType_Type); |
| 1448 |
ADD("HeapCTypeMetaclassCustomNew", HeapCTypeMetaclassCustomNew); |
| 1449 |
|
| 1450 |
PyObject *HeapCTypeMetaclassNullNew = PyType_FromMetaclass( |
| 1451 |
&PyType_Type, m, &HeapCTypeMetaclassNullNew_spec, (PyObject *) &PyType_Type); |
| 1452 |
ADD("HeapCTypeMetaclassNullNew", HeapCTypeMetaclassNullNew); |
| 1453 |
|
| 1454 |
PyObject *bases = PyTuple_Pack(1, &PyLong_Type); |
| 1455 |
if (bases == NULL) { |
| 1456 |
return -1; |
| 1457 |
} |
| 1458 |
HeapCTypeWithBasesSlot_slots[0].pfunc = bases; |
| 1459 |
PyObject *HeapCTypeWithBasesSlot = PyType_FromSpec(&HeapCTypeWithBasesSlot_spec); |
| 1460 |
Py_DECREF(bases); |
| 1461 |
if (HeapCTypeWithBasesSlot == NULL) { |
| 1462 |
return -1; |
| 1463 |
} |
| 1464 |
ADD("HeapCTypeWithBasesSlot", HeapCTypeWithBasesSlot); |
| 1465 |
|
| 1466 |
ADD("Py_TP_USE_SPEC", PyLong_FromVoidPtr(Py_TP_USE_SPEC)); |
| 1467 |
|
| 1468 |
PyObject *HeapCCollection = PyType_FromMetaclass( |
| 1469 |
NULL, m, &HeapCCollection_spec, NULL); |
| 1470 |
if (HeapCCollection == NULL) { |
| 1471 |
return -1; |
| 1472 |
} |
| 1473 |
int rc = PyModule_AddType(m, (PyTypeObject *)HeapCCollection); |
| 1474 |
Py_DECREF(HeapCCollection); |
| 1475 |
if (rc < 0) { |
| 1476 |
return -1; |
| 1477 |
} |
| 1478 |
|
| 1479 |
return 0; |
| 1480 |
} |