Skip to content

Commit a390ebe

Browse files
authored
bpo-42035: Add a PyType_GetName() to get type's short name. (GH-23903)
1 parent 5269c09 commit a390ebe

File tree

10 files changed

+68
-0
lines changed

10 files changed

+68
-0
lines changed

Doc/c-api/type.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ Type Objects
106106
GC protocol itself by at least implementing the
107107
:c:member:`~PyTypeObject.tp_traverse` handle.
108108
109+
.. c:function:: PyObject* PyType_GetName(PyTypeObject *type)
110+
111+
Return the type's name. Equivalent to getting the type's ``__name__`` attribute.
112+
113+
.. versionadded:: 3.11
114+
109115
.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)
110116
111117
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
@@ -2289,6 +2289,9 @@ PyType_GenericNew:PyObject*:kwds:0:
22892289
PyType_GetFlags:unsigned long:::
22902290
PyType_GetFlags:PyTypeObject*:type:0:
22912291

2292+
PyType_GetName:PyObject*::+1:
2293+
PyType_GetName:PyTypeObject*:type:0:
2294+
22922295
PyType_GetSlot:void*:::
22932296
PyType_GetSlot:PyTypeObject*:type:0:
22942297
PyType_GetSlot:int:slot::

Doc/data/stable_abi.dat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ function,PyType_GenericNew,3.2,
639639
function,PyType_GetFlags,3.2,
640640
function,PyType_GetModule,3.10,
641641
function,PyType_GetModuleState,3.10,
642+
function,PyType_GetName,3.11,
642643
function,PyType_GetSlot,3.4,
643644
function,PyType_IsSubtype,3.2,
644645
function,PyType_Modified,3.2,

Doc/whatsnew/3.11.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ Changes in the Python API
295295

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

299301
New Features
300302
------------

Include/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObje
239239
PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
240240
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
241241
#endif
242+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
243+
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
244+
#endif
242245

243246
/* Generic type check */
244247
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 short name.

Misc/stable_abi.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,6 +2132,10 @@ function PyGC_Enable
21322132
function PyGC_IsEnabled
21332133
added 3.10
21342134

2135+
# Add new C API in Python 3.11
2136+
2137+
function PyType_GetName
2138+
added 3.11
21352139

21362140
# (Detailed comments aren't really needed for further entries: from here on
21372141
# we can use version control logs.)

Modules/_testcapimodule.c

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

3939
static struct PyModuleDef _testcapimodule;
40+
static PyType_Spec HeapTypeNameType_Spec;
4041

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

@@ -1134,6 +1135,30 @@ test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
11341135
}
11351136

11361137

1138+
static PyObject *
1139+
test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
1140+
{
1141+
PyObject *tp_name = PyType_GetName(&PyLong_Type);
1142+
assert(strcmp(PyUnicode_AsUTF8(tp_name), "int") == 0);
1143+
Py_DECREF(tp_name);
1144+
1145+
tp_name = PyType_GetName(&PyModule_Type);
1146+
assert(strcmp(PyUnicode_AsUTF8(tp_name), "module") == 0);
1147+
Py_DECREF(tp_name);
1148+
1149+
PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
1150+
if (HeapTypeNameType == NULL) {
1151+
Py_RETURN_NONE;
1152+
}
1153+
tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
1154+
assert(strcmp(PyUnicode_AsUTF8(tp_name), "HeapTypeNameType") == 0);
1155+
Py_DECREF(tp_name);
1156+
1157+
Py_DECREF(HeapTypeNameType);
1158+
Py_RETURN_NONE;
1159+
}
1160+
1161+
11371162
static PyObject *
11381163
get_args(PyObject *self, PyObject *args)
11391164
{
@@ -5624,6 +5649,7 @@ static PyMethodDef TestMethods[] = {
56245649
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
56255650
{"get_args", get_args, METH_VARARGS},
56265651
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
5652+
{"test_get_type_name", test_get_type_name, METH_NOARGS},
56275653
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
56285654
METH_VARARGS|METH_KEYWORDS},
56295655
{"getargs_tuple", getargs_tuple, METH_VARARGS},
@@ -6512,6 +6538,21 @@ static PyType_Spec HeapDocCType_spec = {
65126538
HeapDocCType_slots
65136539
};
65146540

6541+
typedef struct {
6542+
PyObject_HEAD
6543+
} HeapTypeNameObject;
6544+
6545+
static PyType_Slot HeapTypeNameType_slots[] = {
6546+
{0},
6547+
};
6548+
6549+
static PyType_Spec HeapTypeNameType_Spec = {
6550+
.name = "_testcapi.HeapTypeNameType",
6551+
.basicsize = sizeof(HeapTypeNameObject),
6552+
.flags = Py_TPFLAGS_DEFAULT,
6553+
.slots = HeapTypeNameType_slots,
6554+
};
6555+
65156556
typedef struct {
65166557
PyObject_HEAD
65176558
} NullTpDocTypeObject;

Objects/typeobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,6 +3628,12 @@ PyType_FromSpec(PyType_Spec *spec)
36283628
return PyType_FromSpecWithBases(spec, NULL);
36293629
}
36303630

3631+
PyObject *
3632+
PyType_GetName(PyTypeObject *type)
3633+
{
3634+
return type_name(type, NULL);
3635+
}
3636+
36313637
void *
36323638
PyType_GetSlot(PyTypeObject *type, int slot)
36333639
{

PC/python3dll.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ EXPORT_FUNC(PyType_GenericNew)
588588
EXPORT_FUNC(PyType_GetFlags)
589589
EXPORT_FUNC(PyType_GetModule)
590590
EXPORT_FUNC(PyType_GetModuleState)
591+
EXPORT_FUNC(PyType_GetName)
591592
EXPORT_FUNC(PyType_GetSlot)
592593
EXPORT_FUNC(PyType_IsSubtype)
593594
EXPORT_FUNC(PyType_Modified)

0 commit comments

Comments
 (0)