]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #1107973: tarfile.ExFileObject iterators.
authorMartin v. Löwis <martin@v.loewis.de>
Thu, 3 Mar 2005 08:17:42 +0000 (08:17 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Thu, 3 Mar 2005 08:17:42 +0000 (08:17 +0000)
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index 9d0061592efe298b212d060a1f31c7176030e40e..9bfad250cfb936cd75e93b34e1a4a2ff546f1791 100644 (file)
@@ -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
 
 #------------------
index 0fae5efcd4010bfa3be7a5a5030d0562d96fc22d..a6c4c4a4f958ea9df0bb4570db7bc55256a96066 100644 (file)
@@ -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.
         """
index a128cc78234a365b8c9515eb3470fc54e90b78e2..0912e00e5dc47d5d0f50370f81d2d6a64eefbb82 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -59,6 +59,8 @@ Extension Modules
 Library
 -------
 
+- Patch #1107973: Allow to iterate over the lines of a tarfile.ExFileObject.
+
 - Patch #1104111: Alter setup.py --help and --help-commands.
 
 - Patch #1121234: Properly cleanup _exit and tkerror commands.