diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-21 11:24:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-21 11:24:13 (GMT) |
commit | b3a77964ea89a488fc0e920e3db6d8477279f19b (patch) | |
tree | 880d5770553aac324a7b29efdb233eca0cfbd38a /Objects/bytearrayobject.c | |
parent | 9adda0cdf89432386b7a04444a6199b580d287a1 (diff) | |
download | cpython-b3a77964ea89a488fc0e920e3db6d8477279f19b.zip cpython-b3a77964ea89a488fc0e920e3db6d8477279f19b.tar.gz cpython-b3a77964ea89a488fc0e920e3db6d8477279f19b.tar.bz2 |
bpo-27541: Reprs of subclasses of some classes now contain actual type name. (#3631)
Affected classes are bytearray, array, deque, defaultdict, count and repeat.
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index d09b1f22..840d5b0 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -891,11 +891,12 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) static PyObject * bytearray_repr(PyByteArrayObject *self) { - const char *quote_prefix = "bytearray(b"; + const char *className = _PyType_Name(Py_TYPE(self)); + const char *quote_prefix = "(b"; const char *quote_postfix = ")"; Py_ssize_t length = Py_SIZE(self); - /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ - size_t newsize; + /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ + Py_ssize_t newsize; PyObject *v; Py_ssize_t i; char *bytes; @@ -905,13 +906,14 @@ bytearray_repr(PyByteArrayObject *self) char *test, *start; char *buffer; - if (length > (PY_SSIZE_T_MAX - 15) / 4) { + newsize = strlen(className); + if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) { PyErr_SetString(PyExc_OverflowError, "bytearray object is too large to make repr"); return NULL; } - newsize = 15 + length * 4; + newsize += 6 + length * 4; buffer = PyObject_Malloc(newsize); if (buffer == NULL) { PyErr_NoMemory(); @@ -931,6 +933,8 @@ bytearray_repr(PyByteArrayObject *self) } p = buffer; + while (*className) + *p++ = *className++; while (*quote_prefix) *p++ = *quote_prefix++; *p++ = quote; @@ -966,7 +970,7 @@ bytearray_repr(PyByteArrayObject *self) *p++ = *quote_postfix++; } - v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); + v = PyUnicode_FromStringAndSize(buffer, p - buffer); PyObject_Free(buffer); return v; } |