summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Gustäbel <lars@gustaebel.de>2011-12-06 12:00:58 (GMT)
committerLars Gustäbel <lars@gustaebel.de>2011-12-06 12:00:58 (GMT)
commit45fb08218069ec5589c8595cc34d111846ce016d (patch)
treeb3abcc83280048113e595e9b1158e7ef49775321
parent04f6974d84e8054e0aafe5bceef8d5dca14a65d5 (diff)
parented1ac587df34dc30ddd49549d3286befb059dd31 (diff)
downloadcpython-45fb08218069ec5589c8595cc34d111846ce016d.zip
cpython-45fb08218069ec5589c8595cc34d111846ce016d.tar.gz
cpython-45fb08218069ec5589c8595cc34d111846ce016d.tar.bz2
Merge with 3.2: Correctly detect bzip2 compressed streams with blocksizes other than 900k.
-rw-r--r--Lib/tarfile.py2
-rw-r--r--Lib/test/test_tarfile.py22
-rw-r--r--Misc/NEWS3
3 files changed, 22 insertions, 5 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index cf4e164..c185954 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -624,7 +624,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"
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index d346113..8bdef9f 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -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,
diff --git a/Misc/NEWS b/Misc/NEWS
index f941c63..c98c956 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -406,6 +406,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 #6715: Add a module 'lzma' for compression using the LZMA algorithm.