summaryrefslogtreecommitdiffstats
path: root/Python/ceval.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-07-01 18:43:13 (GMT)
committerGuido van Rossum <guido@python.org>1991-07-01 18:43:13 (GMT)
commit89d55cad95697c6807e8dee1642564478f38fdd6 (patch)
tree543da7f1509f9a9e5d720159aba26c9139524d35 /Python/ceval.c
parentc6bb8f7ab290d50934c761b25beb82918f23cf89 (diff)
downloadcpython-89d55cad95697c6807e8dee1642564478f38fdd6.zip
cpython-89d55cad95697c6807e8dee1642564478f38fdd6.tar.gz
cpython-89d55cad95697c6807e8dee1642564478f38fdd6.tar.bz2
Call coerce() in arithmetic operations, to support mixed mode arithmetic
Diffstat (limited to 'Python/ceval.c')
-rw-r--r--Python/ceval.c58
1 files changed, 46 insertions, 12 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 975b788..4f81bff 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1000,23 +1000,36 @@ static object *
add(v, w)
object *v, *w;
{
- if (v->ob_type->tp_as_number != NULL)
- v = (*v->ob_type->tp_as_number->nb_add)(v, w);
+ if (v->ob_type->tp_as_number != NULL) {
+ 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)
- v = (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
+ return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
else {
err_setstr(TypeError, "+ not supported by operands");
return NULL;
}
- return v;
}
static object *
sub(v, w)
object *v, *w;
{
- if (v->ob_type->tp_as_number != NULL)
- return (*v->ob_type->tp_as_number->nb_subtract)(v, w);
+ if (v->ob_type->tp_as_number != NULL) {
+ 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 -");
return NULL;
}
@@ -1033,8 +1046,15 @@ mul(v, w)
w = tmp;
}
tp = v->ob_type;
- if (tp->tp_as_number != NULL)
- return (*tp->tp_as_number->nb_multiply)(v, w);
+ if (tp->tp_as_number != NULL) {
+ 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 (!is_intobject(w)) {
err_setstr(TypeError,
@@ -1052,8 +1072,15 @@ static object *
divide(v, w)
object *v, *w;
{
- if (v->ob_type->tp_as_number != NULL)
- return (*v->ob_type->tp_as_number->nb_divide)(v, w);
+ if (v->ob_type->tp_as_number != NULL) {
+ 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 /");
return NULL;
}
@@ -1062,8 +1089,15 @@ static object *
rem(v, w)
object *v, *w;
{
- if (v->ob_type->tp_as_number != NULL)
- return (*v->ob_type->tp_as_number->nb_remainder)(v, w);
+ if (v->ob_type->tp_as_number != NULL) {
+ 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 %");
return NULL;
}