]> granicus.if.org Git - python/commitdiff
remove some more references to __cmp__ #1717
authorBenjamin Peterson <benjamin@python.org>
Thu, 16 Oct 2008 19:34:46 +0000 (19:34 +0000)
committerBenjamin Peterson <benjamin@python.org>
Thu, 16 Oct 2008 19:34:46 +0000 (19:34 +0000)
Include/object.h
Lib/test/test_class.py
Lib/test/test_descr.py
Lib/test/test_hash.py
Lib/test/test_long.py
Lib/test/test_richcmp.py
Lib/test/test_set.py
Objects/typeobject.c

index bddfb6fe3674b744fae774bd4a9ba0b383b567f4..16708f9f9bfd52a5d7be6ca498682e432edd0f86 100644 (file)
@@ -447,9 +447,6 @@ PyAPI_FUNC(int) PyCallable_Check(PyObject *);
 
 PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
 
-/* A slot function whose address we need to compare */
-extern int _PyObject_SlotCompare(PyObject *, PyObject *);
-
 
 /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
    list of strings.  PyObject_Dir(NULL) is like builtins.dir(),
index cabf715dcbdcf9c86c7864bc9ad1ba8e5da0389c..552c521cdfa2905c46ef1ee9868600f1ca389b9c 100644 (file)
@@ -91,10 +91,6 @@ def __index__(self, *args):
 def __float__(self, *args):
     return 1.0
 
-@trackCall
-def __cmp__(self, *args):
-    return 0
-
 @trackCall
 def __eq__(self, *args):
     return True
@@ -465,11 +461,6 @@ class ClassTests(unittest.TestCase):
 
         hash(C0()) # This should work; the next two should raise TypeError
 
-        class C1:
-            def __cmp__(self, other): return 0
-
-        self.assertRaises(TypeError, hash, C1())
-
         class C2:
             def __eq__(self, other): return 1
 
index 3c0bae0ccc73cf529ad8e3b85e817324a67f9583..85df9dca1e2696985d1b8b1b89590880ef8823ed 100644 (file)
@@ -1566,7 +1566,7 @@ order (MRO) for bases """
         for i in range(10):
             self.assert_(i in d1)
         self.assertFalse(10 in d1)
-        # Test overridden behavior for static classes
+        # Test overridden behavior
         class Proxy(object):
             def __init__(self, x):
                 self.x = x
@@ -1578,8 +1578,14 @@ order (MRO) for bases """
                 return self.x == other
             def __ne__(self, other):
                 return self.x != other
-            def __cmp__(self, other):
-                return cmp(self.x, other.x)
+            def __ge__(self, other):
+                return self.x >= other
+            def __gt__(self, other):
+                return self.x > other
+            def __le__(self, other):
+                return self.x <= other
+            def __lt__(self, other):
+                return self.x < other
             def __str__(self):
                 return "Proxy:%s" % self.x
             def __repr__(self):
@@ -1596,9 +1602,10 @@ order (MRO) for bases """
         self.assertNotEqual(p0, p1)
         self.assert_(not p0 != p0)
         self.assertEqual(not p0, p1)
-        self.assertEqual(cmp(p0, p1), -1)
-        self.assertEqual(cmp(p0, p0), 0)
-        self.assertEqual(cmp(p0, p_1), 1)
+        self.assert_(p0 < p1)
+        self.assert_(p0 <= p1)
+        self.assert_(p1 > p0)
+        self.assert_(p1 >= p0)
         self.assertEqual(str(p0), "Proxy:0")
         self.assertEqual(repr(p0), "Proxy(0)")
         p10 = Proxy(range(10))
@@ -1606,46 +1613,6 @@ order (MRO) for bases """
         for i in range(10):
             self.assert_(i in p10)
         self.assertFalse(10 in p10)
-        # Test overridden behavior for dynamic classes
-        class DProxy(object):
-            def __init__(self, x):
-                self.x = x
-            def __bool__(self):
-                return not not self.x
-            def __hash__(self):
-                return hash(self.x)
-            def __eq__(self, other):
-                return self.x == other
-            def __ne__(self, other):
-                return self.x != other
-            def __cmp__(self, other):
-                return cmp(self.x, other.x)
-            def __str__(self):
-                return "DProxy:%s" % self.x
-            def __repr__(self):
-                return "DProxy(%r)" % self.x
-            def __contains__(self, value):
-                return value in self.x
-        p0 = DProxy(0)
-        p1 = DProxy(1)
-        p_1 = DProxy(-1)
-        self.assertFalse(p0)
-        self.assert_(not not p1)
-        self.assertEqual(hash(p0), hash(0))
-        self.assertEqual(p0, p0)
-        self.assertNotEqual(p0, p1)
-        self.assertNotEqual(not p0, p0)
-        self.assertEqual(not p0, p1)
-        self.assertEqual(cmp(p0, p1), -1)
-        self.assertEqual(cmp(p0, p0), 0)
-        self.assertEqual(cmp(p0, p_1), 1)
-        self.assertEqual(str(p0), "DProxy:0")
-        self.assertEqual(repr(p0), "DProxy(0)")
-        p10 = DProxy(range(10))
-        self.assertFalse(-1 in p10)
-        for i in range(10):
-            self.assert_(i in p10)
-        self.assertFalse(10 in p10)
 
         ## # Safety test for __cmp__
         ## def unsafecmp(a, b):
index 56974b8cc3b236365edc80b051ab82a2dc701c19..59e43dc8d07e1e596b28e8274009c7e21a56ddb6 100644 (file)
@@ -78,7 +78,6 @@ class HashInheritanceTestCase(unittest.TestCase):
                       ]
     error_expected = [NoHash(),
                       OnlyEquality(),
-                      OnlyCmp(),
                       ]
 
     def test_default_hash(self):
