Changeset 583:79fe7ae18727

Show
Ignore:
Timestamp:
2006-10-19 17:28:15 (4 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
add_stat_caching
convert_revision:
svn:778c30c8-61e0-0310-89d4-fe2f97a467b2/branches/add_stat_caching@609
Message:
Added iterator interface for `FTPFile`s.
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • _test_ftputil.py

    r548 r583  
    325325        self.assertRaises(IndexError, operator.__getitem__, xrl_obj, 3) 
    326326 
     327    def test_binary_iterator(self): 
     328        """Test the iterator interface of `FTPFile` objects.""" 
     329        host = _test_base.ftp_host_factory(session_factory=ReadMockSession) 
     330        input_ = host.file('dummy') 
     331        input_iterator = iter(input_) 
     332        self.assertEqual(input_iterator.next(), "line 1\n") 
     333        self.assertEqual(input_iterator.next(), "another line\n") 
     334        self.assertEqual(input_iterator.next(), "yet another line") 
     335        self.assertRaises(StopIteration, input_iterator.next) 
     336        input_.close() 
     337 
     338    def test_ascii_iterator(self): 
     339        """Test the iterator interface of `FTPFile` objects.""" 
     340        host = _test_base.ftp_host_factory(session_factory=ReadMockSession) 
     341        input_ = host.file('dummy', 'rb') 
     342        input_iterator = iter(input_) 
     343        self.assertEqual(input_iterator.next(), "line 1\r\n") 
     344        self.assertEqual(input_iterator.next(), "another line\r\n") 
     345        self.assertEqual(input_iterator.next(), "yet another line") 
     346        self.assertRaises(StopIteration, input_iterator.next) 
     347        input_.close() 
     348 
    327349    def test_read_unknown_file(self): 
    328350        """Test whether reading a file which isn't there fails.""" 
  • ftp_file.py

    r539 r583  
    187187        return _XReadlines(self) 
    188188 
     189    def __iter__(self): 
     190        """Return an file iterator.""" 
     191        return self 
     192 
     193    def next(self): 
     194        """ 
     195        Return the next line or raise `StopIteration`, if there are 
     196        no more. 
     197        """ 
     198        # apply implicit line ending conversion 
     199        line = self.readline() 
     200        if line: 
     201            return line 
     202        else: 
     203            raise StopIteration 
     204 
    189205    def write(self, data): 
    190206        """Write data to file. Do linesep conversion for text mode."""