Skip to content

Commit a940aaf

Browse files
committed
Add a PyType_GetQualName() to get type's qualified name.
1 parent a390ebe commit a940aaf

File tree

10 files changed

+60
-0
lines changed

10 files changed

+60
-0
lines changed

Doc/c-api/type.rst

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ Type Objects
112112
113113
.. versionadded:: 3.11
114114
115+
.. c:function:: PyObject* PyType_GetQualName(PyTypeObject *type)
116+
117+
Return the type's qualified name. Equivalent to getting the
118+
type's ``__qualname__`` attribute.
119+
120+
.. versionadded:: 3.11
121+
115122
.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)
116123
117124
Return the function pointer stored in the given slot. If the

Doc/data/refcounts.dat

+3
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,9 @@ PyType_GetFlags:PyTypeObject*:type:0:
22922292
PyType_GetName:PyObject*::+1:
22932293
PyType_GetName:PyTypeObject*:type:0:
22942294

2295+
PyType_GetQualName:PyObject*::+1:
2296+
PyType_GetQualName:PyTypeObject*:type:0:
2297+
22952298
PyType_GetSlot:void*:::
22962299
PyType_GetSlot:PyTypeObject*:type:0:
22972300
PyType_GetSlot:int:slot::

Doc/data/stable_abi.dat

+1
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ function,PyType_GetFlags,3.2,
640640
function,PyType_GetModule,3.10,
641641
function,PyType_GetModuleState,3.10,
642642
function,PyType_GetName,3.11,
643+
function,PyType_GetQualName,3.11,
643644
function,PyType_GetSlot,3.4,
644645
function,PyType_IsSubtype,3.2,
645646
function,PyType_Modified,3.2,

Doc/whatsnew/3.11.rst

+3
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ C API Changes
298298
* Add a new :c:func:`PyType_GetName` function to get type's short name.
299299
(Contributed by Hai Shi in :issue:`42035`.)
300300

301+
* Add a new :c:func:`PyType_GetQualName` function to get type's qualified name.
302+
(Contributed by Hai Shi in :issue:`42035`.)
303+
301304
New Features
302305
------------
303306

Include/object.h

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
241241
#endif
242242
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
243243
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
244+
PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
244245
#endif
245246

246247
/* Generic type check */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a new :c:func:`PyType_GetQualName` function to get type's qualified
2+
name.

Misc/stable_abi.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,8 @@ function PyGC_IsEnabled
21362136

21372137
function PyType_GetName
21382138
added 3.11
2139+
function PyType_GetQualName
2140+
added 3.11
21392141

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

Modules/_testcapimodule.c

+34
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,39 @@ test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
11591159
}
11601160

11611161

1162+
static PyObject *
1163+
test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored))
1164+
{
1165+
PyObject *tp_qualname = PyType_GetQualName(&PyLong_Type);
1166+
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "int") == 0);
1167+
Py_DECREF(tp_qualname);
1168+
1169+
tp_qualname = PyType_GetQualName(&_PyNamespace_Type);
1170+
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "SimpleNamespace") == 0);
1171+
Py_DECREF(tp_qualname);
1172+
1173+
PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
1174+
if (HeapTypeNameType == NULL) {
1175+
Py_RETURN_NONE;
1176+
}
1177+
PyObject *spec_name = PyUnicode_FromString(HeapTypeNameType_Spec.name);
1178+
if (PyObject_SetAttrString(HeapTypeNameType,
1179+
"__qualname__", spec_name) < 0) {
1180+
Py_DECREF(spec_name);
1181+
Py_DECREF(HeapTypeNameType);
1182+
Py_RETURN_NONE;
1183+
}
1184+
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
1185+
assert(strcmp(PyUnicode_AsUTF8(tp_qualname),
1186+
"_testcapi.HeapTypeNameType") == 0);
1187+
Py_DECREF(spec_name);
1188+
Py_DECREF(tp_qualname);
1189+
1190+
Py_DECREF(HeapTypeNameType);
1191+
Py_RETURN_NONE;
1192+
}
1193+
1194+
11621195
static PyObject *
11631196
get_args(PyObject *self, PyObject *args)
11641197
{
@@ -5650,6 +5683,7 @@ static PyMethodDef TestMethods[] = {
56505683
{"get_args", get_args, METH_VARARGS},
56515684
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
56525685
{"test_get_type_name", test_get_type_name, METH_NOARGS},
5686+
{"test_get_type_qualname", test_get_type_qualname, METH_NOARGS},
56535687
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
56545688
METH_VARARGS|METH_KEYWORDS},
56555689
{"getargs_tuple", getargs_tuple, METH_VARARGS},

Objects/typeobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -3634,6 +3634,12 @@ PyType_GetName(PyTypeObject *type)
36343634
return type_name(type, NULL);
36353635
}
36363636

3637+
PyObject *
3638+
PyType_GetQualName(PyTypeObject *type)
3639+
{
3640+
return type_qualname(type, NULL);
3641+
}
3642+
36373643
void *
36383644
PyType_GetSlot(PyTypeObject *type, int slot)
36393645
{

PC/python3dll.c

+1
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ EXPORT_FUNC(PyType_GetFlags)
589589
EXPORT_FUNC(PyType_GetModule)
590590
EXPORT_FUNC(PyType_GetModuleState)
591591
EXPORT_FUNC(PyType_GetName)
592+
EXPORT_FUNC(PyType_GetQualName)
592593
EXPORT_FUNC(PyType_GetSlot)
593594
EXPORT_FUNC(PyType_IsSubtype)
594595
EXPORT_FUNC(PyType_Modified)

0 commit comments

Comments
 (0)