def read(self, size):
x = len(self.buf)
while x < size:
- try:
- raw = self.fileobj.read(self.blocksize)
- data = self.bz2obj.decompress(raw)
- self.buf += data
- except EOFError:
+ raw = self.fileobj.read(self.blocksize)
+ if not raw:
break
+ data = self.bz2obj.decompress(raw)
+ self.buf += data
x += len(data)
buf = self.buf[:size]
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 MyBytesIO(io.BytesIO):
+ hit_eof = False
+ def read(self, n):
+ if self.hit_eof:
+ raise AssertionError("infinite loop detected in tarfile.open()")
+ self.hit_eof = self.tell() == len(self.getvalue())
+ return super(MyBytesIO, self).read(n)
+
+ data = bz2.compress(tarfile.TarInfo("foo").tobuf())
+ for x in range(len(data) + 1):
+ tarfile.open(fileobj=MyBytesIO(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)
Bz2StreamReadTest,
Bz2WriteTest,
Bz2StreamWriteTest,
+ Bz2PartialReadTest,
]
try:
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 #2110: Add support for thousands separator and 'n' type
specifier to Decimal.__format__