summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-06-22 06:58:22 (GMT)
committerGitHub <noreply@github.com>2024-06-22 06:58:22 (GMT)
commit879d1f28bb97bcecddca0824276877aaf97f25b3 (patch)
treeddf30861caf77912b70db5d97f298ad27a0d1fa5
parent6ad26de6e8ab61b035e7ecfff9791c2b349c3ad0 (diff)
downloadcpython-879d1f28bb97bcecddca0824276877aaf97f25b3.zip
cpython-879d1f28bb97bcecddca0824276877aaf97f25b3.tar.gz
cpython-879d1f28bb97bcecddca0824276877aaf97f25b3.tar.bz2
gh-119182: Use PyUnicodeWriter_WriteWideChar() (#120851)
Use PyUnicodeWriter_WriteWideChar() in PyUnicode_FromFormat()
-rw-r--r--Objects/unicodeobject.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 74a7438..4c174cb 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;
}