Commit c70ab02d authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36365: Rewrite structseq_repr() using _PyUnicodeWriter (GH-12440)

No longer limit repr(structseq) to 512 bytes. Use _PyUnicodeWriter
for better performance and to write directly Unicode rather than
encoding repr() value to UTF-8 and then decoding from UTF-8.
parent fd23cfa4
...@@ -168,78 +168,88 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) ...@@ -168,78 +168,88 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
static PyObject * static PyObject *
structseq_repr(PyStructSequence *obj) structseq_repr(PyStructSequence *obj)
{ {
/* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100
PyTypeObject *typ = Py_TYPE(obj); PyTypeObject *typ = Py_TYPE(obj);
Py_ssize_t i; _PyUnicodeWriter writer;
int removelast = 0;
Py_ssize_t len; /* Write "typename(" */
char buf[REPR_BUFFER_SIZE]; PyObject *type_name = PyUnicode_DecodeUTF8(typ->tp_name,
char *endofbuf, *pbuf = buf; strlen(typ->tp_name),
NULL);
/* pointer to end of writeable buffer; safes space for "...)\0" */ if (type_name == NULL) {
endofbuf= &buf[REPR_BUFFER_SIZE-5]; goto error;
}
/* "typename(", limited to TYPE_MAXSIZE */
len = strlen(typ->tp_name); _PyUnicodeWriter_Init(&writer);
len = Py_MIN(len, TYPE_MAXSIZE); writer.overallocate = 1;
memcpy(pbuf, typ->tp_name, len); /* count 5 characters per item: "x=1, " */
pbuf += len; writer.min_length = (PyUnicode_GET_LENGTH(type_name) + 1
*pbuf++ = '('; + VISIBLE_SIZE(obj) * 5 + 1);
for (i=0; i < VISIBLE_SIZE(obj); i++) { if (_PyUnicodeWriter_WriteStr(&writer, type_name) < 0) {
PyObject *val, *repr; Py_DECREF(type_name);
const char *cname, *crepr; goto error;
}
cname = typ->tp_members[i].name; Py_DECREF(type_name);
if (cname == NULL) {
if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) {
goto error;
}
for (Py_ssize_t i=0; i < VISIBLE_SIZE(obj); i++) {
if (i > 0) {
/* Write ", " */
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
goto error;
}
}
/* Write "name=repr" */
const char *name_utf8 = typ->tp_members[i].name;
if (name_utf8 == NULL) {
PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %zd name is NULL" PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %zd name is NULL"
" for type %.500s", i, typ->tp_name); " for type %.500s", i, typ->tp_name);
return NULL; goto error;
} }
val = PyStructSequence_GET_ITEM(obj, i);
repr = PyObject_Repr(val); PyObject *name = PyUnicode_DecodeUTF8(name_utf8, strlen(name_utf8), NULL);
if (repr == NULL) if (name == NULL) {
return NULL; goto error;
crepr = PyUnicode_AsUTF8(repr); }
if (crepr == NULL) { if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
Py_DECREF(repr); Py_DECREF(name);
return NULL; goto error;
} }
Py_DECREF(name);
/* + 3: keep space for "=" and ", " */ if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) {
len = strlen(cname) + strlen(crepr) + 3; goto error;
if ((pbuf+len) <= endofbuf) {
strcpy(pbuf, cname);
pbuf += strlen(cname);
*pbuf++ = '=';
strcpy(pbuf, crepr);
pbuf += strlen(crepr);
*pbuf++ = ',';
*pbuf++ = ' ';
removelast = 1;
Py_DECREF(repr);
} }
else {
strcpy(pbuf, "..."); PyObject *value = PyStructSequence_GET_ITEM(obj, i);
pbuf += 3; assert(value != NULL);
removelast = 0; PyObject *repr = PyObject_Repr(value);
if (repr == NULL) {
goto error;
}
if (_PyUnicodeWriter_WriteStr(&writer, repr) < 0) {
Py_DECREF(repr); Py_DECREF(repr);
break; goto error;
} }
Py_DECREF(repr);
} }
if (removelast) {
/* overwrite last ", " */ if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) {
pbuf-=2; goto error;
} }
*pbuf++ = ')';
*pbuf = '\0';
return PyUnicode_FromString(buf); return _PyUnicodeWriter_Finish(&writer);
error:
_PyUnicodeWriter_Dealloc(&writer);
return NULL;
} }
static PyObject * static PyObject *
structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored)) structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored))
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment