mirror of https://github.com/python/cpython
gh-125196: Use PyUnicodeWriter for repr(list) (#125202)
Replace the private _PyUnicodeWriter with the public PyUnicodeWriter. Replace PyObject_Repr() + _PyUnicodeWriter_WriteStr() with PyUnicodeWriter_WriteRepr().
This commit is contained in:
parent
7d2c39752f
commit
52f70da19c
|
@ -522,49 +522,50 @@ list_dealloc(PyObject *self)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
list_repr_impl(PyListObject *v)
|
list_repr_impl(PyListObject *v)
|
||||||
{
|
{
|
||||||
PyObject *s;
|
int res = Py_ReprEnter((PyObject*)v);
|
||||||
_PyUnicodeWriter writer;
|
if (res != 0) {
|
||||||
Py_ssize_t i = Py_ReprEnter((PyObject*)v);
|
return (res > 0 ? PyUnicode_FromString("[...]") : NULL);
|
||||||
if (i != 0) {
|
|
||||||
return i > 0 ? PyUnicode_FromString("[...]") : NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_PyUnicodeWriter_Init(&writer);
|
|
||||||
writer.overallocate = 1;
|
|
||||||
/* "[" + "1" + ", 2" * (len - 1) + "]" */
|
/* "[" + "1" + ", 2" * (len - 1) + "]" */
|
||||||
writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
|
Py_ssize_t prealloc = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
|
||||||
|
PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc);
|
||||||
if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
|
if (writer == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PyUnicodeWriter_WriteChar(writer, '[') < 0) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
/* Do repr() on each element. Note that this may mutate the list,
|
/* Do repr() on each element. Note that this may mutate the list,
|
||||||
so must refetch the list size on each iteration. */
|
so must refetch the list size on each iteration. */
|
||||||
for (i = 0; i < Py_SIZE(v); ++i) {
|
for (Py_ssize_t i = 0; i < Py_SIZE(v); ++i) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
|
if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
|
||||||
s = PyObject_Repr(v->ob_item[i]);
|
|
||||||
if (s == NULL)
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
|
|
||||||
Py_DECREF(s);
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
Py_DECREF(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.overallocate = 0;
|
if (PyUnicodeWriter_WriteRepr(writer, v->ob_item[i]) < 0) {
|
||||||
if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
|
|
||||||
goto error;
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PyUnicodeWriter_WriteChar(writer, ']') < 0) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
Py_ReprLeave((PyObject *)v);
|
Py_ReprLeave((PyObject *)v);
|
||||||
return _PyUnicodeWriter_Finish(&writer);
|
return PyUnicodeWriter_Finish(writer);
|
||||||
|
|
||||||
error:
|
error:
|
||||||
_PyUnicodeWriter_Dealloc(&writer);
|
if (writer != NULL) {
|
||||||
|
PyUnicodeWriter_Discard(writer);
|
||||||
|
}
|
||||||
Py_ReprLeave((PyObject *)v);
|
Py_ReprLeave((PyObject *)v);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue