https://github.com/python/cpython/commit/879d1f28bb97bcecddca0824276877aaf97f25b3
commit: 879d1f28bb97bcecddca0824276877aaf97f25b3
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2024-06-22T08:58:22+02:00
summary:

gh-119182: Use PyUnicodeWriter_WriteWideChar() (#120851)

Use PyUnicodeWriter_WriteWideChar() in PyUnicode_FromFormat()

files:
M Objects/unicodeobject.c

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 74a743812c9c78..4c174cbc751091 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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;
 }

_______________________________________________
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