Ticket #10962: trac_10962-pickle_set_pythontype.2.patch

File trac_10962-pickle_set_pythontype.2.patch, 5.7 KB (added by Vincent Delecroix, 10 years ago)
  • sage/structure/parent.pyx

    # HG changeset patch
    # User Vincent Delecroix <20100.delecroix at gmail.com>
    # Date 1358421692 -3600
    # Node ID 3300c484e7c7b37f00dffd593eeb99364356aaee
    # Parent  da495a3434883ee66bd78156e39c4993200b7daa
    trac 10962: make Set_PythonType pickable
    
    diff --git a/sage/structure/parent.pyx b/sage/structure/parent.pyx
    a b  
    19571957            return Hom(self, self).identity()
    19581958
    19591959        elif isinstance(S, Set_PythonType_class):
    1960             return self.coerce_map_from(S._type)
     1960            return self.coerce_map_from((<Set_PythonType_class> S)._type)
    19611961        if self._coerce_from_hash is None: # this is because parent.__init__() does not always get called
    19621962            self.init_coerce(False)
    19631963        #cdef object ret
     
    25412541
    25422542
    25432543import types
     2544include "../ext/python_type.pxi"
    25442545cdef _type_set_cache = {}
    25452546
    25462547cpdef Parent Set_PythonType(theType):
    25472548    """
    2548     Return the (unique) Parent that represents the set of Python objects 
    2549     of a specified type. 
    2550    
     2549    Return the (unique) Parent that represents the set of Python objects
     2550    of a specified type.
     2551
    25512552    EXAMPLES::
    2552    
     2553
    25532554        sage: from sage.structure.parent import Set_PythonType
    25542555        sage: Set_PythonType(list)
    25552556        Set of Python objects of type 'list'
     
    25632564        sage: S.category()
    25642565        Category of sets
    25652566
    2566     EXAMPLES:
     2567    EXAMPLES::
     2568
    25672569        sage: R = sage.structure.parent.Set_PythonType(int)
    25682570        sage: S = sage.structure.parent.Set_PythonType(float)
    25692571        sage: Hom(R, S)
    25702572        Set of Morphisms from Set of Python objects of type 'int' to Set of Python objects of type 'float' in Category of sets
    2571 
    25722573    """
    25732574    try:
    25742575        return _type_set_cache[theType]
     
    25772578        return theSet
    25782579
    25792580cdef class Set_PythonType_class(Set_generic):
    2580    
    25812581    cdef _type
    25822582
    25832583    def __init__(self, theType):
     
    25872587            sage: S = sage.structure.parent.Set_PythonType(float)
    25882588            sage: S.category()
    25892589            Category of sets
     2590            sage: TestSuite(S).run()
     2591
     2592            sage: sage.structure.parent.Set_PythonType(1)
     2593            Traceback (most recent call last):
     2594            ...
     2595            TypeError: must be initialized with a type, not Integer
    25902596        """
     2597        if not PyType_Check(theType):
     2598            raise TypeError("must be initialized with a type, not %s"%type(theType).__name__)
    25912599        Set_generic.__init__(self, element_constructor=theType, category=Sets())
    25922600        self._type = theType
    25932601
     2602    def __reduce__(self):
     2603        r"""
     2604        Pickling support.
     2605
     2606        TESTS::
     2607
     2608            sage: S = sage.structure.parent.Set_PythonType(object)
     2609            sage: loads(dumps(S))
     2610            Set of Python objects of type 'object'
     2611        """
     2612        return Set_PythonType, (self._type,)
     2613
    25942614    def __call__(self, x):
    25952615        """
    2596         This doesn't return Elements, but actual objects of the given type. 
    2597        
     2616        This doesn't return Elements, but actual objects of the given type.
     2617
    25982618        EXAMPLES::
    2599        
     2619
    26002620            sage: S = sage.structure.parent.Set_PythonType(float)
    26012621            sage: S(5)
    26022622            5.0
     
    26132633    def __hash__(self):
    26142634        """
    26152635        TESTS::
    2616        
     2636
    26172637            sage: S = sage.structure.parent.Set_PythonType(int)
    26182638            sage: hash(S) == -hash(int)
    26192639            True
    26202640        """
    26212641        return -hash(self._type)
    2622        
     2642
    26232643    cpdef int _cmp_(self, other) except -100:
    26242644        """
    2625         Two Python type sets are considered the same if they contain the same 
    2626         type. 
    2627        
     2645        Two Python type sets are considered the same if they contain the same
     2646        type.
     2647
    26282648        EXAMPLES::
    2629        
     2649
    26302650            sage: S = sage.structure.parent.Set_PythonType(int)
    26312651            sage: S == S
    26322652            True
     
    26392659            return cmp(self._type, other._type)
    26402660        else:
    26412661            return cmp(self._type, other)
    2642            
     2662
    26432663    def __contains__(self, x):
    26442664        """
    26452665        Only things of the right type (or subtypes thereof) are considered to
    2646         belong to the set. 
    2647        
     2666        belong to the set.
     2667
    26482668        EXAMPLES::
    2649        
     2669
    26502670            sage: S = sage.structure.parent.Set_PythonType(tuple)
    26512671            sage: (1,2,3) in S
    26522672            True
     
    26602680    def _repr_(self):
    26612681        """
    26622682        EXAMPLES::
    2663        
     2683
    26642684            sage: sage.structure.parent.Set_PythonType(tuple)
    26652685            Set of Python objects of type 'tuple'
    26662686            sage: sage.structure.parent.Set_PythonType(Integer)
     
    26732693    def object(self):
    26742694        """
    26752695        EXAMPLES::
    2676        
     2696
    26772697            sage: S = sage.structure.parent.Set_PythonType(tuple)
    26782698            sage: S.object()
    26792699            <type 'tuple'>
     
    26832703    def cardinality(self):
    26842704        """
    26852705        EXAMPLES::
    2686        
     2706
    26872707            sage: S = sage.structure.parent.Set_PythonType(bool)
    26882708            sage: S.cardinality()
    26892709            2
     
    27112731            # probably
    27122732            import sage.rings.infinity
    27132733            return sage.rings.infinity.infinity
    2714            
     2734
     2735    def is_finite(self):
     2736        r"""
     2737        Return ``True`` if ``self`` is finite and ``False`` if self is infinite.
     2738
     2739        See also :meth:`cardinality`.
     2740
     2741        EXAMPLES::
     2742
     2743            sage: sage.structure.parent.Set_PythonType(int).is_finite()
     2744            True
     2745            sage: sage.structure.parent.Set_PythonType(object).is_finite()
     2746            False
     2747        """
     2748        return self._type is bool or self._type is int or self._type is float
     2749
    27152750#     def _Hom_disabled(self, domain, cat=None):
    27162751#         """
    27172752#         By default, create a homset in the category of sets.