diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-17 18:11:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-17 18:11:04 (GMT) |
commit | 4ab46d794961491ed185c195d53da7ee6a16e646 (patch) | |
tree | d8c39fb69ba33674cf8a240f6d068d0cf710d7a3 /Objects | |
parent | 132a7d7cdbc7cb89fa1c1f4e8192241c3d68f549 (diff) | |
download | cpython-4ab46d794961491ed185c195d53da7ee6a16e646.zip cpython-4ab46d794961491ed185c195d53da7ee6a16e646.tar.gz cpython-4ab46d794961491ed185c195d53da7ee6a16e646.tar.bz2 |
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.c | 8 | ||||
-rw-r--r-- | Objects/odictobject.c | 12 | ||||
-rw-r--r-- | Objects/typeobject.c | 24 |
3 files changed, 19 insertions, 25 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 1b70be7..42b3fc7 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 8ad8f38..afacb36 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 662c493..190a8b2 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)); } } |