Skip to content

Commit 72ccb09

Browse files
committed
Add PyType_GetName()
1 parent 66ad07d commit 72ccb09

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎docs/api.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ Python 3.11
667667

668668
Not available on PyPy nor on limited C API 3.10 and older.
669669

670+
.. c:function:: PyObject* PyType_GetName(PyTypeObject *type)
671+
672+
See `PyType_GetName() documentation <https://docs.python.org/dev/c-api/type.html#c.PyType_GetName>`__.
673+
670674
.. c:function:: PyObject* PyType_GetQualName(PyTypeObject *type)
671675

672676
See `PyType_GetQualName() documentation <https://docs.python.org/dev/c-api/type.html#c.PyType_GetQualName>`__.
@@ -675,7 +679,6 @@ Python 3.11
675679
Not supported:
676680

677681
* ``PyType_GetModuleByDef()``.
678-
* ``PyType_GetName()``.
679682
* ``Py_Version`` constant.
680683
* ``PyErr_GetHandledException()``, ``PyErr_SetHandledException()``.
681684
* ``PyFrame_GetGenerator()``.

‎docs/changelog.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
* ``PyImport_ImportModuleAttrString()``
88
* ``PyType_GetFullyQualifiedName()``
99
* ``PyType_GetModuleName()``
10+
* ``PyType_GetName()``
1011
* ``PyType_GetQualName()``
1112

1213
* 2026-10-04: Add support for the limited C API (``Py_LIMITED_API``).

‎pythoncapi_compat.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2901,9 +2901,15 @@ PyImport_ImportModuleAttrString(const char *mod_name, const char *attr_name)
29012901
#endif
29022902

29032903

2904-
// Python 3.11 added PyType_GetQualName()
2904+
// Python 3.11 added PyType_GetName() and PyType_GetQualName()
29052905
#if (PY_VERSION_HEX < 0x030B0000 \
29062906
|| (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030B0000))
2907+
static inline PyObject*
2908+
PyType_GetName(PyTypeObject *type)
2909+
{
2910+
return PyObject_GetAttrString(_PyObject_CAST(type), "__name__");
2911+
}
2912+
29072913
static inline PyObject*
29082914
PyType_GetQualName(PyTypeObject *type)
29092915
{

‎tests/test_pythoncapi_compat_cext.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,11 @@ test_type(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
25812581
#endif
25822582
Py_DECREF(modname);
25832583

2584+
PyObject *name = PyType_GetName(type);
2585+
assert(name != NULL);
2586+
assert(PyUnicode_EqualToUTF8(name, "time") == 1);
2587+
Py_DECREF(name);
2588+
25842589
PyObject *qualname = PyType_GetQualName(type);
25852590
assert(qualname != NULL);
25862591
assert(PyUnicode_EqualToUTF8(qualname, "time") == 1);

0 commit comments

Comments
 (0)