Commit aff3cc65 authored by Victor Stinner's avatar Victor Stinner

Issue #14687: Cleanup PyUnicode_Format()

parent 9a8ad0c5
...@@ -6986,8 +6986,6 @@ PyUnicode_DecodeASCII(const char *s, ...@@ -6986,8 +6986,6 @@ PyUnicode_DecodeASCII(const char *s,
v = PyUnicode_New(size, 127); v = PyUnicode_New(size, 127);
if (v == NULL) if (v == NULL)
goto onError; goto onError;
if (size == 0)
return v;
kind = PyUnicode_KIND(v); kind = PyUnicode_KIND(v);
data = PyUnicode_DATA(v); data = PyUnicode_DATA(v);
outpos = 0; outpos = 0;
...@@ -13856,20 +13854,22 @@ PyUnicode_Format(PyObject *format, PyObject *args) ...@@ -13856,20 +13854,22 @@ PyUnicode_Format(PyObject *format, PyObject *args)
"incomplete format"); "incomplete format");
goto onError; goto onError;
} }
if (c != '%') {
if (c == '%') {
_PyAccu_Accumulate(&acc, percent);
continue;
}
v = getnextarg(args, arglen, &argidx); v = getnextarg(args, arglen, &argidx);
if (v == NULL) if (v == NULL)
goto onError; goto onError;
}
sign = 0; sign = 0;
fill = ' '; fill = ' ';
fillobj = blank; fillobj = blank;
switch (c) { switch (c) {
case '%':
_PyAccu_Accumulate(&acc, percent);
continue;
case 's': case 's':
case 'r': case 'r':
case 'a': case 'a':
...@@ -13884,26 +13884,7 @@ PyUnicode_Format(PyObject *format, PyObject *args) ...@@ -13884,26 +13884,7 @@ PyUnicode_Format(PyObject *format, PyObject *args)
temp = PyObject_Repr(v); temp = PyObject_Repr(v);
else else
temp = PyObject_ASCII(v); temp = PyObject_ASCII(v);
if (temp == NULL)
goto onError;
if (PyUnicode_Check(temp))
/* nothing to do */;
else {
Py_DECREF(temp);
PyErr_SetString(PyExc_TypeError,
"%s argument has non-string str()");
goto onError;
}
} }
if (PyUnicode_READY(temp) == -1) {
Py_CLEAR(temp);
goto onError;
}
pbuf = PyUnicode_DATA(temp);
kind = PyUnicode_KIND(temp);
len = PyUnicode_GET_LENGTH(temp);
if (prec >= 0 && len > prec)
len = prec;
break; break;
case 'i': case 'i':
...@@ -13926,18 +13907,9 @@ PyUnicode_Format(PyObject *format, PyObject *args) ...@@ -13926,18 +13907,9 @@ PyUnicode_Format(PyObject *format, PyObject *args)
if (iobj!=NULL) { if (iobj!=NULL) {
if (PyLong_Check(iobj)) { if (PyLong_Check(iobj)) {
isnumok = 1; isnumok = 1;
sign = 1;
temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c));
Py_DECREF(iobj); Py_DECREF(iobj);
if (!temp)
goto onError;
if (PyUnicode_READY(temp) == -1) {
Py_CLEAR(temp);
goto onError;
}
pbuf = PyUnicode_DATA(temp);
kind = PyUnicode_KIND(temp);
len = PyUnicode_GET_LENGTH(temp);
sign = 1;
} }
else { else {
Py_DECREF(iobj); Py_DECREF(iobj);
...@@ -13962,21 +13934,12 @@ PyUnicode_Format(PyObject *format, PyObject *args) ...@@ -13962,21 +13934,12 @@ PyUnicode_Format(PyObject *format, PyObject *args)
case 'F': case 'F':
case 'g': case 'g':
case 'G': case 'G':
temp = formatfloat(v, flags, prec, c);
if (!temp)
goto onError;
if (PyUnicode_READY(temp) == -1) {
Py_CLEAR(temp);
goto onError;
}
pbuf = PyUnicode_DATA(temp);
kind = PyUnicode_KIND(temp);
len = PyUnicode_GET_LENGTH(temp);
sign = 1; sign = 1;
if (flags & F_ZERO) { if (flags & F_ZERO) {
fill = '0'; fill = '0';
fillobj = zero; fillobj = zero;
} }
temp = formatfloat(v, flags, prec, c);
break; break;
case 'c': case 'c':
...@@ -13985,11 +13948,6 @@ PyUnicode_Format(PyObject *format, PyObject *args) ...@@ -13985,11 +13948,6 @@ PyUnicode_Format(PyObject *format, PyObject *args)
if (ch == (Py_UCS4) -1) if (ch == (Py_UCS4) -1)
goto onError; goto onError;
temp = _PyUnicode_FromUCS4(&ch, 1); temp = _PyUnicode_FromUCS4(&ch, 1);
if (temp == NULL)
goto onError;
pbuf = PyUnicode_DATA(temp);
kind = PyUnicode_KIND(temp);
len = PyUnicode_GET_LENGTH(temp);
break; break;
} }
...@@ -14002,6 +13960,22 @@ PyUnicode_Format(PyObject *format, PyObject *args) ...@@ -14002,6 +13960,22 @@ PyUnicode_Format(PyObject *format, PyObject *args)
fmtpos - 1); fmtpos - 1);
goto onError; goto onError;
} }
if (temp == NULL)
goto onError;
assert (PyUnicode_Check(temp));
if (PyUnicode_READY(temp) == -1) {
Py_CLEAR(temp);
goto onError;
}
kind = PyUnicode_KIND(temp);
pbuf = PyUnicode_DATA(temp);
len = PyUnicode_GET_LENGTH(temp);
if (c == 's' || c == 'r' || c == 'a') {
if (prec >= 0 && len > prec)
len = prec;
}
/* pbuf is initialized here. */ /* pbuf is initialized here. */
pindex = 0; pindex = 0;
if (sign) { if (sign) {
......
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