[pypy-commit] pypy stdlib-2.7.8: (alex, arigo) Move the check for cmp(set(), set()) being illegal to cmp() itself.

alex_gaynor noreply at buildbot.pypy.org
Sun Aug 24 19:23:19 CEST 2014


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: stdlib-2.7.8
Changeset: r73025:33fb5b7c1051
Date: 2014-08-24 10:22 -0700
http://bitbucket.org/pypy/pypy/changeset/33fb5b7c1051/

Log:	(alex, arigo) Move the check for cmp(set(), set()) being illegal to
	cmp() itself.

	The problem is that CPython only calls __cmp__ which are written in
	C (tp_compare) when the types are exactly the same, but PyPy treats
	__cmp__ written in RPython the same as one in python.

diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -452,6 +452,11 @@
 
         # The real comparison
         if space.is_w(space.type(w_v), space.type(w_w)):
+            if space.is_w(space.type(w_v), space.w_set):
+                raise OperationError(
+                    space.w_TypeError,
+                    space.wrap("cannot compare sets using cmp()")
+                )
             # for object of the same type, prefer __cmp__ over rich comparison.
             w_cmp = space.lookup(w_v, '__cmp__')
             w_res = _invoke_binop(space, w_cmp, w_v, w_w)
diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -168,11 +168,6 @@
             w_currently_in_repr = ec._py_repr = space.newdict()
         return setrepr(space, w_currently_in_repr, self)
 
-    def descr_cmp(self, space, w_other):
-        # hack hack until we get the expected result
-        raise OperationError(space.w_TypeError,
-                space.wrap('cannot compare sets using cmp()'))
-
     def descr_eq(self, space, w_other):
         if isinstance(w_other, W_BaseSetObject):
             return space.wrap(self.equals(w_other))
@@ -519,7 +514,6 @@
     __init__ = gateway.interp2app(W_BaseSetObject.descr_init),
     __repr__ = gateway.interp2app(W_BaseSetObject.descr_repr),
     __hash__ = None,
-    __cmp__ = gateway.interp2app(W_BaseSetObject.descr_cmp),
 
     # comparison operators
     __eq__ = gateway.interp2app(W_BaseSetObject.descr_eq),
@@ -619,7 +613,6 @@
     __new__ = gateway.interp2app(W_FrozensetObject.descr_new2),
     __repr__ = gateway.interp2app(W_BaseSetObject.descr_repr),
     __hash__ = gateway.interp2app(W_FrozensetObject.descr_hash),
-    __cmp__ = gateway.interp2app(W_BaseSetObject.descr_cmp),
 
     # comparison operators
     __eq__ = gateway.interp2app(W_BaseSetObject.descr_eq),
diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py
--- a/pypy/objspace/std/test/test_setobject.py
+++ b/pypy/objspace/std/test/test_setobject.py
@@ -340,7 +340,7 @@
     def test_compare(self):
         raises(TypeError, cmp, set('abc'), set('abd'))
         assert set('abc') != 'abc'
-        raises(TypeError, "set('abc') < 42")
+        assert not set('abc') < 42
         assert not (set('abc') < set('def'))
         assert not (set('abc') <= frozenset('abd'))
         assert not (set('abc') < frozenset('abd'))


More information about the pypy-commit mailing list