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

This commit is contained in:
Hai Shi 2021-08-17 21:39:34 +08:00 committed by GitHub
parent 6a358bb948
commit 3e2c643ae0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 67 additions and 0 deletions

View file

@ -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) {
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)
{
@ -5667,6 +5707,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},