Author: Armin Rigo <ar...@tunes.org> Branch: release-2.5.x Changeset: r76420:5aed0a41060e Date: 2015-03-16 19:17 +0100 http://bitbucket.org/pypy/pypy/changeset/5aed0a41060e/
Log: Test and fix: a type's __eq__ or __ne__ methods must return NotImplemented instead of False when given a non-type diff --git a/pypy/objspace/std/test/test_typeobject.py b/pypy/objspace/std/test/test_typeobject.py --- a/pypy/objspace/std/test/test_typeobject.py +++ b/pypy/objspace/std/test/test_typeobject.py @@ -1165,3 +1165,17 @@ return x + 1 a = A() assert a.f(1) == 2 + + def test_eq_returns_notimplemented(self): + assert type.__eq__(int, 42) is NotImplemented + assert type.__ne__(dict, 42) is NotImplemented + assert type.__eq__(int, int) is True + assert type.__eq__(int, dict) is False + + def test_cmp_on_types(self): + class X(type): + def __cmp__(self, other): + return -1 + class Y: + __metaclass__ = X + assert (Y < Y) is True diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py --- a/pypy/objspace/std/typeobject.py +++ b/pypy/objspace/std/typeobject.py @@ -645,9 +645,13 @@ "type object '%N' has no attribute %R", self, w_name) def descr_eq(self, space, w_other): + if not isinstance(w_other, W_TypeObject): + return space.w_NotImplemented return space.is_(self, w_other) def descr_ne(self, space, w_other): + if not isinstance(w_other, W_TypeObject): + return space.w_NotImplemented return space.newbool(not space.is_w(self, w_other)) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit