diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index cef7d476e13..e9a50574751 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index 80a82bfbfd8..d9cb038f819 100644 --- 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. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 7e48172fecf..a537b2bee8c 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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; }