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

Use PyUnicodeWriter_WriteWideChar() in PyUnicode_FromFormat()
This commit is contained in:
Victor Stinner 2024-06-22 08:58:22 +02:00 committed by GitHub
parent 6ad26de6e8
commit 879d1f28bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 6 deletions

View File

@ -2612,11 +2612,7 @@ static int
unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str, unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str,
Py_ssize_t width, Py_ssize_t precision, int flags) Py_ssize_t width, Py_ssize_t precision, int flags)
{ {
/* UTF-8 */
Py_ssize_t length; Py_ssize_t length;
PyObject *unicode;
int res;
if (precision == -1) { if (precision == -1) {
length = wcslen(str); length = wcslen(str);
} }
@ -2626,11 +2622,17 @@ unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str,
length++; 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) if (unicode == NULL)
return -1; 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); Py_DECREF(unicode);
return res; return res;
} }