Call coerce() in arithmetic operations, to support mixed mode arithmetic

This commit is contained in:
Guido van Rossum 1991-07-01 18:43:13 +00:00
parent c6bb8f7ab2
commit 89d55cad95

View file

@ -1000,23 +1000,36 @@ static object *
add(v, w) add(v, w)
object *v, *w; object *v, *w;
{ {
if (v->ob_type->tp_as_number != NULL) if (v->ob_type->tp_as_number != NULL) {
v = (*v->ob_type->tp_as_number->nb_add)(v, w); object *x;
if (coerce(&v, &w) != 0)
return NULL;
x = (*v->ob_type->tp_as_number->nb_add)(v, w);
DECREF(v);
DECREF(w);
return x;
}
else if (v->ob_type->tp_as_sequence != NULL) else if (v->ob_type->tp_as_sequence != NULL)
v = (*v->ob_type->tp_as_sequence->sq_concat)(v, w); return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
else { else {
err_setstr(TypeError, "+ not supported by operands"); err_setstr(TypeError, "+ not supported by operands");
return NULL; return NULL;
} }
return v;
} }
static object * static object *
sub(v, w) sub(v, w)
object *v, *w; object *v, *w;
{ {
if (v->ob_type->tp_as_number != NULL) if (v->ob_type->tp_as_number != NULL) {
return (*v->ob_type->tp_as_number->nb_subtract)(v, w); object *x;
if (coerce(&v, &w) != 0)
return NULL;
x = (*v->ob_type->tp_as_number->nb_subtract)(v, w);
DECREF(v);
DECREF(w);
return x;
}
err_setstr(TypeError, "bad operand type(s) for -"); err_setstr(TypeError, "bad operand type(s) for -");
return NULL; return NULL;
} }
@ -1033,8 +1046,15 @@ mul(v, w)
w = tmp; w = tmp;
} }
tp = v->ob_type; tp = v->ob_type;
if (tp->tp_as_number != NULL) if (tp->tp_as_number != NULL) {
return (*tp->tp_as_number->nb_multiply)(v, w); object *x;
if (coerce(&v, &w) != 0)
return NULL;
x = (*v->ob_type->tp_as_number->nb_multiply)(v, w);
DECREF(v);
DECREF(w);
return x;
}
if (tp->tp_as_sequence != NULL) { if (tp->tp_as_sequence != NULL) {
if (!is_intobject(w)) { if (!is_intobject(w)) {
err_setstr(TypeError, err_setstr(TypeError,
@ -1052,8 +1072,15 @@ static object *
divide(v, w) divide(v, w)
object *v, *w; object *v, *w;
{ {
if (v->ob_type->tp_as_number != NULL) if (v->ob_type->tp_as_number != NULL) {
return (*v->ob_type->tp_as_number->nb_divide)(v, w); object *x;
if (coerce(&v, &w) != 0)
return NULL;
x = (*v->ob_type->tp_as_number->nb_divide)(v, w);
DECREF(v);
DECREF(w);
return x;
}
err_setstr(TypeError, "bad operand type(s) for /"); err_setstr(TypeError, "bad operand type(s) for /");
return NULL; return NULL;
} }
@ -1062,8 +1089,15 @@ static object *
rem(v, w) rem(v, w)
object *v, *w; object *v, *w;
{ {
if (v->ob_type->tp_as_number != NULL) if (v->ob_type->tp_as_number != NULL) {
return (*v->ob_type->tp_as_number->nb_remainder)(v, w); object *x;
if (coerce(&v, &w) != 0)
return NULL;
x = (*v->ob_type->tp_as_number->nb_remainder)(v, w);
DECREF(v);
DECREF(w);
return x;
}
err_setstr(TypeError, "bad operand type(s) for %"); err_setstr(TypeError, "bad operand type(s) for %");
return NULL; return NULL;
} }