mirror of
https://github.com/python/cpython.git
synced 2025-11-24 12:20:42 +00:00
gh-71679: Share the repr implementation between bytes and bytearray (GH-138181)
This allows to use the smart quotes algorithm in the bytearray's repr.
This commit is contained in:
parent
cf9ef73121
commit
a1cf6e92b6
5 changed files with 30 additions and 93 deletions
|
|
@ -1348,27 +1348,33 @@ _PyBytes_ReverseFind(const char *haystack, Py_ssize_t len_haystack,
|
|||
PyObject *
|
||||
PyBytes_Repr(PyObject *obj, int smartquotes)
|
||||
{
|
||||
PyBytesObject* op = (PyBytesObject*) obj;
|
||||
Py_ssize_t i, length = Py_SIZE(op);
|
||||
return _Py_bytes_repr(PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj),
|
||||
smartquotes, "bytes");
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_Py_bytes_repr(const char *data, Py_ssize_t length, int smartquotes,
|
||||
const char *classname)
|
||||
{
|
||||
Py_ssize_t i;
|
||||
Py_ssize_t newsize, squotes, dquotes;
|
||||
PyObject *v;
|
||||
unsigned char quote;
|
||||
const unsigned char *s;
|
||||
Py_UCS1 *p;
|
||||
|
||||
/* Compute size of output string */
|
||||
squotes = dquotes = 0;
|
||||
newsize = 3; /* b'' */
|
||||
s = (const unsigned char*)op->ob_sval;
|
||||
for (i = 0; i < length; i++) {
|
||||
unsigned char c = data[i];
|
||||
Py_ssize_t incr = 1;
|
||||
switch(s[i]) {
|
||||
switch(c) {
|
||||
case '\'': squotes++; break;
|
||||
case '"': dquotes++; break;
|
||||
case '\\': case '\t': case '\n': case '\r':
|
||||
incr = 2; break; /* \C */
|
||||
default:
|
||||
if (s[i] < ' ' || s[i] >= 0x7f)
|
||||
if (c < ' ' || c >= 0x7f)
|
||||
incr = 4; /* \xHH */
|
||||
}
|
||||
if (newsize > PY_SSIZE_T_MAX - incr)
|
||||
|
|
@ -1392,7 +1398,7 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
|
|||
|
||||
*p++ = 'b', *p++ = quote;
|
||||
for (i = 0; i < length; i++) {
|
||||
unsigned char c = op->ob_sval[i];
|
||||
unsigned char c = data[i];
|
||||
if (c == quote || c == '\\')
|
||||
*p++ = '\\', *p++ = c;
|
||||
else if (c == '\t')
|
||||
|
|
@ -1415,8 +1421,8 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
|
|||
return v;
|
||||
|
||||
overflow:
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"bytes object is too large to make repr");
|
||||
PyErr_Format(PyExc_OverflowError,
|
||||
"%s object is too large to make repr", classname);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue