Raise exception instead of dropping imag part for conversion to int,

long, float.

Raise exception instead of dumping core for remainder and divmod.
This commit is contained in:
Guido van Rossum 1996-09-11 22:54:37 +00:00
parent 22a85e5308
commit d4ab3cde8e
1 changed files with 9 additions and 11 deletions

View File

@ -478,29 +478,27 @@ static object *
complex_int(v) complex_int(v)
object *v; object *v;
{ {
double x = ((complexobject *)v)->cval.real; err_setstr(TypeError,
if (x < 0 ? (x = ceil(x)) < (double)LONG_MIN "can't convert complex to int; use e.g. int(abs(z))");
: (x = floor(x)) > (double)LONG_MAX) { return NULL;
err_setstr(OverflowError, "float too large to convert");
return NULL;
}
return newintobject((long)x);
} }
static object * static object *
complex_long(v) complex_long(v)
object *v; object *v;
{ {
double x = ((complexobject *)v)->cval.real; err_setstr(TypeError,
return dnewlongobject(x); "can't convert complex to long; use e.g. long(abs(z))");
return NULL;
} }
static object * static object *
complex_float(v) complex_float(v)
object *v; object *v;
{ {
double x = ((complexobject *)v)->cval.real; err_setstr(TypeError,
return newfloatobject(x); "can't convert complex to float; use e.g. abs(z)");
return NULL;
} }