Patch #1107973: tarfile.ExFileObject iterators.

This commit is contained in:
Martin v. Löwis 2005-03-03 08:17:42 +00:00
parent 8ed338ab44
commit df24153f65
3 changed files with 28 additions and 0 deletions

View File

@ -616,6 +616,22 @@ class ExFileObject(object):
"""Close the file object. """Close the file object.
""" """
self.closed = True self.closed = True
def __iter__(self):
"""Get an iterator over the file object.
"""
if self.closed:
raise ValueError("I/O operation on closed file")
return self
def next(self):
"""Get the next item from the file iterator.
"""
result = self.readline()
if not result:
raise StopIteration
return result
#class ExFileObject #class ExFileObject
#------------------ #------------------

View File

@ -91,6 +91,16 @@ class ReadTest(BaseTest):
self.assert_(lines1 == lines2, self.assert_(lines1 == lines2,
"_FileObject.readline() does not work correctly") "_FileObject.readline() does not work correctly")
def test_iter(self):
# Test iteration over ExFileObject.
if self.sep != "|":
filename = "0-REGTYPE-TEXT"
self.tar.extract(filename, dirname())
lines1 = file(os.path.join(dirname(), filename), "rU").readlines()
lines2 = [line for line in self.tar.extractfile(filename)]
self.assert_(lines1 == lines2,
"ExFileObject iteration does not work correctly")
def test_seek(self): def test_seek(self):
"""Test seek() method of _FileObject, incl. random reading. """Test seek() method of _FileObject, incl. random reading.
""" """

View File

@ -59,6 +59,8 @@ Extension Modules
Library Library
------- -------
- Patch #1107973: Allow to iterate over the lines of a tarfile.ExFileObject.
- Patch #1104111: Alter setup.py --help and --help-commands. - Patch #1104111: Alter setup.py --help and --help-commands.
- Patch #1121234: Properly cleanup _exit and tkerror commands. - Patch #1121234: Properly cleanup _exit and tkerror commands.