diff options
author | Lars Gustäbel <lars@gustaebel.de> | 2011-12-06 12:56:38 +0100 |
---|---|---|
committer | Lars Gustäbel <lars@gustaebel.de> | 2011-12-06 12:56:38 +0100 |
commit | e34f41b9cddfefe11bb4676da17e7fd4b57f85b9 (patch) | |
tree | bb78e4f36a175290b65300aefdbc6e1cfe260674 /Lib/test/test_tarfile.py | |
parent | 175d9106df53296299ccb9097b5b969625a831c6 (diff) | |
download | cpython-e34f41b9cddfefe11bb4676da17e7fd4b57f85b9.tar.gz |
Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Diffstat (limited to 'Lib/test/test_tarfile.py')
-rw-r--r-- | Lib/test/test_tarfile.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 37cfbba898..1757e4495b 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, |