[3.14] gh-129849: Add tests for Py_tp_bases (GH-143208) (#146225)

(cherry picked from commit 6f8867a676)
This commit is contained in:
AN Long 2026-03-21 17:52:58 +08:00 committed by GitHub
parent 73e74eeb2f
commit 6bd5992d8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 0 deletions

View file

@ -915,6 +915,18 @@ def genf(): yield
gen = genf()
self.assertEqual(_testcapi.gen_get_code(gen), gen.gi_code)
def test_tp_bases_slot(self):
cls = _testcapi.HeapCTypeWithBasesSlot
self.assertEqual(cls.__bases__, (int,))
self.assertEqual(cls.__base__, int)
def test_tp_bases_slot_none(self):
self.assertRaisesRegex(
SystemError,
"Py_tp_bases is not a tuple",
_testcapi.create_heapctype_with_none_bases_slot
)
@requires_limited_api
class TestHeapTypeRelative(unittest.TestCase):

View file

@ -528,6 +528,24 @@ pytype_getmodulebydef(PyObject *self, PyObject *type)
return Py_XNewRef(mod);
}
static PyType_Slot HeapCTypeWithBasesSlotNone_slots[] = {
{Py_tp_bases, NULL}, /* filled out with Py_None in runtime */
{0, 0},
};
static PyType_Spec HeapCTypeWithBasesSlotNone_spec = {
.name = "_testcapi.HeapCTypeWithBasesSlotNone",
.basicsize = sizeof(PyObject),
.flags = Py_TPFLAGS_DEFAULT,
.slots = HeapCTypeWithBasesSlotNone_slots
};
static PyObject *
create_heapctype_with_none_bases_slot(PyObject *self, PyObject *Py_UNUSED(ignored))
{
HeapCTypeWithBasesSlotNone_slots[0].pfunc = Py_None;
return PyType_FromSpec(&HeapCTypeWithBasesSlotNone_spec);
}
static PyMethodDef TestMethods[] = {
{"pytype_fromspec_meta", pytype_fromspec_meta, METH_O},
@ -546,6 +564,8 @@ static PyMethodDef TestMethods[] = {
{"get_tp_token", get_tp_token, METH_O},
{"pytype_getbasebytoken", pytype_getbasebytoken, METH_VARARGS},
{"pytype_getmodulebydef", pytype_getmodulebydef, METH_O},
{"create_heapctype_with_none_bases_slot",
create_heapctype_with_none_bases_slot, METH_NOARGS},
{NULL},
};
@ -879,6 +899,18 @@ static PyType_Spec HeapCTypeMetaclassNullNew_spec = {
.slots = empty_type_slots
};
static PyType_Slot HeapCTypeWithBasesSlot_slots[] = {
{Py_tp_bases, NULL}, /* filled out in module init function */
{0, 0},
};
static PyType_Spec HeapCTypeWithBasesSlot_spec = {
.name = "_testcapi.HeapCTypeWithBasesSlot",
.basicsize = sizeof(PyLongObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.slots = HeapCTypeWithBasesSlot_slots
};
typedef struct {
PyObject_HEAD
@ -1419,6 +1451,18 @@ _PyTestCapi_Init_Heaptype(PyObject *m) {
&PyType_Type, m, &HeapCTypeMetaclassNullNew_spec, (PyObject *) &PyType_Type);
ADD("HeapCTypeMetaclassNullNew", HeapCTypeMetaclassNullNew);
PyObject *bases = PyTuple_Pack(1, &PyLong_Type);
if (bases == NULL) {
return -1;
}
HeapCTypeWithBasesSlot_slots[0].pfunc = bases;
PyObject *HeapCTypeWithBasesSlot = PyType_FromSpec(&HeapCTypeWithBasesSlot_spec);
Py_DECREF(bases);
if (HeapCTypeWithBasesSlot == NULL) {
return -1;
}
ADD("HeapCTypeWithBasesSlot", HeapCTypeWithBasesSlot);
ADD("Py_TP_USE_SPEC", PyLong_FromVoidPtr(Py_TP_USE_SPEC));
PyObject *HeapCCollection = PyType_FromMetaclass(