[pypy-svn] r49726 - in pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Dec 13 16:49:09 CET 2007


Author: cfbolz
Date: Thu Dec 13 16:49:09 2007
New Revision: 49726

Modified:
   pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py
   pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py
Log:
implement pow


Modified: pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py
==============================================================================
--- pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py	(original)
+++ pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py	Thu Dec 13 16:49:09 2007
@@ -504,6 +504,47 @@
                 return space.w_True
 
 
+    def descr_pow(self, space, w_other, w_modulo=None):
+        if space.is_w(w_modulo, space.w_None):
+            w_a, w_b = _coerce_helper(space, self, w_other)
+            if w_a is None:
+                w_a = self
+                w_b = w_other
+            if w_a is self:
+                w_func = self.getattr(space, space.wrap('__pow__'), False)
+                if w_func is not None:
+                    return space.call_function(w_func, w_other)
+                return space.w_NotImplemented
+            else:
+                return space.pow(w_a, w_b, space.w_None)
+        else:
+            # CPython also doesn't try coercion in this case
+            w_func = self.getattr(space, space.wrap('__pow__'), False)
+            if w_func is not None:
+                return space.call_function(w_func, w_other, w_modulo)
+            return space.w_NotImplemented
+
+    def descr_rpow(self, space, w_other, w_modulo=None):
+        if space.is_w(w_modulo, space.w_None):
+            w_a, w_b = _coerce_helper(space, self, w_other)
+            if w_a is None:
+                w_a = self
+                w_b = w_other
+            if w_a is self:
+                w_func = self.getattr(space, space.wrap('__rpow__'), False)
+                if w_func is not None:
+                    return space.call_function(w_func, w_other)
+                return space.w_NotImplemented
+            else:
+                return space.pow(w_b, w_a, space.w_None)
+        else:
+            # CPython also doesn't try coercion in this case
+            w_func = self.getattr(space, space.wrap('__rpow__'), False)
+            if w_func is not None:
+                return space.call_function(w_func, w_other, w_modulo)
+            return space.w_NotImplemented
+
+
 rawdict = {}
 
 # unary operations
@@ -579,6 +620,10 @@
                            unwrap_spec=['self', ObjSpace]),
     __contains__ = interp2app(W_InstanceObject.descr_contains,
                          unwrap_spec=['self', ObjSpace, W_Root]),
+    __pow__ = interp2app(W_InstanceObject.descr_pow,
+                         unwrap_spec=['self', ObjSpace, W_Root, W_Root]),
+    __rpow__ = interp2app(W_InstanceObject.descr_rpow,
+                         unwrap_spec=['self', ObjSpace, W_Root, W_Root]),
     **rawdict
 )
 

Modified: pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py
==============================================================================
--- pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py	(original)
+++ pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py	Thu Dec 13 16:49:09 2007
@@ -555,3 +555,27 @@
         a = A()
         for i in range(1, 6):
             assert i in a
+
+    def test_pow(self):
+        class A:
+            __metaclass__ = nclassobj
+            def __pow__(self, other, mod=None):
+                if mod is None:
+                    return 2 ** other
+                return mod ** other
+        a = A()
+        assert a ** 4 == 16
+        assert pow(a, 4) == 16
+        assert pow(a, 4, 5) == 625
+        raises(TypeError, "4 ** a")
+        class A:
+            __metaclass__ = nclassobj
+            def __rpow__(self, other, mod=None):
+                if mod is None:
+                    return 2 ** other
+                return mod ** other
+        a = A()
+        assert 4 ** a == 16
+        assert pow(4, a) == 16
+        raises(TypeError, "a ** 4")
+        assert pow(4, a, 5) == 625



More information about the Pypy-commit mailing list