Ticket #10470: trac_10470-listing-infinite-sets.patch

File trac_10470-listing-infinite-sets.patch, 3.0 KB (added by rbeezer, 10 years ago)
  • sage/structure/parent.pyx

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1294849352 28800
    # Node ID 843063afa39bdc0c9e0e7c30b5704b4ce88c31a1
    # Parent  9f650a053251fc954d2e6e26843c8e9a0795ead5
    10470: prevent listing known infinite sets
    
    diff -r 9f650a053251 -r 843063afa39b sage/structure/parent.pyx
    a b  
    990990            return (<map.Map>mor)._call_(x)
    991991   
    992992    def _list_from_iterator_cached(self):
    993         """
    994         Return a list of all elements in this object, if possible (the
    995         object must define an iterator).
     993        r"""
     994        Return a list of the elements of ``self``.
     995       
     996        OUTPUT:
     997       
     998        A list of all the elements produced by the iterator
     999        defined for the object.  The result is cached. An
     1000        infinite set may define an iterator, allowing one
     1001        to search through the elements, but a request by
     1002        this method for the entire list should fail.
     1003       
     1004        EXAMPLES::
     1005       
     1006            sage: R = Integers(11)
     1007            sage: R.list()    # indirect doctest
     1008            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     1009
     1010            sage: ZZ.list()
     1011            Traceback (most recent call last):
     1012            ...
     1013            ValueError: since it is infinite, cannot list Integer Ring
     1014           
     1015        This is the motivation for Trac ticket #10470. ::
     1016           
     1017            sage: (QQ^2).list()
     1018            Traceback (most recent call last):
     1019            ...
     1020            ValueError: since it is infinite, cannot list Vector space of dimension 2 over Rational Field
     1021           
     1022        TESTS:
     1023       
     1024        The following tests the caching by adjusting the cached version. ::
     1025       
     1026            sage: R = Integers(3)
     1027            sage: R.list()
     1028            [0, 1, 2]
     1029            sage: R._list[0] = 'junk'
     1030            sage: R.list()
     1031            ['junk', 1, 2]
     1032           
     1033        Some objects do not know if they are finite or not, and so a request
     1034        for the entire list of an infinite set could run without stopping. 
     1035        Asking for ``F.list()`` below  will not finish without an interruption
     1036        like pressing Ctrl-C.  Try it. ::
     1037       
     1038            sage: R.<t> = QQ[]
     1039            sage: F = FractionField(R)
     1040            sage: F.is_finite()
     1041            Traceback (most recent call last):
     1042            ...
     1043            NotImplementedError
    9961044        """
    9971045        try:
    9981046            if self._list is not None:
    9991047                return self._list
    10001048        except AttributeError:
    10011049            pass
     1050        # if known to be infinite, give up
     1051        # if unsure, proceed (which will hang for an infinite set)
     1052        try:
     1053            if not self.is_finite():
     1054                raise ValueError('since it is infinite, cannot list %s' % self )
     1055        except AttributeError, NotImplementedError:
     1056            pass
    10021057        the_list = list(self.__iter__())
    10031058        try:
    10041059            self._list = the_list