Skip to content

Commit

Permalink
gh-119182: Use PyUnicodeWriter_WriteWideChar() (#120851)
Browse files Browse the repository at this point in the history
Use PyUnicodeWriter_WriteWideChar() in PyUnicode_FromFormat()
  • Loading branch information
vstinner authored Jun 22, 2024
1 parent 6ad26de commit 879d1f2
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 879d1f2

Please sign in to comment.