Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,49 +522,48 @@ list_dealloc(PyObject *self)
static PyObject *
list_repr_impl(PyListObject *v)
{
PyObject *s;
_PyUnicodeWriter writer;
Py_ssize_t i = Py_ReprEnter((PyObject*)v);
if (i != 0) {
return i > 0 ? PyUnicode_FromString("[...]") : NULL;
int res = Py_ReprEnter((PyObject*)v);
if (res != 0) {
return (res > 0 ? PyUnicode_FromString("[...]") : NULL);
}

_PyUnicodeWriter_Init(&writer);
writer.overallocate = 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 (writer == NULL) {
goto error;
}

if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
if (PyUnicodeWriter_WriteChar(writer, '[') < 0) {
goto error;
}

/* Do repr() on each element. Note that this may mutate the list,
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 (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
goto error;
}
if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
goto error;
}
}

s = PyObject_Repr(v->ob_item[i]);
if (s == NULL)
goto error;

if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
Py_DECREF(s);
if (PyUnicodeWriter_WriteRepr(writer, v->ob_item[i]) < 0) {
goto error;
}
Py_DECREF(s);
}

writer.overallocate = 0;
if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
if (PyUnicodeWriter_WriteChar(writer, ']') < 0) {
goto error;
}

Py_ReprLeave((PyObject *)v);
return _PyUnicodeWriter_Finish(&writer);
return PyUnicodeWriter_Finish(writer);

error:
_PyUnicodeWriter_Dealloc(&writer);
PyUnicodeWriter_Discard(writer);
Py_ReprLeave((PyObject *)v);
return NULL;
}
Expand Down
3 changes: 3 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13428,6 +13428,9 @@ PyUnicodeWriter_Create(Py_ssize_t length)

void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
{
if (writer == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be documented. Please do this in a separate PR.

Also, I think it is worth to try to optimize PyUnicodeWriter_WriteUTF8() for short ASCII strings.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be documented. Please do this in a separate PR.

Oh ok. I reverted the change.

return;
}
_PyUnicodeWriter_Dealloc((_PyUnicodeWriter*)writer);
PyMem_Free(writer);
}
Expand Down
Loading