Commits

Anonymous committed ce6a588

[svn r70527] 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.

(backported from r70523)

Comments (0)

Files changed (3)

 """Read from and write to tar format archives.
 """
 
-__version__ = "$Revision: 60730 $"
+__version__ = "$Revision: 70527 $"
 # $Source$
 
 version     = "0.8.0"
 __author__  = "Lars Gustäbel (lars@gustaebel.de)"
-__date__    = "$Date: 2008-02-11 19:36:07 +0100 (Mon, 11 Feb 2008) $"
-__cvsid__   = "$Id: tarfile.py 60730 2008-02-11 18:36:07Z lars.gustaebel $"
+__date__    = "$Date: 2009-03-22 21:48:03 +0100 (Sun, 22 Mar 2009) $"
+__cvsid__   = "$Id: tarfile.py 70527 2009-03-22 20:48:03Z lars.gustaebel $"
 __credits__ = "Gustavo Niemeyer, Niels Gustäbel, Richard Townsend."
 
 #---------
         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)
 

Lib/test/test_tarfile.py

     class ReadFileobjTestBzip2(ReadFileobjTest):
         comp = "bz2"
 
+    class PartialReadTestBzip2(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")
+
 # If importing gzip failed, discard the Gzip TestCases.
 if not gzip:
     del ReadTestGzip
             WriteTestBzip2, WriteStreamTestBzip2,
             ReadDetectTestBzip2, ReadDetectFileobjTestBzip2,
             ReadAsteriskTestBzip2, ReadStreamAsteriskTestBzip2,
-            ReadFileobjTestBzip2
+            ReadFileobjTestBzip2, PartialReadTestBzip2
         ])
     try:
         test_support.run_unittest(*tests)
 
 (editors: check NEWS.help for information about editing NEWS using ReST.)
 
+What's New in Python 2.5.5?
+===========================
+
+*Release date: XX-XXX-20XX*
+
+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.
+
+
 What's New in Python 2.5.4?
 ===========================