summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2005-03-03 08:17:42 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2005-03-03 08:17:42 (GMT)
commitdf24153f650f39ca82db143cc4a58062412a0896 (patch)
treeef25511e9b65ed1ef05a0848ef456d4e728f565f /Lib
parent8ed338ab44daf3cd681d73156fd1d9b6bed53795 (diff)
downloadcpython-df24153f650f39ca82db143cc4a58062412a0896.zip
cpython-df24153f650f39ca82db143cc4a58062412a0896.tar.gz
cpython-df24153f650f39ca82db143cc4a58062412a0896.tar.bz2
Patch #1107973: tarfile.ExFileObject iterators.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tarfile.py16
-rw-r--r--Lib/test/test_tarfile.py10
2 files changed, 26 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 9d00615..9bfad25 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -616,6 +616,22 @@ class ExFileObject(object):
"""Close the file object.
"""
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
#------------------
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 0fae5ef..a6c4c4a 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -91,6 +91,16 @@ class ReadTest(BaseTest):
self.assert_(lines1 == lines2,
"_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):
"""Test seek() method of _FileObject, incl. random reading.
"""