-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-125196: Use PyUnicodeWriter for repr(list) #125202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8c33f91
gh-125196: Use PyUnicodeWriter for repr(list)
vstinner 5fa3466
Fix error handling
vstinner 0273017
Avoid PyUnicodeWriter_WriteUTF8()
vstinner 8b33b0e
PyUnicodeWriter_Discard(NULL) does nothing
vstinner 9c910b5
Revert "PyUnicodeWriter_Discard(NULL) does nothing"
vstinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -522,49 +522,47 @@ 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_WriteUTF8(writer, ", ", 2) < 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); | ||
| if (writer != NULL) { | ||
| PyUnicodeWriter_Discard(writer); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I modified |
||
| } | ||
| Py_ReprLeave((PyObject *)v); | ||
| return NULL; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is faster,
PyUnicodeWriter_WriteUTF8(writer, ", ", 2)or twoPyUnicodeWriter_WriteChar()?This is an idle question. Even if the latter is marginally faster, it may still be more preferable to use the former. But if the difference is significant, this may be a question for optimization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two PyUnicodeWriter_WriteChar() calls is faster: