changeset 57692:cb26299e0e52 legacy-trunk

fix the ignoring of __cmp__ method on metaclasses #7491
author Benjamin Peterson <benjamin@python.org>
date Sun, 13 Dec 2009 16:36:53 +0000
parents 16cedde9af00
children 215ddff199c1
files Lib/test/test_descr.py Misc/NEWS Objects/typeobject.c
diffstat 3 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/test/test_descr.py	Sun Dec 13 16:18:14 2009 +0000
+++ b/Lib/test/test_descr.py	Sun Dec 13 16:36:53 2009 +0000
@@ -1222,6 +1222,15 @@
         # 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):
--- a/Misc/NEWS	Sun Dec 13 16:18:14 2009 +0000
+++ b/Misc/NEWS	Sun Dec 13 16:36:53 2009 +0000
@@ -12,6 +12,8 @@
 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.
 
--- a/Objects/typeobject.c	Sun Dec 13 16:18:14 2009 +0000
+++ b/Objects/typeobject.c	Sun Dec 13 16:36:53 2009 +0000
@@ -628,7 +628,11 @@
 	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;
 	}