]> granicus.if.org Git - python/commitdiff
Merged revisions 76794 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Sun, 13 Dec 2009 16:41:44 +0000 (16:41 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sun, 13 Dec 2009 16:41:44 +0000 (16:41 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r76794 | benjamin.peterson | 2009-12-13 10:36:53 -0600 (Sun, 13 Dec 2009) | 2 lines

  fix the ignoring of __cmp__ method on metaclasses #7491
........

Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index cef7d476e13aeea9c8a0d27e4428471c6f7c418f..e9a505747513ce44786e64625b1d2cfd9de16310 100644 (file)
@@ -1198,6 +1198,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 80a82bfbfd8d905f00aeb393dec67a7b1924a06a..d9cb038f819475c9af100650263636ad0e3bbda4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6.5
 Core and Builtins
 -----------------
 
+- Issue #7491: Metaclass's __cmp__ method was ignored.
+
 - Add Py3k warnings for parameter names in parenthesis.
 
 - Issue #7362: Give a proper error message for def f((x)=3): pass.
index 7e48172fecf50f023a58377baceea5465481d9e2..a537b2bee8c5fb195e82345295109e24db21d753 100644 (file)
@@ -645,7 +645,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;
        }