gh-76785: Add _PyType_GetModuleName() to the Internal C-API (gh-112323)

The new function corresponds to the existing (public) PyType_GetName() and PyType_GetQualName().
This commit is contained in:
Eric Snow 2023-11-22 15:03:33 -07:00 committed by GitHub
parent 5c3a129ecf
commit 790db85c77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 0 deletions

View File

@ -143,6 +143,11 @@ extern PyTypeObject _PyBufferWrapper_Type;
extern PyObject* _PySuper_Lookup(PyTypeObject *su_type, PyObject *su_obj, extern PyObject* _PySuper_Lookup(PyTypeObject *su_type, PyObject *su_obj,
PyObject *name, int *meth_found); PyObject *name, int *meth_found);
// This is exported for the _testinternalcapi module.
PyAPI_FUNC(PyObject *) _PyType_GetModuleName(PyTypeObject *);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1098,6 +1098,21 @@ class CAPITest(unittest.TestCase):
del d.extra del d.extra
self.assertIsNone(d.extra) self.assertIsNone(d.extra)
def test_get_type_module_name(self):
from collections import OrderedDict
ht = _testcapi.get_heaptype_for_name()
for cls, expected in {
int: 'builtins',
OrderedDict: 'collections',
ht: '_testcapi',
}.items():
with self.subTest(repr(cls)):
modname = _testinternalcapi.get_type_module_name(cls)
self.assertEqual(modname, expected)
ht.__module__ = 'test_module'
modname = _testinternalcapi.get_type_module_name(ht)
self.assertEqual(modname, 'test_module')
@requires_limited_api @requires_limited_api
class TestHeapTypeRelative(unittest.TestCase): class TestHeapTypeRelative(unittest.TestCase):

View File

@ -569,6 +569,12 @@ static PyType_Spec HeapTypeNameType_Spec = {
.slots = HeapTypeNameType_slots, .slots = HeapTypeNameType_slots,
}; };
static PyObject *
get_heaptype_for_name(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return PyType_FromSpec(&HeapTypeNameType_Spec);
}
static PyObject * static PyObject *
test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored)) test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
{ {
@ -3210,6 +3216,7 @@ static PyMethodDef TestMethods[] = {
{"py_buildvalue_ints", py_buildvalue_ints, METH_VARARGS}, {"py_buildvalue_ints", py_buildvalue_ints, METH_VARARGS},
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS}, {"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
{"get_heaptype_for_name", get_heaptype_for_name, METH_NOARGS},
{"test_get_type_name", test_get_type_name, METH_NOARGS}, {"test_get_type_name", test_get_type_name, METH_NOARGS},
{"test_get_type_qualname", test_get_type_qualname, METH_NOARGS}, {"test_get_type_qualname", test_get_type_qualname, METH_NOARGS},
{"test_get_type_dict", test_get_type_dict, METH_NOARGS}, {"test_get_type_dict", test_get_type_dict, METH_NOARGS},

View File

@ -27,6 +27,7 @@
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal() #include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_typeobject.h" // _PyType_GetModuleName()
#include "interpreteridobject.h" // PyInterpreterID_LookUp() #include "interpreteridobject.h" // PyInterpreterID_LookUp()
@ -1616,6 +1617,14 @@ perf_trampoline_set_persist_after_fork(PyObject *self, PyObject *args)
} }
static PyObject *
get_type_module_name(PyObject *self, PyObject *type)
{
assert(PyType_Check(type));
return _PyType_GetModuleName((PyTypeObject *)type);
}
static PyMethodDef module_functions[] = { static PyMethodDef module_functions[] = {
{"get_configs", get_configs, METH_NOARGS}, {"get_configs", get_configs, METH_NOARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@ -1678,6 +1687,7 @@ static PyMethodDef module_functions[] = {
{"get_crossinterp_data", get_crossinterp_data, METH_VARARGS}, {"get_crossinterp_data", get_crossinterp_data, METH_VARARGS},
{"restore_crossinterp_data", restore_crossinterp_data, METH_VARARGS}, {"restore_crossinterp_data", restore_crossinterp_data, METH_VARARGS},
_TESTINTERNALCAPI_TEST_LONG_NUMBITS_METHODDEF _TESTINTERNALCAPI_TEST_LONG_NUMBITS_METHODDEF
{"get_type_module_name", get_type_module_name, METH_O},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };

View File

@ -4540,6 +4540,12 @@ PyType_GetQualName(PyTypeObject *type)
return type_qualname(type, NULL); return type_qualname(type, NULL);
} }
PyObject *
_PyType_GetModuleName(PyTypeObject *type)
{
return type_module(type, NULL);
}
void * void *
PyType_GetSlot(PyTypeObject *type, int slot) PyType_GetSlot(PyTypeObject *type, int slot)
{ {