Skip to content

Commit b80e46e

Browse files
committed
Issue #14687: Avoid an useless duplicated string in PyUnicode_Format()
1 parent aff3cc6 commit b80e46e

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

Objects/unicodeobject.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14051,20 +14051,16 @@ PyUnicode_Format(PyObject *format, PyObject *args)
1405114051
}
1405214052
}
1405314053
/* Copy all characters, preserving len */
14054-
if (temp != NULL) {
14055-
assert(pbuf == PyUnicode_DATA(temp));
14056-
v = PyUnicode_Substring(temp, pindex, pindex + len);
14054+
if (pindex == 0 && len == PyUnicode_GET_LENGTH(temp)) {
14055+
r = _PyAccu_Accumulate(&acc, temp);
1405714056
}
1405814057
else {
14059-
const char *p = (const char *) pbuf;
14060-
assert(pbuf != NULL);
14061-
p += kind * pindex;
14062-
v = PyUnicode_FromKindAndData(kind, p, len);
14058+
v = PyUnicode_Substring(temp, pindex, pindex + len);
14059+
if (v == NULL)
14060+
goto onError;
14061+
r = _PyAccu_Accumulate(&acc, v);
14062+
Py_DECREF(v);
1406314063
}
14064-
if (v == NULL)
14065-
goto onError;
14066-
r = _PyAccu_Accumulate(&acc, v);
14067-
Py_DECREF(v);
1406814064
if (r)
1406914065
goto onError;
1407014066
if (width > len && repeat_accumulate(&acc, blank, width - len))

0 commit comments

Comments
 (0)