X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/catacomb-python/blobdiff_plain/6b54260d1f5312a3ec58be3ee1eb168ddf9cf2e2..f1b0cf0da6b3bcc530d7f72982278510d94f6456:/mp.c diff --git a/mp.c b/mp.c index eb6a496..79c6cf2 100644 --- a/mp.c +++ b/mp.c @@ -163,7 +163,7 @@ PyObject *gf_pywrap(mp *x) return ((PyObject *)z); } -int mp_tolong_checked(mp *x, long *l) +int mp_tolong_checked(mp *x, long *l, int must) { static mp *longmin = 0, *longmax = 0; int rc = -1; @@ -172,8 +172,10 @@ int mp_tolong_checked(mp *x, long *l) longmin = mp_fromlong(MP_NEW, LONG_MIN); longmax = mp_fromlong(MP_NEW, LONG_MAX); } - if (MP_CMP(x, <, longmin) || MP_CMP(x, >, longmax)) - VALERR("mp out of range for int"); + if (MP_CMP(x, <, longmin) || MP_CMP(x, >, longmax)) { + if (must) VALERR("mp out of range for int"); + else goto end; + } *l = mp_tolong(x); rc = 0; end: @@ -362,7 +364,7 @@ static PyObject *mp_pyid(PyObject *x) { RETURN_OBJ(x); } PyObject *z = 0; \ long n; \ if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \ - if (mp_tolong_checked(yy, &n)) goto end; \ + if (mp_tolong_checked(yy, &n, 1)) goto end; \ if (n < 0) \ z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \ else \ @@ -483,8 +485,8 @@ static int mp_pynonzerop(PyObject *x) { return !MP_ZEROP(MP_X(x)); } static PyObject *mp_pyint(PyObject *x) { long l; - if (mp_tolong_checked(MP_X(x), &l)) return (0); - return (PyInt_FromLong(l)); + if (!mp_tolong_checked(MP_X(x), &l, 0)) return (PyInt_FromLong(l)); + else return mp_topylong(MP_X(x)); } static PyObject *mp_pylong(PyObject *x) { return (mp_topylong(MP_X(x))); }