Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a test to avoid more regressions.
  • Loading branch information
ericsnowcurrently committed May 30, 2023
commit e054bbe9b0472047a6c02403d51fe01c97169ca4
33 changes: 33 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,39 @@ def test_module_state_shared_in_global(self):
self.assertEqual(main_attr_id, subinterp_attr_id)


class BuiltinStaticTypesTests(unittest.TestCase):

TYPES = [
object,
type,
int,
str,
dict,
type(None),
bool,
BaseException,
Exception,
Warning,
DeprecationWarning, # Warning subclass
]

def test_tp_bases_is_set(self):
# PyTypeObject.tp_bases is documented as public API.
# See https://github.com/python/cpython/issues/105020.
for typeobj in self.TYPES:
with self.subTest(typeobj):
bases = _testcapi.type_get_tp_bases(typeobj)
self.assertIsNot(bases, None)

def test_tp_mro_is_set(self):
# PyTypeObject.tp_bases is documented as public API.
# See https://github.com/python/cpython/issues/105020.
for typeobj in self.TYPES:
with self.subTest(typeobj):
mro = _testcapi.type_get_tp_mro(typeobj)
self.assertIsNot(mro, None)


class TestThreadState(unittest.TestCase):

@threading_helper.reap_threads
Expand Down
23 changes: 23 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,27 @@ type_assign_version(PyObject *self, PyObject *type)
}


static PyObject *
type_get_tp_bases(PyObject *self, PyObject *type)
{
PyObject *bases = ((PyTypeObject *)type)->tp_bases;
if (bases == NULL) {
Py_RETURN_NONE;
}
return Py_NewRef(bases);
}

static PyObject *
type_get_tp_mro(PyObject *self, PyObject *type)
{
PyObject *mro = ((PyTypeObject *)type)->tp_mro;
if (mro == NULL) {
Py_RETURN_NONE;
}
return Py_NewRef(mro);
}


// Test PyThreadState C API
static PyObject *
test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args))
Expand Down Expand Up @@ -3361,6 +3382,8 @@ static PyMethodDef TestMethods[] = {
{"test_py_is_funcs", test_py_is_funcs, METH_NOARGS},
{"type_get_version", type_get_version, METH_O, PyDoc_STR("type->tp_version_tag")},
{"type_assign_version", type_assign_version, METH_O, PyDoc_STR("PyUnstable_Type_AssignVersionTag")},
{"type_get_tp_bases", type_get_tp_bases, METH_O},
{"type_get_tp_mro", type_get_tp_mro, METH_O},
{"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
{"frame_getlocals", frame_getlocals, METH_O, NULL},
{"frame_getglobals", frame_getglobals, METH_O, NULL},
Expand Down