From 866eddd0d1982a0327ac37daf29bab857f227fa5 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Thu, 6 Apr 2017 16:02:35 +0200 Subject: [PATCH] WithEqualityById.__richcmp__: bail out if "other" is no instance of WithEqualityById --- src/sage/misc/fast_methods.pyx | 73 +++++++++++---------- src/sage/structure/unique_representation.py | 8 +-- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/sage/misc/fast_methods.pyx b/src/sage/misc/fast_methods.pyx index 29bacd43f7b..d04b7e602d2 100644 --- a/src/sage/misc/fast_methods.pyx +++ b/src/sage/misc/fast_methods.pyx @@ -18,31 +18,23 @@ AUTHOR: """ -#****************************************************************************** -# Copyright (C) 2013 Simon A. King -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: +#***************************************************************************** +# Copyright (C) 2013 Simon A. King # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -#****************************************************************************** -from __future__ import print_function +#***************************************************************************** + +from __future__ import absolute_import, print_function from sage.misc.classcall_metaclass import ClasscallMetaclass, typecall from sage.misc.constant_function import ConstantFunction -from sage.misc.lazy_attribute import lazy_class_attribute -from cpython.bool cimport * -from cpython.ref cimport * +from cpython.object cimport Py_EQ, Py_NE -cdef extern from "Python.h": - cdef size_t SIZEOF_VOID_P cdef class WithEqualityById: """ @@ -161,7 +153,7 @@ cdef class WithEqualityById: # This is the default hash function in Python's object.c: return hash_by_id(self) - def __richcmp__(self, other, int m): + def __richcmp__(self, other, int op): """ Equality test provided by this class is by identity. @@ -199,24 +191,37 @@ cdef class WithEqualityById: sage: a < b False + When comparing with an object which is not an instance of + ``WithEqualityById``, the other object determines the + comparison:: + + sage: class AlwaysEqual: + ....: def __eq__(self, other): + ....: return True + sage: AlwaysEqual() == a + True + sage: a == AlwaysEqual() + True + + Check that :trac:`19628` is fixed:: + + sage: from sage.misc.lazy_import import LazyImport + sage: lazyQQ = LazyImport('sage.all', 'QQ') + sage: PolynomialRing(lazyQQ, 'ijk') is PolynomialRing(QQ, 'ijk') + True + sage: PolynomialRing(QQ, 'ijkl') is PolynomialRing(lazyQQ, 'ijkl') + True """ - cdef object out - if self is other: - if m == 2: # == - return True - elif m == 3: # != - return False - else: - # <= or >= or NotImplemented - return m==1 or m==5 or NotImplemented - else: - if m == 2: - return False - elif m == 3: - return True - else: + # This only makes sense if "other" is also of type WithEqualityById + if type(self) is not type(other): + if not isinstance(other, WithEqualityById): return NotImplemented + if op == Py_EQ: + return self is other + elif op == Py_NE: + return self is not other + return NotImplemented cdef class FastHashable_class: diff --git a/src/sage/structure/unique_representation.py b/src/sage/structure/unique_representation.py index fc7bb3849b5..bf01e5f299d 100644 --- a/src/sage/structure/unique_representation.py +++ b/src/sage/structure/unique_representation.py @@ -1265,7 +1265,6 @@ class UniqueRepresentation(CachedRepresentation, WithEqualityById): ....: if c: return c ....: print("custom cmp") ....: return cmp(self.value, other.value) - ....: Two coexisting instances of ``MyClass`` created with the same argument data are guaranteed to share the same identity. Since :trac:`12215`, this @@ -1290,10 +1289,8 @@ class UniqueRepresentation(CachedRepresentation, WithEqualityById): sage: x.value, y.value (1, 1) - Rich comparison by identity is used when possible (hence, for ``==``, for - ``!=``, and for identical arguments in the case of ``<``, ``<=``, ``>=`` - and ``>``), which is as fast as it can get. Only if identity is not enough - to decide the answer of a comparison, the custom comparison is called:: + Comparison by identity is used for ``==`` and for ``!=``. For other + operators, the custom comparison is called:: sage: x == y True @@ -1301,6 +1298,7 @@ class UniqueRepresentation(CachedRepresentation, WithEqualityById): sage: x == z, x is z (False, False) sage: x <= x + custom cmp True sage: x != z True