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

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Dec 12 14:28:47 CET 2007


Author: cfbolz
Date: Wed Dec 12 14:28:46 2007
New Revision: 49675

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:
rich cmp implementation


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:28:46 2007
@@ -197,6 +197,15 @@
         return space.call_function(w_meth)
     return unaryop
 
+def make_richcmp_instance_method(name):
+    def richcmp(self, space, w_other):
+        w_meth = self.getattr(space, space.wrap(name), False)
+        if w_meth is None:
+            return space.w_NotImplemented
+        return space.call_function(w_meth, w_other)
+    return richcmp
+
+
 class W_InstanceObject(Wrappable):
     def __init__(self, space, w_class, w_dict=None):
         if w_dict is None:
@@ -371,6 +380,8 @@
             space.wrap("__nonzero__() should return an int"))
 
 rawdict = {}
+
+# unary operations
 for op in "neg pos abs invert int long float oct hex".split():
     specialname = "__%s__" % (op, )
     # fool the gateway logic by giving it a real unbound method
@@ -382,6 +393,18 @@
         meth,
         unwrap_spec=["self", ObjSpace])
 
+# rich comparison operations
+for op in 'eq ne gt lt ge le'.split():
+    specialname = "__%s__" % (op, )
+    # fool the gateway logic by giving it a real unbound method
+    meth = new.instancemethod(
+        make_richcmp_instance_method(specialname),
+        None,
+        W_InstanceObject)
+    rawdict[specialname] = interp2app(
+        meth,
+        unwrap_spec=["self", ObjSpace, W_Root])
+
 W_InstanceObject.typedef = TypeDef("instance",
     __new__ = interp2app(W_InstanceObject.descr_new),
     __getattribute__ = interp2app(W_InstanceObject.descr_getattribute,

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:28:46 2007
@@ -378,3 +378,14 @@
                  return -1
         a = A()
         assert +a == -1
+
+    def test_cmp(self):
+        class A:
+            __metaclass__ = nclassobj
+            def __lt__(self, other):
+                 return True
+        a = A()
+        b = A()
+        assert a < b
+        assert b < a
+        assert a < 1



More information about the Pypy-commit mailing list