Skip to content

Commit 879d1f2

Browse files
authored
gh-119182: Use PyUnicodeWriter_WriteWideChar() (#120851)
Use PyUnicodeWriter_WriteWideChar() in PyUnicode_FromFormat()
1 parent 6ad26de commit 879d1f2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Objects/unicodeobject.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,11 +2612,7 @@ static int
26122612
unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str,
26132613
Py_ssize_t width, Py_ssize_t precision, int flags)
26142614
{
2615-
/* UTF-8 */
26162615
Py_ssize_t length;
2617-
PyObject *unicode;
2618-
int res;
2619-
26202616
if (precision == -1) {
26212617
length = wcslen(str);
26222618
}
@@ -2626,11 +2622,17 @@ unicode_fromformat_write_wcstr(_PyUnicodeWriter *writer, const wchar_t *str,
26262622
length++;
26272623
}
26282624
}
2629-
unicode = PyUnicode_FromWideChar(str, length);
2625+
2626+
if (width < 0) {
2627+
return PyUnicodeWriter_WriteWideChar((PyUnicodeWriter*)writer,
2628+
str, length);
2629+
}
2630+
2631+
PyObject *unicode = PyUnicode_FromWideChar(str, length);
26302632
if (unicode == NULL)
26312633
return -1;
26322634

2633-
res = unicode_fromformat_write_str(writer, unicode, width, -1, flags);
2635+
int res = unicode_fromformat_write_str(writer, unicode, width, -1, flags);
26342636
Py_DECREF(unicode);
26352637
return res;
26362638
}

0 commit comments

Comments
 (0)