Changes for Lee Busby's SIGFPE patch set.

Surround various f.p. operations with  PyFPE_{START,END}_PROTECT macros.
This commit is contained in:
Guido van Rossum 1997-02-14 22:54:21 +00:00
parent 7d4266ed5a
commit 09e6ad0c1e
3 changed files with 63 additions and 10 deletions

View File

@ -364,7 +364,11 @@ complex_add(v, w)
complexobject *v; complexobject *v;
complexobject *w; complexobject *w;
{ {
return newcomplexobject(c_sum(v->cval,w->cval)); Py_complex result;
PyFPE_START_PROTECT("complex_add", return 0)
result = c_sum(v->cval,w->cval);
PyFPE_END_PROTECT
return newcomplexobject(result);
} }
static object * static object *
@ -372,7 +376,11 @@ complex_sub(v, w)
complexobject *v; complexobject *v;
complexobject *w; complexobject *w;
{ {
return newcomplexobject(c_diff(v->cval,w->cval)); Py_complex result;
PyFPE_START_PROTECT("complex_sub", return 0)
result = c_diff(v->cval,w->cval);
PyFPE_END_PROTECT
return newcomplexobject(result);
} }
static object * static object *
@ -380,7 +388,11 @@ complex_mul(v, w)
complexobject *v; complexobject *v;
complexobject *w; complexobject *w;
{ {
return newcomplexobject(c_prod(v->cval,w->cval)); Py_complex result;
PyFPE_START_PROTECT("complex_mul", return 0)
result = c_prod(v->cval,w->cval);
PyFPE_END_PROTECT
return newcomplexobject(result);
} }
static object * static object *
@ -389,8 +401,10 @@ complex_div(v, w)
complexobject *w; complexobject *w;
{ {
Py_complex quot; Py_complex quot;
PyFPE_START_PROTECT("complex_div", return 0)
c_error = 0; c_error = 0;
quot = c_quot(v->cval,w->cval); quot = c_quot(v->cval,w->cval);
PyFPE_END_PROTECT
if (c_error == 1) { if (c_error == 1) {
err_setstr(ZeroDivisionError, "complex division"); err_setstr(ZeroDivisionError, "complex division");
return NULL; return NULL;
@ -457,6 +471,7 @@ complex_pow(v, w, z)
return NULL; return NULL;
} }
PyFPE_START_PROTECT("complex_pow", return 0)
c_error = 0; c_error = 0;
exponent = ((complexobject*)w)->cval; exponent = ((complexobject*)w)->cval;
int_exponent = (long)exponent.real; int_exponent = (long)exponent.real;
@ -465,6 +480,7 @@ complex_pow(v, w, z)
else else
p = c_pow(v->cval,exponent); p = c_pow(v->cval,exponent);
PyFPE_END_PROTECT
if (c_error == 2) { if (c_error == 2) {
err_setstr(ValueError, "0.0 to a negative or complex power"); err_setstr(ValueError, "0.0 to a negative or complex power");
return NULL; return NULL;
@ -495,7 +511,11 @@ static object *
complex_abs(v) complex_abs(v)
complexobject *v; complexobject *v;
{ {
return newfloatobject(hypot(v->cval.real,v->cval.imag)); double result;
PyFPE_START_PROTECT("complex_abs", return 0)
result = hypot(v->cval.real,v->cval.imag);
PyFPE_END_PROTECT
return newfloatobject(result);
} }
static int static int

View File

@ -257,7 +257,11 @@ float_add(v, w)
floatobject *v; floatobject *v;
floatobject *w; floatobject *w;
{ {
return newfloatobject(v->ob_fval + w->ob_fval); double result;
PyFPE_START_PROTECT("add", return 0)
result = v->ob_fval + w->ob_fval;
PyFPE_END_PROTECT
return newfloatobject(result);
} }
static object * static object *
@ -265,7 +269,11 @@ float_sub(v, w)
floatobject *v; floatobject *v;
floatobject *w; floatobject *w;
{ {
return newfloatobject(v->ob_fval - w->ob_fval); double result;
PyFPE_START_PROTECT("subtract", return 0)
result = v->ob_fval - w->ob_fval;
PyFPE_END_PROTECT
return newfloatobject(result);
} }
static object * static object *
@ -273,7 +281,12 @@ float_mul(v, w)
floatobject *v; floatobject *v;
floatobject *w; floatobject *w;
{ {
return newfloatobject(v->ob_fval * w->ob_fval); double result;
PyFPE_START_PROTECT("multiply", return 0)
result = v->ob_fval * w->ob_fval;
PyFPE_END_PROTECT
return newfloatobject(result);
} }
static object * static object *
@ -281,11 +294,15 @@ float_div(v, w)
floatobject *v; floatobject *v;
floatobject *w; floatobject *w;
{ {
double result;
if (w->ob_fval == 0) { if (w->ob_fval == 0) {
err_setstr(ZeroDivisionError, "float division"); err_setstr(ZeroDivisionError, "float division");
return NULL; return NULL;
} }
return newfloatobject(v->ob_fval / w->ob_fval); PyFPE_START_PROTECT("divide", return 0)
result = v->ob_fval / w->ob_fval;
PyFPE_END_PROTECT
return newfloatobject(result);
} }
static object * static object *
@ -300,6 +317,7 @@ float_rem(v, w)
err_setstr(ZeroDivisionError, "float modulo"); err_setstr(ZeroDivisionError, "float modulo");
return NULL; return NULL;
} }
PyFPE_START_PROTECT("modulo", return 0)
vx = v->ob_fval; vx = v->ob_fval;
mod = fmod(vx, wx); mod = fmod(vx, wx);
/* div = (vx - mod) / wx; */ /* div = (vx - mod) / wx; */
@ -307,6 +325,7 @@ float_rem(v, w)
mod += wx; mod += wx;
/* div -= 1.0; */ /* div -= 1.0; */
} }
PyFPE_END_PROTECT
return newfloatobject(mod); return newfloatobject(mod);
} }
@ -322,6 +341,7 @@ float_divmod(v, w)
err_setstr(ZeroDivisionError, "float divmod()"); err_setstr(ZeroDivisionError, "float divmod()");
return NULL; return NULL;
} }
PyFPE_START_PROTECT("divmod", return 0)
vx = v->ob_fval; vx = v->ob_fval;
mod = fmod(vx, wx); mod = fmod(vx, wx);
div = (vx - mod) / wx; div = (vx - mod) / wx;
@ -329,6 +349,7 @@ float_divmod(v, w)
mod += wx; mod += wx;
div -= 1.0; div -= 1.0;
} }
PyFPE_END_PROTECT
return mkvalue("(dd)", div, mod); return mkvalue("(dd)", div, mod);
} }
@ -367,18 +388,22 @@ float_pow(v, w, z)
if (iw == intw && -10000 < intw && intw < 10000) { if (iw == intw && -10000 < intw && intw < 10000) {
/* Sort out special cases here instead of relying on pow() */ /* Sort out special cases here instead of relying on pow() */
if (intw == 0) { /* x**0 is 1, even 0**0 */ if (intw == 0) { /* x**0 is 1, even 0**0 */
PyFPE_START_PROTECT("pow", return 0)
if ((object *)z!=None) { if ((object *)z!=None) {
ix=fmod(1.0, z->ob_fval); ix=fmod(1.0, z->ob_fval);
if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval; if (ix!=0 && z->ob_fval<0) ix+=z->ob_fval;
} }
else ix=1.0; else ix=1.0;
PyFPE_END_PROTECT
return newfloatobject(ix); return newfloatobject(ix);
} }
errno = 0; errno = 0;
PyFPE_START_PROTECT("pow", return 0)
if (intw > 0) if (intw > 0)
ix = powu(iv, intw); ix = powu(iv, intw);
else else
ix = 1./powu(iv, -intw); ix = 1./powu(iv, -intw);
PyFPE_END_PROTECT
} }
else { else {
/* Sort out special cases here instead of relying on pow() */ /* Sort out special cases here instead of relying on pow() */
@ -396,7 +421,9 @@ float_pow(v, w, z)
return NULL; return NULL;
} }
errno = 0; errno = 0;
PyFPE_START_PROTECT("pow", return 0)
ix = pow(iv, iw); ix = pow(iv, iw);
PyFPE_END_PROTECT
} }
CHECK(ix); CHECK(ix);
if (errno != 0) { if (errno != 0) {
@ -405,11 +432,13 @@ float_pow(v, w, z)
return NULL; return NULL;
} }
if ((object *)z!=None) { if ((object *)z!=None) {
PyFPE_START_PROTECT("pow", return 0)
ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */ ix=fmod(ix, z->ob_fval); /* XXX To Be Rewritten */
if ( ix!=0 && if ( ix!=0 &&
((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) { ((iv<0 && z->ob_fval>0) || (iv>0 && z->ob_fval<0) )) {
ix+=z->ob_fval; ix+=z->ob_fval;
} }
PyFPE_END_PROTECT
} }
return newfloatobject(ix); return newfloatobject(ix);
} }

View File

@ -237,7 +237,7 @@ PyLong_AsUnsignedLong(vv)
return x; return x;
} }
/* Get a C double from a long int object. No overflow check. */ /* Get a C double from a long int object. */
double double
dgetlongvalue(vv) dgetlongvalue(vv)
@ -1418,7 +1418,11 @@ static object *
long_float(v) long_float(v)
object *v; object *v;
{ {
return newfloatobject(dgetlongvalue(v)); double result;
PyFPE_START_PROTECT("long_float", return 0)
result = dgetlongvalue(v);
PyFPE_END_PROTECT
return newfloatobject(result);
} }
static object * static object *