[pypy-commit] pypy py3k: kill the old __cmp__ for Cells, and implement rich comparison instead

antocuni noreply at buildbot.pypy.org
Thu Mar 1 17:21:07 CET 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r53064:d395491bc35d
Date: 2012-03-01 17:06 +0100
http://bitbucket.org/pypy/pypy/changeset/d395491bc35d/

Log:	kill the old __cmp__ for Cells, and implement rich comparison
	instead

diff --git a/pypy/interpreter/nestedscope.py b/pypy/interpreter/nestedscope.py
--- a/pypy/interpreter/nestedscope.py
+++ b/pypy/interpreter/nestedscope.py
@@ -30,21 +30,27 @@
         if self.w_value is None:
             raise ValueError, "delete() on an empty cell"
         self.w_value = None
-  
-    def descr__cmp__(self, space, w_other):
-        # XXX fix me, cmp is gone
+
+    def descr__lt__(self, space, w_other):
         other = space.interpclass_w(w_other)
         if not isinstance(other, Cell):
             return space.w_NotImplemented
+        if self.w_value is None:
+            # an empty cell is alway less than a non-empty one
+            if other.w_value is None:
+                return space.w_False
+            return space.w_True
+        elif other.w_value is None:
+            return space.w_False
+        return space.lt(self.w_value, other.w_value)
 
-        if self.w_value is None:
-            if other.w_value is None:
-                return space.newint(0)
-            return space.newint(-1)
-        elif other.w_value is None:
-            return space.newint(1)
-
-        return space.cmp(self.w_value, other.w_value)
+    def descr__eq__(self, space, w_other):
+        other = space.interpclass_w(w_other)
+        if not isinstance(other, Cell):
+            return space.w_NotImplemented
+        if self.w_value is None or other.w_value is None:
+            return space.wrap(self.w_value == other.w_value)
+        return space.eq(self.w_value, other.w_value)
 
     def descr__reduce__(self, space):
         w_mod    = space.getbuiltinmodule('_pickle_support')
diff --git a/pypy/interpreter/test/test_nestedscope.py b/pypy/interpreter/test/test_nestedscope.py
--- a/pypy/interpreter/test/test_nestedscope.py
+++ b/pypy/interpreter/test/test_nestedscope.py
@@ -82,14 +82,25 @@
     def test_compare_cells(self):
         def f(n):
             if n:
-                x = 42
+                x = n
             def f(y):
                   return x + y
             return f
 
-        g0 = f(0).__closure__[0]
+        empty_cell_1 = f(0).__closure__[0]
+        empty_cell_2 = f(0).__closure__[0]
         g1 = f(1).__closure__[0]
-        assert cmp(g0, g1) == -1
+        g2 = f(2).__closure__[0]
+        assert g1 < g2
+        assert g1 <= g2
+        assert g2 > g1
+        assert g2 >= g1
+        assert not g1 == g2
+        assert g1 != g2
+        #
+        assert empty_cell_1 == empty_cell_2
+        assert not empty_cell_1 != empty_cell_2
+        assert empty_cell_1 < g1
 
     def test_leaking_class_locals(self):
         def f(x):
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -919,6 +919,9 @@
 GeneratorIterator.typedef.acceptable_as_base_class = False
 
 Cell.typedef = TypeDef("cell",
+    __total_ordering__ = 'auto',
+    __lt__       = interp2app(Cell.descr__lt__),
+    __eq__       = interp2app(Cell.descr__eq__),
     __hash__     = None,
     __reduce__   = interp2app(Cell.descr__reduce__),
     __setstate__ = interp2app(Cell.descr__setstate__),


More information about the pypy-commit mailing list