Mercurial > cpython-lru_cache-weakref
diff Objects/stringobject.c @ 45500:c03aff6d07f5 legacy-trunk
Issue 1742669. Now %d accepts very big float numbers.
Thanks Gabriel Genellina.
| author | Facundo Batista <facundobatista@gmail.com> |
|---|---|
| date | Sun, 24 Feb 2008 03:17:21 +0000 |
| parents | b7231bf6bfb7 |
| children | 162c6952ea34 |
line wrap: on
line diff
--- a/Objects/stringobject.c Sun Feb 24 02:40:58 2008 +0000 +++ b/Objects/stringobject.c Sun Feb 24 03:17:21 2008 +0000 @@ -4585,6 +4585,7 @@ int prec = -1; int c = '\0'; int fill; + int isnumok; PyObject *v = NULL; PyObject *temp = NULL; char *pbuf; @@ -4786,23 +4787,52 @@ case 'X': if (c == 'i') c = 'd'; - if (PyLong_Check(v)) { - int ilen; - temp = _PyString_FormatLong(v, flags, - prec, c, &pbuf, &ilen); - len = ilen; - if (!temp) - goto error; - sign = 1; + isnumok = 0; + if (PyNumber_Check(v)) { + PyObject *iobj=NULL; + + if (PyInt_Check(v) || (PyLong_Check(v))) { + iobj = v; + Py_INCREF(iobj); + } + else { + iobj = PyNumber_Int(v); + if (iobj==NULL) iobj = PyNumber_Long(v); + } + if (iobj!=NULL) { + if (PyInt_Check(iobj)) { + isnumok = 1; + pbuf = formatbuf; + len = formatint(pbuf, + sizeof(formatbuf), + flags, prec, c, iobj); + Py_DECREF(iobj); + if (len < 0) + goto error; + sign = 1; + } + else if (PyLong_Check(iobj)) { + int ilen; + + isnumok = 1; + temp = _PyString_FormatLong(iobj, flags, + prec, c, &pbuf, &ilen); + Py_DECREF(iobj); + len = ilen; + if (!temp) + goto error; + sign = 1; + } + else { + Py_DECREF(iobj); + } + } } - else { - pbuf = formatbuf; - len = formatint(pbuf, - sizeof(formatbuf), - flags, prec, c, v); - if (len < 0) - goto error; - sign = 1; + if (!isnumok) { + PyErr_Format(PyExc_TypeError, + "%%%c format: a number is required, " + "not %.200s", c, Py_TYPE(v)->tp_name); + goto error; } if (flags & F_ZERO) fill = '0';
