Skip to content

Commit 9635bcf

Browse files
committed
PyType_GetName return a object
1 parent 8ec352a commit 9635bcf

File tree

7 files changed

+18
-16
lines changed

7 files changed

+18
-16
lines changed

Doc/c-api/type.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ Type Objects
9595
from a type's base class. Return ``0`` on success, or return ``-1`` and sets an
9696
exception on error.
9797
98-
.. c:function:: const char* PyType_GetName(PyTypeObject *type)
98+
.. c:function:: PyObject* PyType_GetName(PyTypeObject *type)
9999
100-
Return the type's name. The returned pointer includes the module name
101-
if type's name have a prefix of module name.
102-
Callers can hold this pointer until the type has been deallocated.
100+
Return the type's name. The returned object haven't a prefix of module name.
103101
104102
.. versionadded:: 3.10
105103

Doc/data/refcounts.dat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ PyType_GenericNew:PyObject*:kwds:0:
23092309
PyType_GetFlags:unsigned long:::
23102310
PyType_GetFlags:PyTypeObject*:type:0:
23112311

2312-
PyType_GetName:const char*:::
2312+
PyType_GetName:PyObject*::+1:
23132313
PyType_GetName:PyTypeObject*:type:0:
23142314

23152315
PyType_GetSlot:void*:::

Doc/whatsnew/3.10.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ New Features
606606
* The :c:func:`PyType_GetSlot` function can accept static types.
607607
(Contributed by Hai Shi and Petr Viktorin in :issue:`41073`.)
608608

609-
* Add a new :c:func:`PyType_GetName` function to get type's name.
609+
* Add a new :c:func:`PyType_GetName` function to get type's short name.
610610
(Contributed by Hai Shi in :issue:`42035`.)
611611

612612

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
233233
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
234234
#endif
235235
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03100000
236-
PyAPI_FUNC(const char *) PyType_GetName(PyTypeObject *);
236+
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
237237
#endif
238238

239239
/* Generic type check */
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add a new :c:func:`PyType_GetName` function to get type's name.
1+
Add a new :c:func:`PyType_GetName` function to get type's short name.

Modules/_testcapimodule.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,18 +1078,21 @@ test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
10781078
static PyObject *
10791079
test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
10801080
{
1081-
const char *tp_name = PyType_GetName(&PyLong_Type);
1082-
assert(strcmp(tp_name, "int") == 0);
1081+
PyObject *tp_name = PyType_GetName(&PyLong_Type);
1082+
assert(strcmp(PyUnicode_AsUTF8(tp_name), "int") == 0);
1083+
Py_DECREF(tp_name);
10831084

10841085
tp_name = PyType_GetName(&PyModule_Type);
1085-
assert(strcmp(tp_name, "module") == 0);
1086+
assert(strcmp(PyUnicode_AsUTF8(tp_name), "module") == 0);
1087+
Py_DECREF(tp_name);
10861088

10871089
PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
10881090
if (HeapTypeNameType == NULL) {
10891091
Py_RETURN_NONE;
10901092
}
10911093
tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
1092-
assert(strcmp(tp_name, "_testcapi.HeapTypeNameType") == 0);
1094+
assert(strcmp(PyUnicode_AsUTF8(tp_name), "HeapTypeNameType") == 0);
1095+
Py_DECREF(tp_name);
10931096

10941097
Py_DECREF(HeapTypeNameType);
10951098
Py_RETURN_NONE;

Objects/typeobject.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,15 +3152,16 @@ PyType_FromSpec(PyType_Spec *spec)
31523152
return PyType_FromSpecWithBases(spec, NULL);
31533153
}
31543154

3155-
const char *
3155+
PyObject *
31563156
PyType_GetName(PyTypeObject *type)
31573157
{
31583158
assert(PyType_Check(type));
31593159
if (_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
3160-
PyTypeObject ht_type = ((PyHeapTypeObject *)type)->ht_type;
3161-
return ht_type.tp_name;
3160+
PyHeapTypeObject *ht_type = (PyHeapTypeObject *)type;
3161+
Py_INCREF(ht_type->ht_name);
3162+
return ht_type->ht_name;
31623163
}
3163-
return type->tp_name;
3164+
return PyUnicode_FromString(_PyType_Name(type));
31643165
}
31653166

31663167
void *

0 commit comments

Comments
 (0)