diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-06-29 19:36:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-29 19:36:41 (GMT) |
commit | e67f7db3c34f70536f36c56bb82e33c3512a53a3 (patch) | |
tree | 51530f17f218fde20aca7866d663b46bac614305 /Objects/unicodeobject.c | |
parent | 5b96370030707b68e8a5b787e933654297ddbc98 (diff) | |
download | cpython-e67f7db3c34f70536f36c56bb82e33c3512a53a3.zip cpython-e67f7db3c34f70536f36c56bb82e33c3512a53a3.tar.gz cpython-e67f7db3c34f70536f36c56bb82e33c3512a53a3.tar.bz2 |
bpo-37999: Simplify the conversion code for %c, %d, %x, etc. (GH-20437)
Since PyLong_AsLong() no longer use __int__, explicit call
of PyNumber_Index() before it is no longer needed.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 34 |
1 files changed, 11 insertions, 23 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 6fa6c3f..c46ba4a 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -14641,19 +14641,14 @@ mainformatlong(PyObject *v, if (!PyLong_Check(v)) { if (type == 'o' || type == 'x' || type == 'X') { iobj = _PyNumber_Index(v); - if (iobj == NULL) { - if (PyErr_ExceptionMatches(PyExc_TypeError)) - goto wrongtype; - return -1; - } } else { iobj = PyNumber_Long(v); - if (iobj == NULL ) { - if (PyErr_ExceptionMatches(PyExc_TypeError)) - goto wrongtype; - return -1; - } + } + if (iobj == NULL ) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) + goto wrongtype; + return -1; } assert(PyLong_Check(iobj)); } @@ -14736,24 +14731,17 @@ formatchar(PyObject *v) goto onError; } else { - PyObject *iobj; - long x; - /* make sure number is a type of integer */ - if (!PyLong_Check(v)) { - iobj = PyNumber_Index(v); - if (iobj == NULL) { + int overflow; + long x = PyLong_AsLongAndOverflow(v, &overflow); + if (x == -1 && PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) { goto onError; } - x = PyLong_AsLong(iobj); - Py_DECREF(iobj); - } - else { - x = PyLong_AsLong(v); + return (Py_UCS4) -1; } - if (x == -1 && PyErr_Occurred()) - goto onError; if (x < 0 || x > MAX_UNICODE) { + /* this includes an overflow in converting to C long */ PyErr_SetString(PyExc_OverflowError, "%c arg not in range(0x110000)"); return (Py_UCS4) -1; |