summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
42d30ef)
Mostly, Python handles the error from the `int' conversion and falls
back to long, but there's something weird in iteration, where if you say
for i in ...:
print '%d' % x
then the loop finishes and /then/ you get an exception for the overflow
from the failed conversion of x to an `int'.
Follow Python's actual behaviour: have `mp_tolong_checked' take an extra
argument indicating whether to throw an exception, and modify most of
the call sites to fall back to a conversion based on `mp_topylong'.
extern mp *mp_frompyobject(PyObject *, int);
extern PyObject *mp_topystring(mp *, int,
const char *, const char *, const char *);
extern mp *mp_frompyobject(PyObject *, int);
extern PyObject *mp_topystring(mp *, int,
const char *, const char *, const char *);
-extern int mp_tolong_checked(mp *, long *);
+extern int mp_tolong_checked(mp *, long *, int);
/*----- Abstract fields ---------------------------------------------------*/
/*----- Abstract fields ---------------------------------------------------*/
PyObject *rc = 0;
if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
getecptout(&p, me);
PyObject *rc = 0;
if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
getecptout(&p, me);
- if (mp_tolong_checked(p.x, &l)) goto end;
- rc = PyInt_FromLong(l);
+ if (!mp_tolong_checked(p.x, &l, 0)) rc = PyInt_FromLong(l);
+ else rc = mp_topylong(p.x);
end:
EC_DESTROY(&p);
return (rc);
end:
EC_DESTROY(&p);
return (rc);
static PyObject *fe_pyint(PyObject *x)
{
long l;
static PyObject *fe_pyint(PyObject *x)
{
long l;
mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
- if (mp_tolong_checked(xx, &l)) { MP_DROP(xx); return (0); }
+ if (!mp_tolong_checked(xx, &l, 0)) rc = PyInt_FromLong(l);
+ else rc = mp_topylong(xx);
- return (PyInt_FromLong(l));
}
static PyObject *fe_pylong(PyObject *x)
}
static PyObject *fe_pylong(PyObject *x)
if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
TYERR("can't convert to integer");
if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
TYERR("can't convert to integer");
- if (mp_tolong_checked(x, &l)) goto end;
- rc = PyInt_FromLong(l);
+ if (!mp_tolong_checked(x, &l, 0)) rc = PyInt_FromLong(l);
+ else rc = mp_topylong(x);
end:
mp_drop(x);
return (rc);
end:
mp_drop(x);
return (rc);
return ((PyObject *)z);
}
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;
{
static mp *longmin = 0, *longmax = 0;
int rc = -1;
longmin = mp_fromlong(MP_NEW, LONG_MIN);
longmax = mp_fromlong(MP_NEW, LONG_MAX);
}
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:
*l = mp_tolong(x);
rc = 0;
end:
PyObject *z = 0; \
long n; \
if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
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 \
if (n < 0) \
z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \
else \
static PyObject *mp_pyint(PyObject *x)
{
long l;
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))); }
}
static PyObject *mp_pylong(PyObject *x)
{ return (mp_topylong(MP_X(x))); }
long l;
PyObject *rc = 0;
long l;
PyObject *rc = 0;
- if (mp_tolong_checked(PFILT_F(me)->m, &l)) goto end;
- rc = PyInt_FromLong(l);
-end:
+ if (!mp_tolong_checked(PFILT_F(me)->m, &l, 0)) rc = PyInt_FromLong(l);
+ else rc = mp_topylong(PFILT_F(me)->m);