[Python-checkins] cpython (2.7): Correctly detect bzip2 compressed streams with blocksizes other than 900k.

lars.gustaebel python-checkins at python.org
Tue Dec 6 13:10:37 CET 2011


http://hg.python.org/cpython/rev/6d1a91e9f506
changeset: 73870:6d1a91e9f506
branch: 2.7
parent: 73853:478b4e9551fa
user: Lars Gustäbel <lars at gustaebel.de>
date: Tue Dec 06 13:07:09 2011 +0100
summary:
 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
files:
 Lib/tarfile.py | 2 +-
 Lib/test/test_tarfile.py | 17 +++++++++++++++++
 Misc/NEWS | 3 +++
 3 files changed, 21 insertions(+), 1 deletions(-)
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -627,7 +627,7 @@
 def getcomptype(self):
 if self.buf.startswith("037円213円010円"):
 return "gz"
- if self.buf.startswith("BZh91"):
+ if self.buf[0:3] == "BZh" and self.buf[4:10] == "1AY&SY":
 return "bz2"
 return "tar"
 
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -440,6 +440,23 @@
 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):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -79,6 +79,9 @@
 Library
 -------
 
+- tarfile.py: Correctly detect bzip2 compressed streams with blocksizes
+ other than 900k.
+
 - Issue #13439: Fix many errors in turtle docstrings.
 
 - Issue #12856: Ensure child processes do not inherit the parent's random
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /