From fa2bc728a4b66cf93192dab05b0422874093b257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Tue, 4 Dec 2007 22:10:37 +0000 Subject: Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow. --- Python/bltinmodule.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index b57083b455..56ec738f30 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1623,10 +1623,14 @@ builtin_sum(PyObject *self, PyObject *args) Assumes all inputs are the same type. If the assumption fails, default to the more general routine. */ - if (PyInt_CheckExact(result)) { - long i_result = PyLong_AS_LONG(result); - Py_DECREF(result); - result = NULL; + if (PyLong_CheckExact(result)) { + int overflow; + long i_result = PyLong_AsLongAndOverflow(result, &overflow); + /* If this already overflowed, don't even enter the loop. */ + if (overflow == 0) { + Py_DECREF(result); + result = NULL; + } while(result == NULL) { item = PyIter_Next(iter); if (item == NULL) { @@ -1635,10 +1639,10 @@ builtin_sum(PyObject *self, PyObject *args) return NULL; return PyLong_FromLong(i_result); } - if (PyInt_CheckExact(item)) { - long b = PyLong_AS_LONG(item); + if (PyLong_CheckExact(item)) { + long b = PyLong_AsLongAndOverflow(item, &overflow); long x = i_result + b; - if ((x^i_result) >= 0 || (x^b) >= 0) { + if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) { i_result = x; Py_DECREF(item); continue; @@ -1676,12 +1680,17 @@ builtin_sum(PyObject *self, PyObject *args) Py_DECREF(item); continue; } - if (PyInt_CheckExact(item)) { - PyFPE_START_PROTECT("add", return 0) - f_result += (double)PyLong_AS_LONG(item); - PyFPE_END_PROTECT(f_result) - Py_DECREF(item); - continue; + if (PyLong_CheckExact(item)) { + long value; + int overflow; + value = PyLong_AsLongAndOverflow(item, &overflow); + if (!overflow) { + PyFPE_START_PROTECT("add", return 0) + f_result += (double)value; + PyFPE_END_PROTECT(f_result) + Py_DECREF(item); + continue; + } } result = PyFloat_FromDouble(f_result); temp = PyNumber_Add(result, item); -- cgit v1.2.1