summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorHai Shi <shihai1992@gmail.com>2021-08-17 13:39:34 (GMT)
committerGitHub <noreply@github.com>2021-08-17 13:39:34 (GMT)
commit3e2c643ae0b21f9e596bfd9c8ec99ca546ea8d0f (patch)
treebe4a37d2598016830e499dd7fb45b75fea71b1e9 /Modules
parent6a358bb9482f7595b858ea7b800cbe66f0de5fa1 (diff)
downloadcpython-3e2c643ae0b21f9e596bfd9c8ec99ca546ea8d0f.zip
cpython-3e2c643ae0b21f9e596bfd9c8ec99ca546ea8d0f.tar.gz
cpython-3e2c643ae0b21f9e596bfd9c8ec99ca546ea8d0f.tar.bz2
bpo-42035: Add PyType_GetQualName() to get a type's qualified name. (GH-27551)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index c35eac7..0a3c6e0 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1160,6 +1160,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)
{
if (args == NULL) {
@@ -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},