diff -r 5ca8790f1161 Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Mon Aug 15 03:23:23 2016 -0400 +++ b/Objects/bytearrayobject.c Tue Aug 16 00:25:36 2016 +0800 @@ -880,89 +880,10 @@ return -1; } -/* Mostly copied from string_repr, but without the - "smart quote" functionality. */ static PyObject * bytearray_repr(PyByteArrayObject *self) { - const char *quote_prefix = "bytearray(b"; - const char *quote_postfix = ")"; - Py_ssize_t length = Py_SIZE(self); - /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ - size_t newsize; - PyObject *v; - Py_ssize_t i; - char *bytes; - char c; - char *p; - int quote; - char *test, *start; - char *buffer; - - if (length > (PY_SSIZE_T_MAX - 15) / 4) { - PyErr_SetString(PyExc_OverflowError, - "bytearray object is too large to make repr"); - return NULL; - } - - newsize = 15 + length * 4; - buffer = PyObject_Malloc(newsize); - if (buffer == NULL) { - PyErr_NoMemory(); - return NULL; - } - - /* Figure out which quote to use; single is preferred */ - quote = '\''; - start = PyByteArray_AS_STRING(self); - for (test = start; test < start+length; ++test) { - if (*test == '"') { - quote = '\''; /* back to single */ - break; - } - else if (*test == '\'') - quote = '"'; - } - - p = buffer; - while (*quote_prefix) - *p++ = *quote_prefix++; - *p++ = quote; - - bytes = PyByteArray_AS_STRING(self); - for (i = 0; i < length; i++) { - /* There's at least enough room for a hex escape - and a closing quote. */ - assert(newsize - (p - buffer) >= 5); - c = bytes[i]; - if (c == '\'' || c == '\\') - *p++ = '\\', *p++ = c; - else if (c == '\t') - *p++ = '\\', *p++ = 't'; - else if (c == '\n') - *p++ = '\\', *p++ = 'n'; - else if (c == '\r') - *p++ = '\\', *p++ = 'r'; - else if (c == 0) - *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; - else if (c < ' ' || c >= 0x7f) { - *p++ = '\\'; - *p++ = 'x'; - *p++ = Py_hexdigits[(c & 0xf0) >> 4]; - *p++ = Py_hexdigits[c & 0xf]; - } - else - *p++ = c; - } - assert(newsize - (p - buffer) >= 1); - *p++ = quote; - while (*quote_postfix) { - *p++ = *quote_postfix++; - } - - v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); - PyObject_Free(buffer); - return v; + return PyBytes_Repr((PyObject *)self, 1); } static PyObject * diff -r 5ca8790f1161 Objects/bytesobject.c --- a/Objects/bytesobject.c Mon Aug 15 03:23:23 2016 -0400 +++ b/Objects/bytesobject.c Tue Aug 16 00:25:36 2016 +0800 @@ -1291,16 +1291,22 @@ PyObject * PyBytes_Repr(PyObject *obj, int smartquotes) { - PyBytesObject* op = (PyBytesObject*) obj; - Py_ssize_t i, length = Py_SIZE(op); + Py_ssize_t i, length; Py_ssize_t newsize, squotes, dquotes; PyObject *v; + PyTypeObject *type = Py_TYPE(obj); + Py_buffer view; unsigned char quote, *s, *p; /* Compute size of output string */ + if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) == -1) + return NULL; + s = (unsigned char *)view.buf; + length = view.len; squotes = dquotes = 0; newsize = 3; /* b'' */ - s = (unsigned char*)op->ob_sval; + if (!PyBytes_Check(obj)) + newsize += strlen(type->tp_name) + 2; /* bytearray(b'') */ for (i = 0; i < length; i++) { Py_ssize_t incr = 1; switch(s[i]) { @@ -1331,9 +1337,15 @@ } p = PyUnicode_1BYTE_DATA(v); + if (!PyBytes_Check(obj)) { + const char *q = type->tp_name; + *(p + newsize - 1) = ')'; + while (*q) *p++ = *q++; + *p++ = '('; + } *p++ = 'b', *p++ = quote; for (i = 0; i < length; i++) { - unsigned char c = op->ob_sval[i]; + unsigned char c = s[i]; if (c == quote || c == '\\') *p++ = '\\', *p++ = c; else if (c == '\t') @@ -1352,12 +1364,15 @@ *p++ = c; } *p++ = quote; + PyBuffer_Release(&view); assert(_PyUnicode_CheckConsistency(v, 1)); return v; overflow: - PyErr_SetString(PyExc_OverflowError, - "bytes object is too large to make repr"); + PyBuffer_Release(&view); + PyErr_Format(PyExc_OverflowError, + "%s object is too large to make repr", + type->tp_name); return NULL; }