[pypy-svn] r12301 - in pypy/dist/pypy/objspace: . test

arigo at codespeak.net arigo at codespeak.net
Sun May 15 17:57:44 CEST 2005


Author: arigo
Date: Sun May 15 17:57:44 2005
New Revision: 12301

Modified:
   pypy/dist/pypy/objspace/descroperation.py
   pypy/dist/pypy/objspace/test/test_descroperation.py
Log:
Oups.  DescrOperation was using wrong rules for which binary operators to call
in the presence of subclassing.  Wasn't tested...


Modified: pypy/dist/pypy/objspace/descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/descroperation.py	(original)
+++ pypy/dist/pypy/objspace/descroperation.py	Sun May 15 17:57:44 2005
@@ -223,7 +223,7 @@
             w_right_impl = None
         else:
             w_right_impl = space.lookup(w_obj2, '__rpow__')
-            if space.is_true(space.issubtype(w_typ1, w_typ2)):
+            if space.is_true(space.issubtype(w_typ2, w_typ1)):
                 w_obj1, w_obj2 = w_obj2, w_obj1
                 w_left_impl, w_right_impl = w_right_impl, w_left_impl
         if w_left_impl is not None:
@@ -358,7 +358,7 @@
             w_right_impl = None
         else:
             w_right_impl = space.lookup(w_obj2, '__coerce__')
-            if space.is_true(space.issubtype(w_typ1, w_typ2)):
+            if space.is_true(space.issubtype(w_typ2, w_typ1)):
                 w_obj1, w_obj2 = w_obj2, w_obj1
                 w_left_impl, w_right_impl = w_right_impl, w_left_impl
 
@@ -415,7 +415,7 @@
         w_right_impl = None
     else:
         w_right_impl = space.lookup(w_obj2, '__cmp__')
-        if space.is_true(space.issubtype(w_typ1, w_typ2)):
+        if space.is_true(space.issubtype(w_typ2, w_typ1)):
             w_obj1, w_obj2 = w_obj2, w_obj1
             w_left_impl, w_right_impl = w_right_impl, w_left_impl
             do_neg1, do_neg2 = do_neg2, do_neg1
@@ -458,7 +458,7 @@
             w_right_impl = None
         else:
             w_right_impl = space.lookup(w_obj2, right)
-            if space.is_true(space.issubtype(w_typ1, w_typ2)):
+            if space.is_true(space.issubtype(w_typ2, w_typ1)):
                 w_obj1, w_obj2 = w_obj2, w_obj1
                 w_left_impl, w_right_impl = w_right_impl, w_left_impl
 
@@ -519,7 +519,7 @@
                 w_right_impl = None
             else:
                 w_right_impl = space.lookup(w_obj2, right)
-                if space.is_true(space.issubtype(w_typ1, w_typ2)):
+                if space.is_true(space.issubtype(w_typ2, w_typ1)):
                     w_obj1, w_obj2 = w_obj2, w_obj1
                     w_left_impl, w_right_impl = w_right_impl, w_left_impl
 

Modified: pypy/dist/pypy/objspace/test/test_descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/test/test_descroperation.py	(original)
+++ pypy/dist/pypy/objspace/test/test_descroperation.py	Sun May 15 17:57:44 2005
@@ -16,6 +16,67 @@
 
 class AppTest_Descroperation:
 
+    def test_special_methods(self):
+        class A(object):
+            def __lt__(self, other):
+                return "lt"
+            def __imul__(self, other):
+                return "imul"
+            def __sub__(self, other):
+                return "sub"
+            def __rsub__(self, other):
+                return "rsub"
+            def __pow__(self, other):
+                return "pow"
+            def __rpow__(self, other):
+                return "rpow"
+            def __neg__(self):
+                return "neg"
+        a = A()
+        assert (a < 5) == "lt"
+        assert (object() > a) == "lt"
+        a1 = a
+        a1 *= 4
+        assert a1 == "imul"
+        assert a - 2 == "sub"
+        assert object() - a == "rsub"
+        assert a ** 2 == "pow"
+        assert object() ** a == "rpow"
+        assert -a == "neg"
+
+        class B(A):
+            def __lt__(self, other):
+                return "B's lt"
+            def __imul__(self, other):
+                return "B's imul"
+            def __sub__(self, other):
+                return "B's sub"
+            def __rsub__(self, other):
+                return "B's rsub"
+            def __pow__(self, other):
+                return "B's pow"
+            def __rpow__(self, other):
+                return "B's rpow"
+            def __neg__(self):
+                return "B's neg"
+
+        b = B()
+        assert (a < b) == "lt"
+        assert (b > a) == "lt"
+        b1 = b
+        b1 *= a
+        assert b1 == "B's imul"
+        a1 = a
+        a1 *= b
+        assert a1 == "imul"
+        assert a - b == "B's rsub"
+        assert b - a == "B's sub"
+        assert b - b == "B's sub"
+        assert a ** b == "B's rpow"
+        assert b ** a == "B's pow"
+        assert b ** b == "B's pow"
+        assert -b == "B's neg"
+
     def test_getslice(self):
         class Sq(object):
             def __getslice__(self, start, stop):



More information about the Pypy-commit mailing list