[pypy-svn] pypy default: Fix a crash in cell objects comparison

amauryfa commits-noreply at bitbucket.org
Thu Jan 27 14:56:58 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41391:68d9990f6747
Date: 2011-01-27 13:52 +0100
http://bitbucket.org/pypy/pypy/changeset/68d9990f6747/

Log:	Fix a crash in cell objects comparison

diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -897,9 +897,8 @@
 GeneratorIterator.typedef.acceptable_as_base_class = False
 
 Cell.typedef = TypeDef("cell",
-    __eq__       = interp2app(Cell.descr__eq__,
+    __cmp__      = interp2app(Cell.descr__cmp__,
                               unwrap_spec=['self', ObjSpace, W_Root]),
-    __ne__       = descr_generic_ne,
     __hash__     = None,
     __reduce__   = interp2app(Cell.descr__reduce__,
                               unwrap_spec=['self', ObjSpace]),

diff --git a/pypy/interpreter/nestedscope.py b/pypy/interpreter/nestedscope.py
--- a/pypy/interpreter/nestedscope.py
+++ b/pypy/interpreter/nestedscope.py
@@ -31,12 +31,20 @@
             raise ValueError, "delete() on an empty cell"
         self.w_value = None
   
-    def descr__eq__(self, space, w_other):
+    def descr__cmp__(self, space, w_other):
         other = space.interpclass_w(w_other)
         if not isinstance(other, Cell):
-            return space.w_False
-        return space.eq(self.w_value, other.w_value)    
-        
+            return space.w_NotImplemented
+
+        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__reduce__(self, space):
         w_mod    = space.getbuiltinmodule('_pickle_support')
         mod      = space.interp_w(MixedModule, w_mod)

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
@@ -80,6 +80,18 @@
         g = f()
         raises(ValueError, "g.func_closure[0].cell_contents")
 
+    def test_compare_cells(self):
+        def f(n):
+            if n:
+                x = 42
+            def f(y):
+                  return x + y
+            return f
+
+        g0 = f(0).func_closure[0]
+        g1 = f(1).func_closure[0]
+        assert cmp(g0, g1) == -1
+
     def test_leaking_class_locals(self):
         def f(x):
             class X:


More information about the Pypy-commit mailing list