Ticket #13378: trac_13378-convert_map_shortcut.patch

File trac_13378-convert_map_shortcut.patch, 2.1 KB (added by SimonKing, 10 years ago)

Test validity of input before a (failing) construction of coercion

  • sage/structure/parent.pyx

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1346147394 -7200
    # Node ID 4e230c92400de3407a14ead563bc0c5e72868917
    # Parent  618dd21098bc08985243ad1a6af36db262d63392
    #13378: Do not try to construct a conversion/coercion on invalid input
    
    diff --git a/sage/structure/parent.pyx b/sage/structure/parent.pyx
    a b  
    190190from sage.structure.dynamic_class import dynamic_class
    191191Sets_parent_class = Sets().parent_class
    192192
     193cdef inline bint good_as_coerce_domain(S):
     194    """
     195    Determine whether the input can be the domain of a map.
     196
     197    NOTE:
     198
     199    This is the same as being an object in a category, or
     200    being a type. Namely, in Sage, we do consider coercion maps
     201    from the type ``<int>`` to, say, `ZZ`.
     202
     203    TESTS:
     204
     205    If an instance `S` is not suitable as domain of a map, then
     206    the non-existence of a coercion or conversion map from `S`
     207    to some other parent is not cached, by :trac:`13378`::
     208
     209        sage: P.<x,y> = QQ[]
     210        sage: P.is_coercion_cached(x)
     211        False
     212        sage: P.coerce_map_from(x)
     213        sage: P.is_coercion_cached(x)  # indirect doctest
     214        False
     215
     216    """
     217    return isinstance(S,CategoryObject) or isinstance(S,type)
     218
     219cdef inline bint good_as_convert_domain(S):
     220    return isinstance(S,SageObject) or isinstance(S,type)
     221
    193222cdef class Parent(category_object.CategoryObject):
    194223    """
    195224    Parents are the Sage/mathematical analogues of container objects
     
    19631992                      To:   Macdonald polynomials in the Ht basis over Fraction Field of Multivariate Polynomial Ring in q, t over Rational Field
    19641993
    19651994        """
     1995        if not good_as_coerce_domain(S):
     1996            return None
    19661997        self._coercions_used = True
    19671998        cdef map.Map mor
    19681999        if S is self:
     
    21612192            sage: parent(m(-35/7))
    21622193            Integer Ring
    21632194        """
     2195        if not good_as_convert_domain(S):
     2196            return None
    21642197        if self._convert_from_hash is None: # this is because parent.__init__() does not always get called
    21652198            self.init_coerce()
    21662199        try: