From: Serhiy Storchaka <storchaka@gmail.com>
Date: Thu, 9 May 2013 11:22:26 +0000 (+0300)
Subject: Issue #16601: Restarting iteration over tarfile no more continues from where
X-Git-Tag: v3.3.2~16
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=263fab94ee1f32b53b709c25c76044719d0eb88e;p=python

Issue #16601: Restarting iteration over tarfile no more continues from where
it left off.  Patch by Michael Birtwell.
---

diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 11b4b68146..6693840c22 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -2398,16 +2398,18 @@ class TarIter:
         # Fix for SF #1100429: Under rare circumstances it can
         # happen that getmembers() is called during iteration,
         # which will cause TarIter to stop prematurely.
-        if not self.tarfile._loaded:
+
+        if self.index == 0 and self.tarfile.firstmember is not None:
+            tarinfo = self.tarfile.next()
+        elif self.index < len(self.tarfile.members):
+            tarinfo = self.tarfile.members[self.index]
+        elif not self.tarfile._loaded:
             tarinfo = self.tarfile.next()
             if not tarinfo:
                 self.tarfile._loaded = True
                 raise StopIteration
         else:
-            try:
-                tarinfo = self.tarfile.members[self.index]
-            except IndexError:
-                raise StopIteration
+            raise StopIteration
         self.index += 1
         return tarinfo
 
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index b224bf093f..9b98df215d 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -415,6 +415,14 @@ class MiscReadTest(CommonReadTest):
         finally:
             support.unlink(empty)
 
+    def test_parallel_iteration(self):
+        # Issue #16601: Restarting iteration over tarfile continued
+        # from where it left off.
+        with tarfile.open(self.tarname) as tar:
+            for m1, m2 in zip(tar, tar):
+                self.assertEqual(m1.offset, m2.offset)
+                self.assertEqual(m1.get_info(), m2.get_info())
+
 
 class StreamReadTest(CommonReadTest):
 
diff --git a/Misc/ACKS b/Misc/ACKS
index 56bb6afb83..ed30056c6a 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -117,6 +117,7 @@ Adrian von Bidder
 David Binger
 Dominic Binks
 Philippe Biondi
+Michael Birtwell
 Stuart Bishop
 Roy Bixler
 Jonathan Black
diff --git a/Misc/NEWS b/Misc/NEWS
index 4739cfdff5..4d1ee47fd4 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -47,6 +47,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #16601: Restarting iteration over tarfile no more continues from where
+  it left off.  Patch by Michael Birtwell.
+
 - Issue #17289: The readline module now plays nicer with external modules
   or applications changing the rl_completer_word_break_characters global
   variable.  Initial patch by Bradley Froehle.