index dc04badf20a5de071beb99fe29929fbc982bb9a3..a1db26ac8036fbd645a0d5274c48aa6116de03c0 100644 (file)
@@ -669,10 +669,22 @@ class LongTest(unittest.TestCase):
                 else:
                     raise TypeError("can't deal with %r" % val)
 
-            def __cmp__(self, other):
+            def _cmp__(self, other):
                 if not isinstance(other, Rat):
                     other = Rat(other)
                 return cmp(self.n * other.d, self.d * other.n)
+            def __eq__(self, other):
+                return self._cmp__(other) == 0
+            def __ne__(self, other):
+                return self._cmp__(other) != 0
+            def __ge__(self, other):
+                return self._cmp__(other) >= 0
+            def __gt__(self, other):
+                return self._cmp__(other) > 0
+            def __le__(self, other):
+                return self._cmp__(other) <= 0
+            def __lt__(self, other):
+                return self._cmp__(other) < 0
 
         cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200]
         # 2**48 is an important boundary in the internals.  2**53 is an
index 435de75a84887139a80b08d652411f93e00cbeb4..72e636d87d362235672b60a1e859544fe0ba46f8 100644 (file)
@@ -198,13 +198,11 @@ class MiscTest(unittest.TestCase):
             def __le__(self, other): raise TestFailed("This shouldn't happen")
             def __ge__(self, other): raise TestFailed("This shouldn't happen")
             def __ne__(self, other): raise TestFailed("This shouldn't happen")
-            def __cmp__(self, other): raise RuntimeError("expected")
         a = Misb()
         b = Misb()
         self.assertEqual(a<b, 0)
         self.assertEqual(a==b, 0)
         self.assertEqual(a>b, 0)
-        self.assertRaises(RuntimeError, cmp, a, b)
 
     def test_not(self):
         # Check that exceptions in __bool__ are properly
index 0effd65c83cc589a089225ce44a152f6bcf394a1..614c9c09b0e1614c942729b2ec65ca35891fd5c1 100644 (file)
@@ -202,9 +202,6 @@ class TestJointOps(unittest.TestCase):
         s = self.thetype(t)
         self.assertEqual(len(s), 3)
 
-    def test_compare(self):
-        self.assertRaises(TypeError, self.s.__cmp__, self.s)
-
     def test_sub_and_super(self):
         p, q, r = map(self.thetype, ['ab', 'abcde', 'def'])
         self.assert_(p < q)
index 8f1b3892ac588d163cdae6da575884aca388e23a..7f9551fa4c8e0a7f34491fec942053295109c42b 100644 (file)
@@ -3536,7 +3536,6 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
 
 static char *hash_name_op[] = {
        "__eq__",
-       "__cmp__",
        "__hash__",
        NULL
 };
@@ -4227,32 +4226,6 @@ wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
        return Py_None;
 }
 
-static PyObject *
-wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
-{
-       cmpfunc func = (cmpfunc)wrapped;
-       int res;
-       PyObject *other;
-
-       if (!check_num_args(args, 1))
-               return NULL;
-       other = PyTuple_GET_ITEM(args, 0);
-       if (Py_TYPE(other)->tp_compare != func &&
-           !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
-               PyErr_Format(
-                       PyExc_TypeError,
-                       "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
-                       Py_TYPE(self)->tp_name,
-                       Py_TYPE(self)->tp_name,
-                       Py_TYPE(other)->tp_name);
-               return NULL;
-       }
-       res = (*func)(self, other);
-       if (PyErr_Occurred())
-               return NULL;
-       return PyLong_FromLong((long)res);
-}
-
 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
    This is called the Carlo Verre hack after its discoverer. */
 static int
@@ -4878,62 +4851,6 @@ SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
 
-static int
-half_compare(PyObject *self, PyObject *other)
-{
-       PyObject *func, *args, *res;
-       static PyObject *cmp_str;
-       Py_ssize_t c;
-
-       func = lookup_method(self, "__cmp__", &cmp_str);
-       if (func == NULL) {
-               PyErr_Clear();
-       }
-       else {
-               args = PyTuple_Pack(1, other);
-               if (args == NULL)
-                       res = NULL;
-               else {
-                       res = PyObject_Call(func, args, NULL);
-                       Py_DECREF(args);
-               }
-               Py_DECREF(func);
-               if (res != Py_NotImplemented) {
-                       if (res == NULL)
-                               return -2;
-                       c = PyLong_AsLong(res);
-                       Py_DECREF(res);
-                       if (c == -1 && PyErr_Occurred())
-                               return -2;
-                       return (c < 0) ? -1 : (c > 0) ? 1 : 0;
-               }
-               Py_DECREF(res);
-       }
-       return 2;
-}
-
-/* This slot is published for the benefit of try_3way_compare in object.c */
-int
-_PyObject_SlotCompare(PyObject *self, PyObject *other)
-{
-       int c;
-
-       if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {
-               c = half_compare(self, other);
-               if (c <= 1)
-                       return c;
-       }
-       if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {
-               c = half_compare(other, self);
-               if (c < -1)
-                       return -2;
-               if (c <= 1)
-                       return -c;
-       }
-       return (void *)self < (void *)other ? -1 :
-               (void *)self > (void *)other ? 1 : 0;
-}
-
 static PyObject *
 slot_tp_repr(PyObject *self)
 {
@@ -5532,8 +5449,6 @@ static slotdef slotdefs[] = {
               "x.__str__() <==> str(x)"),
        TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
               "x.__repr__() <==> repr(x)"),
-       TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
-              "x.__cmp__(y) <==> cmp(x,y)"),
        TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
               "x.__hash__() <==> hash(x)"),
        FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,