move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
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 @@
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.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 53720ad..f47b408 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -256,13 +256,15 @@
}
static object *
-do_coerce(v, w)
- object *v, *w;
+builtin_coerce(self, args)
+ object *self;
+ object *args;
{
+ object *v, *w;
object *res;
- if (is_instanceobject(v) || is_instanceobject(w))
- return instancebinop(v, w, "__coerce__", "__rcoerce__",
- do_coerce);
+
+ if (!newgetargs(args, "OO:coerce", &v, &w))
+ return NULL;
if (coerce(&v, &w) < 0)
return NULL;
res = mkvalue("(OO)", v, w);
@@ -272,18 +274,6 @@
}
static object *
-builtin_coerce(self, args)
- object *self;
- object *args;
-{
- object *v, *w;
-
- if (!newgetargs(args, "OO:coerce", &v, &w))
- return NULL;
- return do_coerce(v, w);
-}
-
-static object *
builtin_compile(self, args)
object *self;
object *args;
@@ -1464,39 +1454,6 @@
(void) dictinsert(builtin_dict, "None", None);
}
-/* 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;
-}
-
/* Helper for filter(): filter a tuple through a function */