Repository navigation
gh-159813: Add a fast path for None in PyUnicodeWriter_WriteRepr - #159814
Merged
Merged
Conversation
Member
|
Can you please apply this patch to add tests on diff --git a/Lib/test/test_capi/test_unicode.py b/Lib/test/test_capi/test_unicode.py
index 9237809e7f0..0358bc38dfd 100644
--- a/Lib/test/test_capi/test_unicode.py
+++ b/Lib/test/test_capi/test_unicode.py
@@ -2038,6 +2038,21 @@ def __str__(self):
self.assertEqual(result, 'abc')
self.assertIs(type(result), str)
+ def test_repr(self):
+ # Test PyUnicodeWriter_WriteRepr()
+ writer = self.create_writer(0)
+ writer.write_str('str=')
+ writer.write_repr('abc')
+ writer.write_str(' int=')
+ writer.write_repr(123)
+ writer.write_str(' none=')
+ writer.write_repr_none()
+ obj = object()
+ writer.write_str(' obj=')
+ writer.write_repr(obj)
+ self.assertEqual(writer.finish(),
+ f"str='abc' int=123 none=None obj={obj!r}")
+
def test_repr_null(self):
writer = self.create_writer(0)
writer.write_utf8(b'var=', -1)
diff --git a/Modules/_testcapi/unicode.c b/Modules/_testcapi/unicode.c
index ec3e9a62351..19178b2977e 100644
--- a/Modules/_testcapi/unicode.c
+++ b/Modules/_testcapi/unicode.c
@@ -689,6 +689,23 @@ writer_write_repr(PyObject *self_raw, PyObject *obj)
}
+// write_repr(None) calls PyUnicodeWriter_WriteRepr(writer, NULL), so add
+// a specialized function calling PyUnicodeWriter_WriteRepr(writer, Py_None).
+static PyObject*
+writer_write_repr_none(PyObject *self_raw, PyObject *Py_UNUSED(arg))
+{
+ WriterObject *self = (WriterObject *)self_raw;
+ if (writer_check(self) < 0) {
+ return NULL;
+ }
+
+ if (PyUnicodeWriter_WriteRepr(self->writer, Py_None) < 0) {
+ return NULL;
+ }
+ Py_RETURN_NONE;
+}
+
+
static PyObject*
writer_write_substring(PyObject *self_raw, PyObject *args)
{
@@ -854,6 +871,7 @@ static PyMethodDef writer_methods[] = {
{"write_ucs4", _PyCFunction_CAST(writer_write_ucs4), METH_VARARGS},
{"write_str", _PyCFunction_CAST(writer_write_str), METH_O},
{"write_repr", _PyCFunction_CAST(writer_write_repr), METH_O},
+ {"write_repr_none", _PyCFunction_CAST(writer_write_repr_none), METH_NOARGS},
{"write_substring", _PyCFunction_CAST(writer_write_substring), METH_VARARGS},
{"write_latin1", _PyCFunction_CAST(writer_write_latin1), METH_VARARGS},
{"decodeutf8stateful", _PyCFunction_CAST(writer_decodeutf8stateful), METH_VARARGS}, |
Co-authored-by: Victor Stinner <[email protected]>
vstinner
approved these changes
Oct 20, 2026
vstinner
left a comment
Member
There was a problem hiding this comment.
LGTM.
The implementation looks correct to me, and I think that it's worth it to optimize this use case according to statistics that you collected.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Uh oh!
There was an error while loading. Please reload this page.