Skip to content

Commit 3e2c643

Browse files
authored
bpo-42035: Add PyType_GetQualName() to get a type's qualified name. (GH-27551)
1 parent 6a358bb commit 3e2c643

File tree

10 files changed

+67
-0
lines changed

10 files changed

+67
-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
@@ -2310,6 +2310,9 @@ PyType_GetFlags:PyTypeObject*:type:0:
23102310
PyType_GetName:PyObject*::+1:
23112311
PyType_GetName:PyTypeObject*:type:0:
23122312

2313+
PyType_GetQualName:PyObject*::+1:
2314+
PyType_GetQualName:PyTypeObject*:type:0:
2315+
23132316
PyType_GetSlot:void*:::
23142317
PyType_GetSlot:PyTypeObject*:type:0:
23152318
PyType_GetSlot:int:slot::

Doc/data/stable_abi.dat

+1
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ function,PyType_GetFlags,3.2,
644644
function,PyType_GetModule,3.10,
645645
function,PyType_GetModuleState,3.10,
646646
function,PyType_GetName,3.11,
647+
function,PyType_GetQualName,3.11,
647648
function,PyType_GetSlot,3.4,
648649
function,PyType_IsSubtype,3.2,
649650
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
@@ -2147,6 +2147,8 @@ function PyGC_IsEnabled
21472147

21482148
function PyType_GetName
21492149
added 3.11
2150+
function PyType_GetQualName
2151+
added 3.11
21502152

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

Modules/_testcapimodule.c

+41
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,46 @@ 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+
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
1178+
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "HeapTypeNameType") == 0);
1179+
Py_DECREF(tp_qualname);
1180+
1181+
PyObject *spec_name = PyUnicode_FromString(HeapTypeNameType_Spec.name);
1182+
if (spec_name == NULL) {
1183+
goto done;
1184+
}
1185+
if (PyObject_SetAttrString(HeapTypeNameType,
1186+
"__qualname__", spec_name) < 0) {
1187+
Py_DECREF(spec_name);
1188+
goto done;
1189+
}
1190+
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
1191+
assert(strcmp(PyUnicode_AsUTF8(tp_qualname),
1192+
"_testcapi.HeapTypeNameType") == 0);
1193+
Py_DECREF(spec_name);
1194+
Py_DECREF(tp_qualname);
1195+
1196+
done:
1197+
Py_DECREF(HeapTypeNameType);
1198+
Py_RETURN_NONE;
1199+
}
1200+
1201+
11621202
static PyObject *
11631203
get_args(PyObject *self, PyObject *args)
11641204
{
@@ -5667,6 +5707,7 @@ static PyMethodDef TestMethods[] = {
56675707
{"get_args", get_args, METH_VARARGS},
56685708
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
56695709
{"test_get_type_name", test_get_type_name, METH_NOARGS},
5710+
{"test_get_type_qualname", test_get_type_qualname, METH_NOARGS},
56705711
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
56715712
METH_VARARGS|METH_KEYWORDS},
56725713
{"getargs_tuple", getargs_tuple, METH_VARARGS},

Objects/typeobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -3620,6 +3620,12 @@ PyType_GetName(PyTypeObject *type)
36203620
return type_name(type, NULL);
36213621
}
36223622

3623+
PyObject *
3624+
PyType_GetQualName(PyTypeObject *type)
3625+
{
3626+
return type_qualname(type, NULL);
3627+
}
3628+
36233629
void *
36243630
PyType_GetSlot(PyTypeObject *type, int slot)
36253631
{

PC/python3dll.c

+1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ EXPORT_FUNC(PyType_GetFlags)
593593
EXPORT_FUNC(PyType_GetModule)
594594
EXPORT_FUNC(PyType_GetModuleState)
595595
EXPORT_FUNC(PyType_GetName)
596+
EXPORT_FUNC(PyType_GetQualName)
596597
EXPORT_FUNC(PyType_GetSlot)
597598
EXPORT_FUNC(PyType_IsSubtype)
598599
EXPORT_FUNC(PyType_Modified)

0 commit comments

Comments
 (0)