[Python-checkins] r70524 - in python/branches/release26-maint: Lib/tarfile.py Lib/test/test_tarfile.py Misc/NEWS

lars.gustaebel python-checkins at python.org
Sun Mar 22 21:22:39 CET 2009


Author: lars.gustaebel
Date: Sun Mar 22 21:22:29 2009
New Revision: 70524

Log:
Merged revisions 70523 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r70523 | lars.gustaebel | 2009-03-22 21:09:33 +0100 (Sun, 22 Mar 2009) | 5 lines
  
  Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
  forever on incomplete input. That caused tarfile.open() to hang when used
  with mode 'r' or 'r:bz2' and a fileobj argument that contained no data or
  partial bzip2 compressed data.
........


Modified:
   python/branches/release26-maint/   (props changed)
   python/branches/release26-maint/Lib/tarfile.py
   python/branches/release26-maint/Lib/test/test_tarfile.py
   python/branches/release26-maint/Misc/NEWS

Modified: python/branches/release26-maint/Lib/tarfile.py
==============================================================================
--- python/branches/release26-maint/Lib/tarfile.py	(original)
+++ python/branches/release26-maint/Lib/tarfile.py	Sun Mar 22 21:22:29 2009
@@ -662,12 +662,11 @@
         b = [self.buf]
         x = len(self.buf)
         while x < size:
-            try:
-                raw = self.fileobj.read(self.blocksize)
-                data = self.bz2obj.decompress(raw)
-                b.append(data)
-            except EOFError:
+            raw = self.fileobj.read(self.blocksize)
+            if not raw:
                 break
+            data = self.bz2obj.decompress(raw)
+            b.append(data)
             x += len(data)
         self.buf = "".join(b)
 

Modified: python/branches/release26-maint/Lib/test/test_tarfile.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_tarfile.py	(original)
+++ python/branches/release26-maint/Lib/test/test_tarfile.py	Sun Mar 22 21:22:29 2009
@@ -1140,6 +1140,30 @@
 class Bz2StreamWriteTest(StreamWriteTest):
     mode = "w|bz2"
 
+class Bz2PartialReadTest(unittest.TestCase):
+    # Issue5068: The _BZ2Proxy.read() method loops forever
+    # on an empty or partial bzipped file.
+
+    def _test_partial_input(self, mode):
+        class MyStringIO(StringIO.StringIO):
+            hit_eof = False
+            def read(self, n):
+                if self.hit_eof:
+                    raise AssertionError("infinite loop detected in tarfile.open()")
+                self.hit_eof = self.pos == self.len
+                return StringIO.StringIO.read(self, n)
+
+        data = bz2.compress(tarfile.TarInfo("foo").tobuf())
+        for x in range(len(data) + 1):
+            tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode)
+
+    def test_partial_input(self):
+        self._test_partial_input("r")
+
+    def test_partial_input_bz2(self):
+        self._test_partial_input("r:bz2")
+
+
 def test_main():
     if not os.path.exists(TEMPDIR):
         os.mkdir(TEMPDIR)
@@ -1196,6 +1220,7 @@
             Bz2StreamReadTest,
             Bz2WriteTest,
             Bz2StreamWriteTest,
+            Bz2PartialReadTest,
         ]
 
     try:

Modified: python/branches/release26-maint/Misc/NEWS
==============================================================================
--- python/branches/release26-maint/Misc/NEWS	(original)
+++ python/branches/release26-maint/Misc/NEWS	Sun Mar 22 21:22:29 2009
@@ -92,6 +92,11 @@
 Library
 -------
 
+- Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
+  forever on incomplete input. That caused tarfile.open() to hang when used
+  with mode 'r' or 'r:bz2' and a fileobj argument that contained no data or
+  partial bzip2 compressed data.
+
 - Issue #5536: urllib.urlretrieve makes sure to close the file it's writing to
   even if an exception occurs.
 


More information about the Python-checkins mailing list