]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
authorGuido van Rossum <guido@python.org>
Tue, 10 Jan 1995 15:26:20 +0000 (15:26 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 10 Jan 1995 15:26:20 +0000 (15:26 +0000)
Objects/object.c
Python/bltinmodule.c

index 424caeb8ab86b6fd52d811119704876eafdc7cbf..c19d96ff371f4a1bffd4e64a460ba76ca5e2b828 100644 (file)
@@ -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.
index 53720ada474cb5d3b62e62f6adcdd3970ae1c087..f47b4085e29e01f3a01283c2f1ada32a0e74f190 100644 (file)
@@ -255,32 +255,22 @@ builtin_cmp(self, args)
        return newintobject((long)cmpobject(a, b));
 }
 
-static object *
-do_coerce(v, w)
-       object *v, *w;
-{
-       object *res;
-       if (is_instanceobject(v) || is_instanceobject(w))
-               return instancebinop(v, w, "__coerce__", "__rcoerce__",
-                                    do_coerce);
-       if (coerce(&v, &w) < 0)
-               return NULL;
-       res = mkvalue("(OO)", v, w);
-       DECREF(v);
-       DECREF(w);
-       return res;
-}
-
 static object *
 builtin_coerce(self, args)
        object *self;
        object *args;
 {
        object *v, *w;
+       object *res;
 
        if (!newgetargs(args, "OO:coerce", &v, &w))
                return NULL;
-       return do_coerce(v, w);
+       if (coerce(&v, &w) < 0)
+               return NULL;
+       res = mkvalue("(OO)", v, w);
+       DECREF(v);
+       DECREF(w);
+       return res;
 }
 
 static object *
@@ -1464,39 +1454,6 @@ initbuiltin()
        (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 */