Skip to content

bpo-42035: Add a PyType_GetQualName() to get type's qualified name. #27551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ Type Objects

.. versionadded:: 3.11

.. c:function:: PyObject* PyType_GetQualName(PyTypeObject *type)

Return the type's qualified name. Equivalent to getting the
type's ``__qualname__`` attribute.

.. versionadded:: 3.11

.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)

Return the function pointer stored in the given slot. If the
Expand Down
3 changes: 3 additions & 0 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,9 @@ PyType_GetFlags:PyTypeObject*:type:0:
PyType_GetName:PyObject*::+1:
PyType_GetName:PyTypeObject*:type:0:

PyType_GetQualName:PyObject*::+1:
PyType_GetQualName:PyTypeObject*:type:0:

PyType_GetSlot:void*:::
PyType_GetSlot:PyTypeObject*:type:0:
PyType_GetSlot:int:slot::
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ function,PyType_GetFlags,3.2,
function,PyType_GetModule,3.10,
function,PyType_GetModuleState,3.10,
function,PyType_GetName,3.11,
function,PyType_GetQualName,3.11,
function,PyType_GetSlot,3.4,
function,PyType_IsSubtype,3.2,
function,PyType_Modified,3.2,
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ C API Changes
* Add a new :c:func:`PyType_GetName` function to get type's short name.
(Contributed by Hai Shi in :issue:`42035`.)

* Add a new :c:func:`PyType_GetQualName` function to get type's qualified name.
(Contributed by Hai Shi in :issue:`42035`.)

New Features
------------

Expand Down
1 change: 1 addition & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
#endif

/* Generic type check */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a new :c:func:`PyType_GetQualName` function to get type's qualified
name.
2 changes: 2 additions & 0 deletions Misc/stable_abi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,8 @@ function PyGC_IsEnabled

function PyType_GetName
added 3.11
function PyType_GetQualName
added 3.11

# (Detailed comments aren't really needed for further entries: from here on
# we can use version control logs.)
41 changes: 41 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,46 @@ test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
}


static PyObject *
test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *tp_qualname = PyType_GetQualName(&PyLong_Type);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "int") == 0);
Py_DECREF(tp_qualname);

tp_qualname = PyType_GetQualName(&_PyNamespace_Type);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "SimpleNamespace") == 0);
Py_DECREF(tp_qualname);

PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
if (HeapTypeNameType == NULL) {
Py_RETURN_NONE;
}
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "HeapTypeNameType") == 0);
Py_DECREF(tp_qualname);

PyObject *spec_name = PyUnicode_FromString(HeapTypeNameType_Spec.name);
if (spec_name == NULL) {
goto done;
}
if (PyObject_SetAttrString(HeapTypeNameType,
"__qualname__", spec_name) < 0) {
Comment on lines +1185 to +1186
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting test.
Would it make sense to test the original qualname first, then assign a different one and test that PyType_GetQualName returns the replacement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha. I try to get some qual names in _testcapimodule. But I can get the qual name with prefix.
The reason is res->ht_qualname = res->ht_name; in https://github.com/python/cpython/blob/main/Objects/typeobject.c#L3439.
I don't know the reason why the qual name need to reomve the prefix too.
So I set a qual name with prefix to test this C API, somthing like a mock behavior~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, C-API classes don't have distinct qualnames.
IMO, the best thing to do here would be:

  • Test the original qualname (same as the short name)
  • Change the qualname
  • Then test the new qualname.

Ideally, the same thing would be tested for PyType_GetName as well – it's not that different from this function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, C-API classes don't have distinct qualnames.
IMO, the best thing to do here would be:

  • Test the original qualname (same as the short name)
  • Change the qualname
  • Then test the new qualname.

Make sense. How about this updated PR :)

Ideally, the same thing would be tested for PyType_GetName as well – it's not that different from this function.

Got it. I will update enhance this test case later.

Py_DECREF(spec_name);
goto done;
}
tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
assert(strcmp(PyUnicode_AsUTF8(tp_qualname),
"_testcapi.HeapTypeNameType") == 0);
Py_DECREF(spec_name);
Py_DECREF(tp_qualname);

done:
Py_DECREF(HeapTypeNameType);
Py_RETURN_NONE;
}


static PyObject *
get_args(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -5650,6 +5690,7 @@ static PyMethodDef TestMethods[] = {
{"get_args", get_args, METH_VARARGS},
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
{"test_get_type_name", test_get_type_name, METH_NOARGS},
{"test_get_type_qualname", test_get_type_qualname, METH_NOARGS},
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
METH_VARARGS|METH_KEYWORDS},
{"getargs_tuple", getargs_tuple, METH_VARARGS},
Expand Down
6 changes: 6 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3634,6 +3634,12 @@ PyType_GetName(PyTypeObject *type)
return type_name(type, NULL);
}

PyObject *
PyType_GetQualName(PyTypeObject *type)
{
return type_qualname(type, NULL);
}

void *
PyType_GetSlot(PyTypeObject *type, int slot)
{
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ EXPORT_FUNC(PyType_GetFlags)
EXPORT_FUNC(PyType_GetModule)
EXPORT_FUNC(PyType_GetModuleState)
EXPORT_FUNC(PyType_GetName)
EXPORT_FUNC(PyType_GetQualName)
EXPORT_FUNC(PyType_GetSlot)
EXPORT_FUNC(PyType_IsSubtype)
EXPORT_FUNC(PyType_Modified)
Expand Down