FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159813: Add a fast path for None in PyUnicodeWriter_WriteRepr by corona10 · Pull Request #159814 · python/cpython · GitHub

Repository navigation

gh-159813: Add a fast path for None in PyUnicodeWriter_WriteRepr - #159814

Merged
corona10 merged 3 commits into
python:mainfrom
corona10:gh-159813
Oct 20, 2026
Merged

corona10 merged 3 commits into
python:mainfrom
corona10:gh-159813

Conversation

corona10 commented Oct 19, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Member

Can you please apply this patch to add tests on PyUnicodeWriter_WriteRepr()?

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 <vstinner@python.org>

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

corona10 merged commit 4c73f50 into python:main Oct 20, 2026
60 checks passed
corona10 deleted the gh-159813 branch October 20, 2026 04:00
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL