Commit 53f3d4ac authored by Marc-André Lemburg's avatar Marc-André Lemburg

[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix...

[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
to the way string formatting delegated work to the Unicode formatting
function.
parent b96d8020
...@@ -2666,7 +2666,7 @@ PyString_Format(PyObject *format, PyObject *args) ...@@ -2666,7 +2666,7 @@ PyString_Format(PyObject *format, PyObject *args)
char *fmt, *res; char *fmt, *res;
int fmtcnt, rescnt, reslen, arglen, argidx; int fmtcnt, rescnt, reslen, arglen, argidx;
int args_owned = 0; int args_owned = 0;
PyObject *result, *orig_args; PyObject *result, *orig_args, *v, *w;
PyObject *dict = NULL; PyObject *dict = NULL;
if (format == NULL || !PyString_Check(format) || args == NULL) { if (format == NULL || !PyString_Check(format) || args == NULL) {
PyErr_BadInternalCall(); PyErr_BadInternalCall();
...@@ -3055,22 +3055,28 @@ PyString_Format(PyObject *format, PyObject *args) ...@@ -3055,22 +3055,28 @@ PyString_Format(PyObject *format, PyObject *args)
Py_INCREF(orig_args); Py_INCREF(orig_args);
args = orig_args; args = orig_args;
} }
/* Paste rest of format string to what we have of the result args_owned = 1;
string; we reuse result for this */ /* Take what we have of the result and let the Unicode formatting
function format the rest of the input. */
rescnt = res - PyString_AS_STRING(result); rescnt = res - PyString_AS_STRING(result);
if (_PyString_Resize(&result, rescnt))
goto error;
fmtcnt = PyString_GET_SIZE(format) - \ fmtcnt = PyString_GET_SIZE(format) - \
(fmt - PyString_AS_STRING(format)); (fmt - PyString_AS_STRING(format));
if (_PyString_Resize(&result, rescnt + fmtcnt)) { format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL);
Py_DECREF(args); if (format == NULL)
goto error; goto error;
} v = PyUnicode_Format(format, args);
memcpy(PyString_AS_STRING(result) + rescnt, fmt, fmtcnt);
format = result;
/* Let Unicode do its magic */
result = PyUnicode_Format(format, args);
Py_DECREF(format); Py_DECREF(format);
if (v == NULL)
goto error;
/* Paste what we have (result) to what the Unicode formatting
function returned (v) and return the result (or error) */
w = PyUnicode_Concat(result, v);
Py_DECREF(result);
Py_DECREF(v);
Py_DECREF(args); Py_DECREF(args);
return result; return w;
error: error:
Py_DECREF(result); Py_DECREF(result);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment