]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-119182: Use PyUnicodeWriter_WriteWideChar() (#120851)
authorVictor Stinner <vstinner@python.org>
Sat, 22 Jun 2024 06:58:22 +0000 (08:58 +0200)
committerGitHub <noreply@github.com>
Sat, 22 Jun 2024 06:58:22 +0000 (08:58 +0200)
Use PyUnicodeWriter_WriteWideChar() in PyUnicode_FromFormat()

Objects/unicodeobject.c

index 74a743812c9c78d2a98daefd97045226251dde23..4c174cbc7510914e8f0c1e06cdb01b5b1658e0e0 100644 (file)
@@ -2612,11 +2612,7 @@ static int
 unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str,
                               Py_ssize_t width, Py_ssize_t precision, int flags)
 {
-    /* UTF-8 */
     Py_ssize_t length;
-    PyObject *unicode;
-    int res;
-
     if (precision == -1) {
         length = wcslen(str);
     }
@@ -2626,11 +2622,17 @@ unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str,
             length++;
         }
     }
-    unicode = PyUnicode_FromWideChar(str, length);
+
+    if (width < 0) {
+        return PyUnicodeWriter_WriteWideChar((PyUnicodeWriter*)writer,
+                                             str, length);
+    }
+
+    PyObject *unicode = PyUnicode_FromWideChar(str, length);
     if (unicode == NULL)
         return -1;
 
-    res = unicode_fromformat_write_str(writer, unicode, width, -1, flags);
+    int res = unicode_fromformat_write_str(writer, unicode, width, -1, flags);
     Py_DECREF(unicode);
     return res;
 }