diff Python/bltinmodule.c @ 1914:893412101296 legacy-trunk

* Python/ceval.c, Include/ceval.h: promote MakePendingCalls to global: Py_MakePendingCalls. Also guard against recursive calls * Include/classobject.h, Objects/classobject.c, Python/{ceval.c,bltinmodule.c}: entirely redone operator overloading. The rules for class instances are now much more relaxed than for other built-in types (whose coerce must still return two objects of the same type)
author Guido van Rossum <guido@python.org>
date Thu, 29 Sep 1994 09:45:57 +0000
parents 1822465a35b8
children e8952b8dfeff
line wrap: on
line diff
--- a/Python/bltinmodule.c	Thu Sep 29 09:42:55 1994 +0000
+++ b/Python/bltinmodule.c	Thu Sep 29 09:45:57 1994 +0000
@@ -243,6 +243,8 @@
 
 	if (!getargs(args, "(OO)", &v, &w))
 		return NULL;
+	if (is_instanceobject(v) || is_instanceobject(w))
+		return instancebinop(v, w, "__coerce__", "__rcoerce__");
 	if (coerce(&v, &w) < 0)
 		return NULL;
 	res = mkvalue("(OO)", v, w);
@@ -314,9 +316,11 @@
 	object *v, *w, *x;
 	if (!getargs(args, "(OO)", &v, &w))
 		return NULL;
+	if (is_instanceobject(v) || is_instanceobject(w))
+		return instancebinop(v, w, "__divmod__", "__rdivmod__");
 	if (v->ob_type->tp_as_number == NULL ||
 				w->ob_type->tp_as_number == NULL) {
-		err_setstr(TypeError, "divmod() requires numeric arguments");
+		err_setstr(TypeError, "divmod() requires numeric or class instance arguments");
 		return NULL;
 	}
 	if (coerce(&v, &w) != 0)
@@ -876,11 +880,16 @@
 {
 	object *v, *w, *z, *x;
  	z = None;
-	if (!getargs(args, "(OO)", &v, &w)) {
-		 err_clear();
-		 if (!getargs(args, "(OOO)", &v, &w, &z)) {
-			return NULL;
-		}
+	if (!newgetargs(args, "OO|O", &v, &w, &z))
+		return NULL;
+	if (z == None) {
+		if (is_instanceobject(v) || is_instanceobject(w))
+			return instancebinop(v, w, "__pow__", "__rpow__");
+	}
+	else {
+		/* XXX The ternary version doesn't do coercions */
+		if (is_instanceobject(v))
+			return v->ob_type->tp_as_number->nb_power(v, w, z);
 	}
 	if (v->ob_type->tp_as_number == NULL ||
 	    (z!=None && z->ob_type->tp_as_number == NULL) ||