diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2007-12-04 22:10:37 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2007-12-04 22:10:37 (GMT) |
commit | d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6 (patch) | |
tree | 17489e6ea4df32ba3b3bbda6e4b31155a460f265 /Objects/longobject.c | |
parent | 0fbab7ff8d2efd92e222fcc13c0aff0998c3c158 (diff) | |
download | cpython-d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6.zip cpython-d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6.tar.gz cpython-d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6.tar.bz2 |
Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r-- | Objects/longobject.c | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index 1e20485..cf7cb47 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -299,7 +299,7 @@ PyLong_FromDouble(double dval) Returns -1 and sets an error condition if overflow occurs. */ long -PyLong_AsLong(PyObject *vv) +PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) { /* This version by Tim Peters */ register PyLongObject *v; @@ -309,6 +309,7 @@ PyLong_AsLong(PyObject *vv) int sign; int do_decref = 0; /* if nb_int was called */ + *overflow = 0; if (vv == NULL) { PyErr_BadInternalCall(); return -1; @@ -358,8 +359,7 @@ PyLong_AsLong(PyObject *vv) prev = x; x = (x << PyLong_SHIFT) + v->ob_digit[i]; if ((x >> PyLong_SHIFT) != prev) { - PyErr_SetString(PyExc_OverflowError, - "Python int too large to convert to C long"); + *overflow = Py_Size(v) > 0 ? 1 : -1; goto exit; } } @@ -373,8 +373,8 @@ PyLong_AsLong(PyObject *vv) res = LONG_MIN; } else { - PyErr_SetString(PyExc_OverflowError, - "Python int too large to convert to C long"); + *overflow = Py_Size(v) > 0 ? 1 : -1; + /* res is already set to -1 */ } } exit: @@ -384,6 +384,20 @@ PyLong_AsLong(PyObject *vv) return res; } +long +PyLong_AsLong(PyObject *obj) +{ + int overflow; + long result = PyLong_AsLongAndOverflow(obj, &overflow); + if (overflow) { + /* XXX: could be cute and give a different + message for overflow == -1 */ + PyErr_SetString(PyExc_OverflowError, + "Python int too large to convert to C long"); + } + return result; +} + int _PyLong_FitsInLong(PyObject *vv) { |