]> granicus.if.org Git - python/commitdiff
Correctly detect bzip2 compressed streams with blocksizes other than 900k.
authorLars Gustäbel <lars@gustaebel.de>
Tue, 6 Dec 2011 11:56:38 +0000 (12:56 +0100)
committerLars Gustäbel <lars@gustaebel.de>
Tue, 6 Dec 2011 11:56:38 +0000 (12:56 +0100)
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index d8d94f0292bfe5d059356d9949d32c40e0f7c016..226d4818ca78359cedab6dbdf5b7ef489d74f539 100644 (file)
@@ -627,7 +627,7 @@ class _StreamProxy(object):
     def getcomptype(self):
         if self.buf.startswith(b"\037\213\010"):
             return "gz"
-        if self.buf.startswith(b"BZh91"):
+        if self.buf[0:3] == b"BZh" and self.buf[4:10] == b"1AY&SY":
             return "bz2"
         return "tar"
 
index 37cfbba898e93171315b7acc349daf48bf9b6153..1757e4495b10ea0efe55b76a46de87bbc6d9b1ad 100644 (file)
@@ -529,6 +529,23 @@ class DetectReadTest(unittest.TestCase):
     def test_detect_fileobj(self):
         self._test_modes(self._testfunc_fileobj)
 
+    def test_detect_stream_bz2(self):
+        # Originally, tarfile's stream detection looked for the string
+        # "BZh91" at the start of the file. This is incorrect because
+        # the '9' represents the blocksize (900kB). If the file was
+        # compressed using another blocksize autodetection fails.
+        if not bz2:
+            return
+
+        with open(tarname, "rb") as fobj:
+            data = fobj.read()
+
+        # Compress with blocksize 100kB, the file starts with "BZh11".
+        with bz2.BZ2File(tmpname, "wb", compresslevel=1) as fobj:
+            fobj.write(data)
+
+        self._testfunc_file(tmpname, "r|*")
+
 
 class MemberReadTest(ReadTest):
 
@@ -1818,11 +1835,8 @@ def test_main():
     if bz2:
         # Create testtar.tar.bz2 and add bz2-specific tests.
         support.unlink(bz2name)
-        tar = bz2.BZ2File(bz2name, "wb")
-        try:
+        with bz2.BZ2File(bz2name, "wb") as tar:
             tar.write(data)
-        finally:
-            tar.close()
 
         tests += [
             Bz2MiscReadTest,
index e65e8054b4a0a3b44f922ceaf70bcd8a50f8ed78..8640d70c458e254f7a9cca790c1bb50e8da7d09f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -90,6 +90,9 @@ Core and Builtins
 Library
 -------
 
+- tarfile.py: Correctly detect bzip2 compressed streams with blocksizes
+  other than 900k.
+
 - Issue #13439: Fix many errors in turtle docstrings.
 
 - Issue #13487: Make inspect.getmodule robust against changes done to