Skip to content

bpo-42035: Add a PyType_GetName() to get type's short name. #23903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ Type Objects
GC protocol itself by at least implementing the
:c:member:`~PyTypeObject.tp_traverse` handle.

.. c:function:: PyObject* PyType_GetName(PyTypeObject *type)

Return the type's name. Equivalent to getting the type's ``__name__`` attribute.

.. versionadded:: 3.11

.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)

Return the function pointer stored in the given slot. If the
Expand Down
3 changes: 3 additions & 0 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,9 @@ PyType_GenericNew:PyObject*:kwds:0:
PyType_GetFlags:unsigned long:::
PyType_GetFlags:PyTypeObject*:type:0:

PyType_GetName:PyObject*::+1:
PyType_GetName:PyTypeObject*:type:0:

PyType_GetSlot:void*:::
PyType_GetSlot:PyTypeObject*:type:0:
PyType_GetSlot:int:slot::
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ function,PyType_GenericNew,3.2,
function,PyType_GetFlags,3.2,
function,PyType_GetModule,3.10,
function,PyType_GetModuleState,3.10,
function,PyType_GetName,3.11,
function,PyType_GetSlot,3.4,
function,PyType_IsSubtype,3.2,
function,PyType_Modified,3.2,
Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ Changes in the Python API

C API Changes
=============
* Add a new :c:func:`PyType_GetName` function to get type's short name.
(Contributed by Hai Shi in :issue:`42035`.)

New Features
------------
Expand Down
3 changes: 3 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObje
PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
#endif

/* Generic type check */
PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a new :c:func:`PyType_GetName` function to get type's short name.
4 changes: 4 additions & 0 deletions Misc/stable_abi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,10 @@ function PyGC_Enable
function PyGC_IsEnabled
added 3.10

# Add new C API in Python 3.11

function PyType_GetName
added 3.11

# (Detailed comments aren't really needed for further entries: from here on
# we can use version control logs.)
41 changes: 41 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#endif

static struct PyModuleDef _testcapimodule;
static PyType_Spec HeapTypeNameType_Spec;

static PyObject *TestError; /* set to exception object in init */

Expand Down Expand Up @@ -1134,6 +1135,30 @@ test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
}


static PyObject *
test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *tp_name = PyType_GetName(&PyLong_Type);
assert(strcmp(PyUnicode_AsUTF8(tp_name), "int") == 0);
Py_DECREF(tp_name);

tp_name = PyType_GetName(&PyModule_Type);
assert(strcmp(PyUnicode_AsUTF8(tp_name), "module") == 0);
Py_DECREF(tp_name);

PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
if (HeapTypeNameType == NULL) {
Py_RETURN_NONE;
}
tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_name), "HeapTypeNameType") == 0);
Py_DECREF(tp_name);

Py_DECREF(HeapTypeNameType);
Py_RETURN_NONE;
}


static PyObject *
get_args(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -5624,6 +5649,7 @@ static PyMethodDef TestMethods[] = {
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
{"get_args", get_args, METH_VARARGS},
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
{"test_get_type_name", test_get_type_name, METH_NOARGS},
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
METH_VARARGS|METH_KEYWORDS},
{"getargs_tuple", getargs_tuple, METH_VARARGS},
Expand Down Expand Up @@ -6512,6 +6538,21 @@ static PyType_Spec HeapDocCType_spec = {
HeapDocCType_slots
};

typedef struct {
PyObject_HEAD
} HeapTypeNameObject;

static PyType_Slot HeapTypeNameType_slots[] = {
{0},
};

static PyType_Spec HeapTypeNameType_Spec = {
.name = "_testcapi.HeapTypeNameType",
.basicsize = sizeof(HeapTypeNameObject),
.flags = Py_TPFLAGS_DEFAULT,
.slots = HeapTypeNameType_slots,
};

typedef struct {
PyObject_HEAD
} NullTpDocTypeObject;
Expand Down
6 changes: 6 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3638,6 +3638,12 @@ PyType_FromSpec(PyType_Spec *spec)
return PyType_FromSpecWithBases(spec, NULL);
}

PyObject *
PyType_GetName(PyTypeObject *type)
{
return type_name(type, NULL);
}

void *
PyType_GetSlot(PyTypeObject *type, int slot)
{
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ EXPORT_FUNC(PyType_GenericNew)
EXPORT_FUNC(PyType_GetFlags)
EXPORT_FUNC(PyType_GetModule)
EXPORT_FUNC(PyType_GetModuleState)
EXPORT_FUNC(PyType_GetName)
EXPORT_FUNC(PyType_GetSlot)
EXPORT_FUNC(PyType_IsSubtype)
EXPORT_FUNC(PyType_Modified)
Expand Down