diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2012-04-30 03:19:21 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2012-04-30 03:19:21 (GMT) |
commit | aff3cc659b8ddb0fc7f49077dda1922a67658acd (patch) | |
tree | b05dda1069294612f69b82ef753bb961ac522b1a /Objects/unicodeobject.c | |
parent | 9a8ad0c5ea6d9562d94d6e4f8c0be28927cdcaa6 (diff) | |
download | cpython-aff3cc659b8ddb0fc7f49077dda1922a67658acd.zip cpython-aff3cc659b8ddb0fc7f49077dda1922a67658acd.tar.gz cpython-aff3cc659b8ddb0fc7f49077dda1922a67658acd.tar.bz2 |
Issue #14687: Cleanup PyUnicode_Format()
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 82 |
1 files changed, 28 insertions, 54 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 68f11ff..8959a97 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6986,8 +6986,6 @@ PyUnicode_DecodeASCII(const char *s, v = PyUnicode_New(size, 127); if (v == NULL) goto onError; - if (size == 0) - return v; kind = PyUnicode_KIND(v); data = PyUnicode_DATA(v); outpos = 0; @@ -13856,20 +13854,22 @@ PyUnicode_Format(PyObject *format, PyObject *args) "incomplete format"); goto onError; } - if (c != '%') { - v = getnextarg(args, arglen, &argidx); - if (v == NULL) - goto onError; + + if (c == '%') { + _PyAccu_Accumulate(&acc, percent); + continue; } + + + v = getnextarg(args, arglen, &argidx); + if (v == NULL) + goto onError; + sign = 0; fill = ' '; fillobj = blank; switch (c) { - case '%': - _PyAccu_Accumulate(&acc, percent); - continue; - case 's': case 'r': case 'a': @@ -13884,26 +13884,7 @@ PyUnicode_Format(PyObject *format, PyObject *args) temp = PyObject_Repr(v); else 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; case 'i': @@ -13926,18 +13907,9 @@ PyUnicode_Format(PyObject *format, PyObject *args) if (iobj!=NULL) { if (PyLong_Check(iobj)) { isnumok = 1; + sign = 1; temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); 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 { Py_DECREF(iobj); @@ -13962,21 +13934,12 @@ PyUnicode_Format(PyObject *format, PyObject *args) case 'F': 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; if (flags & F_ZERO) { fill = '0'; fillobj = zero; } + temp = formatfloat(v, flags, prec, c); break; case 'c': @@ -13985,11 +13948,6 @@ PyUnicode_Format(PyObject *format, PyObject *args) if (ch == (Py_UCS4) -1) goto onError; temp = _PyUnicode_FromUCS4(&ch, 1); - if (temp == NULL) - goto onError; - pbuf = PyUnicode_DATA(temp); - kind = PyUnicode_KIND(temp); - len = PyUnicode_GET_LENGTH(temp); break; } @@ -14002,6 +13960,22 @@ PyUnicode_Format(PyObject *format, PyObject *args) fmtpos - 1); 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. */ pindex = 0; if (sign) { |