https://github.com/python/cpython/commit/1877543d03d323d581b5fc0f19eff501926ba151
commit: 1877543d03d323d581b5fc0f19eff501926ba151
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2024-10-09T22:04:50Z
summary:

gh-125196: Use PyUnicodeWriter for repr(structseq) (#125219)

Replace the private _PyUnicodeWriter with the public PyUnicodeWriter.

* Avoid temporary PyUnicode_DecodeUTF8(): call
  PyUnicodeWriter_WriteUTF8() instead.
* Avoid temporary PyObject_Repr(): call PyUnicodeWriter_WriteRepr()
  instead.

files:
M Objects/structseq.c

diff --git a/Objects/structseq.c b/Objects/structseq.c
index ee3dbf9d4c047a..6092742835400b 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -266,83 +266,66 @@ static PyObject *
 structseq_repr(PyStructSequence *obj)
 {
     PyTypeObject *typ = Py_TYPE(obj);
-    _PyUnicodeWriter writer;
 
-    /* Write "typename(" */
-    PyObject *type_name = PyUnicode_DecodeUTF8(typ->tp_name,
-                                               strlen(typ->tp_name),
-                                               NULL);
-    if (type_name == NULL) {
+    // count 5 characters per item: "x=1, "
+    Py_ssize_t type_name_len = strlen(typ->tp_name);
+    Py_ssize_t prealloc = (type_name_len + 1
+                           + VISIBLE_SIZE(obj) * 5 + 1);
+    PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc);
+    if (writer == NULL) {
         return NULL;
     }
 
-    _PyUnicodeWriter_Init(&writer);
-    writer.overallocate = 1;
-    /* count 5 characters per item: "x=1, " */
-    writer.min_length = (PyUnicode_GET_LENGTH(type_name) + 1
-                         + VISIBLE_SIZE(obj) * 5 + 1);
-
-    if (_PyUnicodeWriter_WriteStr(&writer, type_name) < 0) {
-        Py_DECREF(type_name);
+    // Write "typename("
+    if (PyUnicodeWriter_WriteUTF8(writer, typ->tp_name, type_name_len) < 0) {
         goto error;
     }
-    Py_DECREF(type_name);
-
-    if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) {
+    if (PyUnicodeWriter_WriteChar(writer, '(') < 0) {
         goto error;
     }
 
     for (Py_ssize_t i=0; i < VISIBLE_SIZE(obj); i++) {
         if (i > 0) {
-            /* Write ", " */
-            if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
+            // Write ", "
+            if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
+                goto error;
+            }
+            if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
                 goto error;
             }
         }
 
-        /* Write "name=repr" */
+        // Write name
         const char *name_utf8 = typ->tp_members[i].name;
         if (name_utf8 == NULL) {
-            PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %zd 
name is NULL"
+            PyErr_Format(PyExc_SystemError,
+                         "In structseq_repr(), member %zd name is NULL"
                          " for type %.500s", i, typ->tp_name);
             goto error;
         }
-
-        PyObject *name = PyUnicode_DecodeUTF8(name_utf8, strlen(name_utf8), 
NULL);
-        if (name == NULL) {
-            goto error;
-        }
-        if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
-            Py_DECREF(name);
+        if (PyUnicodeWriter_WriteUTF8(writer, name_utf8, -1) < 0) {
             goto error;
         }
-        Py_DECREF(name);
 
-        if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) {
+        // Write "=" + repr(value)
+        if (PyUnicodeWriter_WriteChar(writer, '=') < 0) {
             goto error;
         }
-
         PyObject *value = PyStructSequence_GetItem((PyObject*)obj, i);
         assert(value != NULL);
-        PyObject *repr = PyObject_Repr(value);
-        if (repr == NULL) {
-            goto error;
-        }
-        if (_PyUnicodeWriter_WriteStr(&writer, repr) < 0) {
-            Py_DECREF(repr);
+        if (PyUnicodeWriter_WriteRepr(writer, value) < 0) {
             goto error;
         }
-        Py_DECREF(repr);
     }
 
-    if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) {
+    if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
         goto error;
     }
 
-    return _PyUnicodeWriter_Finish(&writer);
+    return PyUnicodeWriter_Finish(writer);
 
 error:
-    _PyUnicodeWriter_Dealloc(&writer);
+    PyUnicodeWriter_Discard(writer);
     return NULL;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to