]> granicus.if.org Git - python/commitdiff
fix the ignoring of __cmp__ method on metaclasses #7491
authorBenjamin Peterson <benjamin@python.org>
Sun, 13 Dec 2009 16:36:53 +0000 (16:36 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sun, 13 Dec 2009 16:36:53 +0000 (16:36 +0000)
Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index 0b21f57ce81a7441ea9365290e24ee9a4e4458d7..87d30eb9fafe105542b863d90428ebfb5eafcd4e 100644 (file)
@@ -1222,6 +1222,15 @@ order (MRO) for bases """
         # This used to crash
         self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
 
+    def test_metaclass_cmp(self):
+        # See bug 7491.
+        class M(type):
+            def __cmp__(self, other):
+                return -1
+        class X(object):
+            __metaclass__ = M
+        self.assertTrue(X < M)
+
     def test_dynamics(self):
         # Testing class attribute propagation...
         class D(object):
index c90b69f75ab89b1239ae9e79ec57c81ca77d422b..76f2377f5ac945b62c06b16b80d1dfccf0234140 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.7 alpha 2?
 Core and Builtins
 -----------------
 
+- Issue #7491: Metaclass's __cmp__ method was ignored.
+
 - Issue #7466: segmentation fault when the garbage collector is called
   in the middle of populating a tuple.  Patch by Florent Xicluna.
 
index 8c49096216d7c7e0e11dba6b701df53087b3e481..8ef23d52e19f547fcf772ab972ea8cd5adc8e0d9 100644 (file)
@@ -628,7 +628,11 @@ type_richcompare(PyObject *v, PyObject *w, int op)
        int c;
 
        /* Make sure both arguments are types. */
-       if (!PyType_Check(v) || !PyType_Check(w)) {
+       if (!PyType_Check(v) || !PyType_Check(w) ||
+           /* If there is a __cmp__ method defined, let it be called instead
+              of our dumb function designed merely to warn.  See bug
+              #7491. */
+           Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) {
                result = Py_NotImplemented;
                goto out;
        }