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

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Dec 12 14:40:03 CET 2007


Author: cfbolz
Date: Wed Dec 12 14:40:02 2007
New Revision: 49676

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 coerce


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	Wed Dec 12 14:40:02 2007
@@ -197,7 +197,7 @@
         return space.call_function(w_meth)
     return unaryop
 
-def make_richcmp_instance_method(name):
+def make_binary_returning_notimplemented_instance_method(name):
     def richcmp(self, space, w_other):
         w_meth = self.getattr(space, space.wrap(name), False)
         if w_meth is None:
@@ -393,12 +393,13 @@
         meth,
         unwrap_spec=["self", ObjSpace])
 
-# rich comparison operations
-for op in 'eq ne gt lt ge le'.split():
+# binary operations that return NotImplemented if they fail
+# e.g. rich comparisons and coerce
+for op in 'eq ne gt lt ge le coerce'.split():
     specialname = "__%s__" % (op, )
     # fool the gateway logic by giving it a real unbound method
     meth = new.instancemethod(
-        make_richcmp_instance_method(specialname),
+        make_binary_returning_notimplemented_instance_method(specialname),
         None,
         W_InstanceObject)
     rawdict[specialname] = interp2app(

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	Wed Dec 12 14:40:02 2007
@@ -389,3 +389,14 @@
         assert a < b
         assert b < a
         assert a < 1
+
+    def test_coerce(self):
+        class B:
+            __metaclass__ = nclassobj
+            def __coerce__(self, other):
+                return other, self
+        b = B()
+        assert coerce(b, 1) == (1, b)
+        class B:
+            __metaclass__ = nclassobj
+        raises(TypeError, coerce, B(), [])



More information about the Pypy-commit mailing list