summaryrefslogtreecommitdiffstats
path: root/Objects/object.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-01-10 15:26:20 (GMT)
committerGuido van Rossum <guido@python.org>1995-01-10 15:26:20 (GMT)
commit5524a59b0930638413ab44b150a3e66818a34cf9 (patch)
treec33deeb26fb8eed084b3906faebf83ebe1a0bd7e /Objects/object.c
parent879c581826ec9c0356dbf137cface7a27f51810c (diff)
downloadcpython-5524a59b0930638413ab44b150a3e66818a34cf9.zip
cpython-5524a59b0930638413ab44b150a3e66818a34cf9.tar.gz
cpython-5524a59b0930638413ab44b150a3e66818a34cf9.tar.bz2
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Diffstat (limited to 'Objects/object.c')
-rw-r--r--Objects/object.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 424caeb..c19d96f 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -319,6 +319,39 @@ testbool(v)
return res;
}
+/* Coerce two numeric types to the "larger" one.
+ Increment the reference count on each argument.
+ Return -1 and raise an exception if no coercion is possible
+ (and then no reference count is incremented).
+*/
+
+int
+coerce(pv, pw)
+ object **pv, **pw;
+{
+ register object *v = *pv;
+ register object *w = *pw;
+ int res;
+
+ if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
+ INCREF(v);
+ INCREF(w);
+ return 0;
+ }
+ if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
+ res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
+ if (res <= 0)
+ return res;
+ }
+ if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
+ res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
+ if (res <= 0)
+ return res;
+ }
+ err_setstr(TypeError, "number coercion failed");
+ return -1;
+}
+
/*
NoObject is usable as a non-NULL undefined value, used by the macro None.