Skip to content

Commit f98c574

Browse files
committed
Add a PyType_GetName() to get type's name
1 parent 711381d commit f98c574

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

Doc/c-api/type.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ Type Objects
9595
from a type's base class. Return ``0`` on success, or return ``-1`` and sets an
9696
exception on error.
9797
98+
.. c:function:: const char* PyType_GetName(PyTypeObject *type)
99+
100+
Return the type's name. This function can accept heap or static types.
101+
102+
.. versionadded:: 3.10
103+
98104
.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)
99105
100106
Return the function pointer stored in the given slot. If the

Doc/data/refcounts.dat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,6 +2309,9 @@ PyType_GenericNew:PyObject*:kwds:0:
23092309
PyType_GetFlags:unsigned long:::
23102310
PyType_GetFlags:PyTypeObject*:type:0:
23112311

2312+
PyType_GetName:const char*:::
2313+
PyType_GetName:PyTypeObject*:type:0:
2314+
23122315
PyType_GetSlot:void*:::
23132316
PyType_GetSlot:PyTypeObject*:type:0:
23142317
PyType_GetSlot:int:slot::

Doc/whatsnew/3.10.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,9 @@ New Features
606606
* The :c:func:`PyType_GetSlot` function can accept static types.
607607
(Contributed by Hai Shi and Petr Viktorin in :issue:`41073`.)
608608

609+
* Add a new :c:func:`PyType_GetName` function to get type's name.
610+
(Contributed by Hai Shi in :issue:`42035`.)
611+
609612

610613
Porting to Python 3.10
611614
----------------------

Include/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObje
232232
PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
233233
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
234234
#endif
235+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000
236+
PyAPI_FUNC(const char *) PyType_GetName(PyTypeObject *);
237+
#endif
235238

236239
/* Generic type check */
237240
PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a new :c:func:`PyType_GetName` function to get type's name.

Modules/_testcapimodule.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#endif
3939

4040
static struct PyModuleDef _testcapimodule;
41+
static PyType_Spec HeapTypeNameType_Spec;
4142

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

@@ -1074,6 +1075,27 @@ test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
10741075
}
10751076

10761077

1078+
static PyObject *
1079+
test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
1080+
{
1081+
const char *tp_name = PyType_GetName(&PyLong_Type);
1082+
assert(strcmp(tp_name, "int") == 0);
1083+
1084+
tp_name = PyType_GetName(&PyModule_Type);
1085+
assert(strcmp(tp_name, "module") == 0);
1086+
1087+
PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
1088+
if (HeapTypeNameType == NULL) {
1089+
Py_RETURN_NONE;
1090+
}
1091+
tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
1092+
assert(strcmp(tp_name, "HeapTypeNameType") == 0);
1093+
1094+
Py_DECREF(HeapTypeNameType);
1095+
Py_RETURN_NONE;
1096+
}
1097+
1098+
10771099
static PyObject *
10781100
get_args(PyObject *self, PyObject *args)
10791101
{
@@ -5738,6 +5760,7 @@ static PyMethodDef TestMethods[] = {
57385760
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
57395761
{"get_args", get_args, METH_VARARGS},
57405762
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
5763+
{"test_get_type_name", test_get_type_name, METH_NOARGS},
57415764
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
57425765
METH_VARARGS|METH_KEYWORDS},
57435766
{"getargs_tuple", getargs_tuple, METH_VARARGS},
@@ -6622,6 +6645,21 @@ static PyType_Spec HeapDocCType_spec = {
66226645
HeapDocCType_slots
66236646
};
66246647

6648+
typedef struct {
6649+
PyObject_HEAD
6650+
} HeapTypeNameObject;
6651+
6652+
static PyType_Slot HeapTypeNameType_slots[] = {
6653+
{0},
6654+
};
6655+
6656+
static PyType_Spec HeapTypeNameType_Spec = {
6657+
.name = "_testcapi.HeapTypeNameType",
6658+
.basicsize = sizeof(HeapTypeNameObject),
6659+
.flags = Py_TPFLAGS_DEFAULT,
6660+
.slots = HeapTypeNameType_slots,
6661+
};
6662+
66256663
typedef struct {
66266664
PyObject_HEAD
66276665
} NullTpDocTypeObject;

Objects/typeobject.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,17 @@ PyType_FromSpec(PyType_Spec *spec)
31523152
return PyType_FromSpecWithBases(spec, NULL);
31533153
}
31543154

3155+
const char *
3156+
PyType_GetName(PyTypeObject *type)
3157+
{
3158+
assert(PyType_Check(type));
3159+
if (_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
3160+
return PyUnicode_AsUTF8(((PyHeapTypeObject *)type)->ht_name);
3161+
} else {
3162+
return type->tp_name;
3163+
}
3164+
}
3165+
31553166
void *
31563167
PyType_GetSlot(PyTypeObject *type, int slot)
31573168
{

0 commit comments

Comments
 (0)