summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-09-17 21:11:04 +0300
committerGitHub <noreply@github.com>2017-09-17 21:11:04 +0300
commit4ab46d794961491ed185c195d53da7ee6a16e646 (patch)
treed8c39fb69ba33674cf8a240f6d068d0cf710d7a3 /Objects
parent132a7d7cdbc7cb89fa1c1f4e8192241c3d68f549 (diff)
downloadcpython-git-4ab46d794961491ed185c195d53da7ee6a16e646.tar.gz
bpo-31497: Add private helper _PyType_Name(). (#3630)
This function returns the last component of tp_name after a dot. Returns tp_name itself if it doesn't contain a dot.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/exceptions.c8
-rw-r--r--Objects/odictobject.c12
-rw-r--r--Objects/typeobject.c24
3 files changed, 19 insertions, 25 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 1b70be786a..42b3fc7bb1 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -116,13 +116,7 @@ BaseException_str(PyBaseExceptionObject *self)
static PyObject *
BaseException_repr(PyBaseExceptionObject *self)
{
- const char *name;
- const char *dot;
-
- name = Py_TYPE(self)->tp_name;
- dot = (const char *) strrchr(name, '.');
- if (dot != NULL) name = dot+1;
-
+ const char *name = _PyType_Name(Py_TYPE(self));
return PyUnicode_FromFormat("%s%R", name, self->args);
}
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index 8ad8f384c4..afacb36f6b 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1471,16 +1471,9 @@ odict_repr(PyODictObject *self)
int i;
_Py_IDENTIFIER(items);
PyObject *pieces = NULL, *result = NULL;
- const char *classname;
-
- classname = strrchr(Py_TYPE(self)->tp_name, '.');
- if (classname == NULL)
- classname = Py_TYPE(self)->tp_name;
- else
- classname++;
if (PyODict_SIZE(self) == 0)
- return PyUnicode_FromFormat("%s()", classname);
+ return PyUnicode_FromFormat("%s()", _PyType_Name(Py_TYPE(self)));
i = Py_ReprEnter((PyObject *)self);
if (i != 0) {
@@ -1532,7 +1525,8 @@ odict_repr(PyODictObject *self)
goto Done;
}
- result = PyUnicode_FromFormat("%s(%R)", classname, pieces);
+ result = PyUnicode_FromFormat("%s(%R)",
+ _PyType_Name(Py_TYPE(self)), pieces);
Done:
Py_XDECREF(pieces);
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 662c493ff2..190a8b2aa4 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -388,11 +388,22 @@ check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *nam
return 1;
}
+const char *
+_PyType_Name(PyTypeObject *type)
+{
+ const char *s = strrchr(type->tp_name, '.');
+ if (s == NULL) {
+ s = type->tp_name;
+ }
+ else {
+ s++;
+ }
+ return s;
+}
+
static PyObject *
type_name(PyTypeObject *type, void *context)
{
- const char *s;
-
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
PyHeapTypeObject* et = (PyHeapTypeObject*)type;
@@ -400,12 +411,7 @@ type_name(PyTypeObject *type, void *context)
return et->ht_name;
}
else {
- s = strrchr(type->tp_name, '.');
- if (s == NULL)
- s = type->tp_name;
- else
- s++;
- return PyUnicode_FromString(s);
+ return PyUnicode_FromString(_PyType_Name(type));
}
}
@@ -418,7 +424,7 @@ type_qualname(PyTypeObject *type, void *context)
return et->ht_qualname;
}
else {
- return type_name(type, context);
+ return PyUnicode_FromString(_PyType_Name(type));
}
}