Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 2 | from test.support import bigmemtest, _4G |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 3 | |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 4 | import unittest |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 5 | from io import BytesIO, DEFAULT_BUFFER_SIZE |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 6 | import os |
Nadeem Vawda | 3797065 | 2013-10-28 21:35:23 +0100 | [diff] [blame] | 7 | import pickle |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 8 | import glob |
Berker Peksag | 8bdd448 | 2016-10-02 20:07:06 +0300 | [diff] [blame^] | 9 | import pathlib |
Nadeem Vawda | ea4b46f | 2011-04-12 23:02:42 +0200 | [diff] [blame] | 10 | import random |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 11 | import subprocess |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 12 | import sys |
Nadeem Vawda | 8a9e99c | 2013-10-19 00:11:06 +0200 | [diff] [blame] | 13 | from test.support import unlink |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 14 | import _compression |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 15 | |
| 16 | try: |
| 17 | import threading |
| 18 | except ImportError: |
| 19 | threading = None |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 20 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 21 | # Skip tests if the bz2 module doesn't exist. |
| 22 | bz2 = support.import_module('bz2') |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 23 | from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 24 | |
Tim Peters | 9de06bd | 2002-11-09 06:45:31 +0000 | [diff] [blame] | 25 | |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 26 | class BaseTest(unittest.TestCase): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 27 | "Base for other testcases." |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 28 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 29 | TEXT_LINES = [ |
| 30 | b'root:x:0:0:root:/root:/bin/bash\n', |
| 31 | b'bin:x:1:1:bin:/bin:\n', |
| 32 | b'daemon:x:2:2:daemon:/sbin:\n', |
| 33 | b'adm:x:3:4:adm:/var/adm:\n', |
| 34 | b'lp:x:4:7:lp:/var/spool/lpd:\n', |
| 35 | b'sync:x:5:0:sync:/sbin:/bin/sync\n', |
| 36 | b'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n', |
| 37 | b'halt:x:7:0:halt:/sbin:/sbin/halt\n', |
| 38 | b'mail:x:8:12:mail:/var/spool/mail:\n', |
| 39 | b'news:x:9:13:news:/var/spool/news:\n', |
| 40 | b'uucp:x:10:14:uucp:/var/spool/uucp:\n', |
| 41 | b'operator:x:11:0:operator:/root:\n', |
| 42 | b'games:x:12:100:games:/usr/games:\n', |
| 43 | b'gopher:x:13:30:gopher:/usr/lib/gopher-data:\n', |
| 44 | b'ftp:x:14:50:FTP User:/var/ftp:/bin/bash\n', |
| 45 | b'nobody:x:65534:65534:Nobody:/home:\n', |
| 46 | b'postfix:x:100:101:postfix:/var/spool/postfix:\n', |
| 47 | b'niemeyer:x:500:500::/home/niemeyer:/bin/bash\n', |
| 48 | b'postgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\n', |
| 49 | b'mysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\n', |
| 50 | b'www:x:103:104::/var/www:/bin/false\n', |
| 51 | ] |
| 52 | TEXT = b''.join(TEXT_LINES) |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 53 | DATA = b'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1\x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10\x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`' |
Nadeem Vawda | 638fb9b | 2013-01-02 23:02:00 +0100 | [diff] [blame] | 54 | EMPTY_DATA = b'BZh9\x17rE8P\x90\x00\x00\x00\x00' |
Nadeem Vawda | 1de19ac | 2013-12-04 23:01:15 +0100 | [diff] [blame] | 55 | BAD_DATA = b'this is not a valid bzip2 file' |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 56 | |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 57 | # Some tests need more than one block of uncompressed data. Since one block |
| 58 | # is at least 100 kB, we gather some data dynamically and compress it. |
| 59 | # Note that this assumes that compression works correctly, so we cannot |
| 60 | # simply use the bigger test data for all tests. |
| 61 | test_size = 0 |
| 62 | BIG_TEXT = bytearray(128*1024) |
| 63 | for fname in glob.glob(os.path.join(os.path.dirname(__file__), '*.py')): |
| 64 | with open(fname, 'rb') as fh: |
| 65 | test_size += fh.readinto(memoryview(BIG_TEXT)[test_size:]) |
| 66 | if test_size > 128*1024: |
| 67 | break |
| 68 | BIG_DATA = bz2.compress(BIG_TEXT, compresslevel=1) |
| 69 | |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 70 | def setUp(self): |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 71 | self.filename = support.TESTFN |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 72 | |
| 73 | def tearDown(self): |
| 74 | if os.path.isfile(self.filename): |
| 75 | os.unlink(self.filename) |
| 76 | |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 77 | if sys.platform == "win32": |
| 78 | # bunzip2 isn't available to run on Windows. |
| 79 | def decompress(self, data): |
| 80 | return bz2.decompress(data) |
| 81 | else: |
Tim Peters | 9de06bd | 2002-11-09 06:45:31 +0000 | [diff] [blame] | 82 | def decompress(self, data): |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 83 | pop = subprocess.Popen("bunzip2", shell=True, |
| 84 | stdin=subprocess.PIPE, |
| 85 | stdout=subprocess.PIPE, |
| 86 | stderr=subprocess.STDOUT) |
| 87 | pop.stdin.write(data) |
| 88 | pop.stdin.close() |
| 89 | ret = pop.stdout.read() |
| 90 | pop.stdout.close() |
Tim Peters | 9de06bd | 2002-11-09 06:45:31 +0000 | [diff] [blame] | 91 | if pop.wait() != 0: |
| 92 | ret = bz2.decompress(data) |
| 93 | return ret |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 94 | |
Tim Peters | 9de06bd | 2002-11-09 06:45:31 +0000 | [diff] [blame] | 95 | |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 96 | class BZ2FileTest(BaseTest): |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 97 | "Test the BZ2File class." |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 98 | |
Nadeem Vawda | 1de19ac | 2013-12-04 23:01:15 +0100 | [diff] [blame] | 99 | def createTempFile(self, streams=1, suffix=b""): |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 100 | with open(self.filename, "wb") as f: |
Nadeem Vawda | 07d291a | 2011-05-30 01:44:45 +0200 | [diff] [blame] | 101 | f.write(self.DATA * streams) |
Nadeem Vawda | 1de19ac | 2013-12-04 23:01:15 +0100 | [diff] [blame] | 102 | f.write(suffix) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 103 | |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 104 | def testBadArgs(self): |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 105 | self.assertRaises(TypeError, BZ2File, 123.456) |
Serhiy Storchaka | 85c3033 | 2015-02-15 13:58:23 +0200 | [diff] [blame] | 106 | self.assertRaises(ValueError, BZ2File, os.devnull, "z") |
| 107 | self.assertRaises(ValueError, BZ2File, os.devnull, "rx") |
| 108 | self.assertRaises(ValueError, BZ2File, os.devnull, "rbt") |
| 109 | self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=0) |
| 110 | self.assertRaises(ValueError, BZ2File, os.devnull, compresslevel=10) |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 111 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 112 | def testRead(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 113 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 114 | with BZ2File(self.filename) as bz2f: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 115 | self.assertRaises(TypeError, bz2f.read, float()) |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 116 | self.assertEqual(bz2f.read(), self.TEXT) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 117 | |
Nadeem Vawda | 1de19ac | 2013-12-04 23:01:15 +0100 | [diff] [blame] | 118 | def testReadBadFile(self): |
| 119 | self.createTempFile(streams=0, suffix=self.BAD_DATA) |
| 120 | with BZ2File(self.filename) as bz2f: |
| 121 | self.assertRaises(OSError, bz2f.read) |
| 122 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 123 | def testReadMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 124 | self.createTempFile(streams=5) |
| 125 | with BZ2File(self.filename) as bz2f: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 126 | self.assertRaises(TypeError, bz2f.read, float()) |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 127 | self.assertEqual(bz2f.read(), self.TEXT * 5) |
| 128 | |
| 129 | def testReadMonkeyMultiStream(self): |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 130 | # Test BZ2File.read() on a multi-stream archive where a stream |
| 131 | # boundary coincides with the end of the raw read buffer. |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 132 | buffer_size = _compression.BUFFER_SIZE |
| 133 | _compression.BUFFER_SIZE = len(self.DATA) |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 134 | try: |
| 135 | self.createTempFile(streams=5) |
| 136 | with BZ2File(self.filename) as bz2f: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 137 | self.assertRaises(TypeError, bz2f.read, float()) |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 138 | self.assertEqual(bz2f.read(), self.TEXT * 5) |
| 139 | finally: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 140 | _compression.BUFFER_SIZE = buffer_size |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 141 | |
Nadeem Vawda | 1de19ac | 2013-12-04 23:01:15 +0100 | [diff] [blame] | 142 | def testReadTrailingJunk(self): |
| 143 | self.createTempFile(suffix=self.BAD_DATA) |
| 144 | with BZ2File(self.filename) as bz2f: |
| 145 | self.assertEqual(bz2f.read(), self.TEXT) |
| 146 | |
| 147 | def testReadMultiStreamTrailingJunk(self): |
| 148 | self.createTempFile(streams=5, suffix=self.BAD_DATA) |
| 149 | with BZ2File(self.filename) as bz2f: |
| 150 | self.assertEqual(bz2f.read(), self.TEXT * 5) |
| 151 | |
Guido van Rossum | 75c26bc | 2007-08-07 23:29:20 +0000 | [diff] [blame] | 152 | def testRead0(self): |
Guido van Rossum | 75c26bc | 2007-08-07 23:29:20 +0000 | [diff] [blame] | 153 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 154 | with BZ2File(self.filename) as bz2f: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 155 | self.assertRaises(TypeError, bz2f.read, float()) |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 156 | self.assertEqual(bz2f.read(0), b"") |
Guido van Rossum | 75c26bc | 2007-08-07 23:29:20 +0000 | [diff] [blame] | 157 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 158 | def testReadChunk10(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 159 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 160 | with BZ2File(self.filename) as bz2f: |
| 161 | text = b'' |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 162 | while True: |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 163 | str = bz2f.read(10) |
| 164 | if not str: |
| 165 | break |
| 166 | text += str |
Antoine Pitrou | 528d9f6 | 2011-01-26 23:29:28 +0000 | [diff] [blame] | 167 | self.assertEqual(text, self.TEXT) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 168 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 169 | def testReadChunk10MultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 170 | self.createTempFile(streams=5) |
| 171 | with BZ2File(self.filename) as bz2f: |
| 172 | text = b'' |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 173 | while True: |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 174 | str = bz2f.read(10) |
| 175 | if not str: |
| 176 | break |
| 177 | text += str |
| 178 | self.assertEqual(text, self.TEXT * 5) |
| 179 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 180 | def testRead100(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 181 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 182 | with BZ2File(self.filename) as bz2f: |
| 183 | self.assertEqual(bz2f.read(100), self.TEXT[:100]) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 185 | def testPeek(self): |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 186 | self.createTempFile() |
| 187 | with BZ2File(self.filename) as bz2f: |
| 188 | pdata = bz2f.peek() |
| 189 | self.assertNotEqual(len(pdata), 0) |
| 190 | self.assertTrue(self.TEXT.startswith(pdata)) |
| 191 | self.assertEqual(bz2f.read(), self.TEXT) |
| 192 | |
| 193 | def testReadInto(self): |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 194 | self.createTempFile() |
| 195 | with BZ2File(self.filename) as bz2f: |
| 196 | n = 128 |
| 197 | b = bytearray(n) |
| 198 | self.assertEqual(bz2f.readinto(b), n) |
| 199 | self.assertEqual(b, self.TEXT[:n]) |
| 200 | n = len(self.TEXT) - n |
| 201 | b = bytearray(len(self.TEXT)) |
| 202 | self.assertEqual(bz2f.readinto(b), n) |
| 203 | self.assertEqual(b[:n], self.TEXT[-n:]) |
| 204 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 205 | def testReadLine(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 206 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 207 | with BZ2File(self.filename) as bz2f: |
| 208 | self.assertRaises(TypeError, bz2f.readline, None) |
Nadeem Vawda | 72e5865 | 2011-05-30 02:09:54 +0200 | [diff] [blame] | 209 | for line in self.TEXT_LINES: |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 210 | self.assertEqual(bz2f.readline(), line) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 211 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 212 | def testReadLineMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 213 | self.createTempFile(streams=5) |
| 214 | with BZ2File(self.filename) as bz2f: |
| 215 | self.assertRaises(TypeError, bz2f.readline, None) |
Nadeem Vawda | 72e5865 | 2011-05-30 02:09:54 +0200 | [diff] [blame] | 216 | for line in self.TEXT_LINES * 5: |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 217 | self.assertEqual(bz2f.readline(), line) |
| 218 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 219 | def testReadLines(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 220 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 221 | with BZ2File(self.filename) as bz2f: |
| 222 | self.assertRaises(TypeError, bz2f.readlines, None) |
Nadeem Vawda | 72e5865 | 2011-05-30 02:09:54 +0200 | [diff] [blame] | 223 | self.assertEqual(bz2f.readlines(), self.TEXT_LINES) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 224 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 225 | def testReadLinesMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 226 | self.createTempFile(streams=5) |
| 227 | with BZ2File(self.filename) as bz2f: |
| 228 | self.assertRaises(TypeError, bz2f.readlines, None) |
Nadeem Vawda | 72e5865 | 2011-05-30 02:09:54 +0200 | [diff] [blame] | 229 | self.assertEqual(bz2f.readlines(), self.TEXT_LINES * 5) |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 230 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 231 | def testIterator(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 232 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 233 | with BZ2File(self.filename) as bz2f: |
Nadeem Vawda | 72e5865 | 2011-05-30 02:09:54 +0200 | [diff] [blame] | 234 | self.assertEqual(list(iter(bz2f)), self.TEXT_LINES) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 235 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 236 | def testIteratorMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 237 | self.createTempFile(streams=5) |
| 238 | with BZ2File(self.filename) as bz2f: |
Nadeem Vawda | 72e5865 | 2011-05-30 02:09:54 +0200 | [diff] [blame] | 239 | self.assertEqual(list(iter(bz2f)), self.TEXT_LINES * 5) |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 240 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 241 | def testClosedIteratorDeadlock(self): |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 242 | # Issue #3309: Iteration on a closed BZ2File should release the lock. |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 243 | self.createTempFile() |
| 244 | bz2f = BZ2File(self.filename) |
| 245 | bz2f.close() |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 246 | self.assertRaises(ValueError, next, bz2f) |
| 247 | # This call will deadlock if the above call failed to release the lock. |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 248 | self.assertRaises(ValueError, bz2f.readlines) |
| 249 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 250 | def testWrite(self): |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 251 | with BZ2File(self.filename, "w") as bz2f: |
| 252 | self.assertRaises(TypeError, bz2f.write) |
| 253 | bz2f.write(self.TEXT) |
| 254 | with open(self.filename, 'rb') as f: |
| 255 | self.assertEqual(self.decompress(f.read()), self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 256 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 257 | def testWriteChunks10(self): |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 258 | with BZ2File(self.filename, "w") as bz2f: |
| 259 | n = 0 |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 260 | while True: |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 261 | str = self.TEXT[n*10:(n+1)*10] |
| 262 | if not str: |
| 263 | break |
| 264 | bz2f.write(str) |
| 265 | n += 1 |
| 266 | with open(self.filename, 'rb') as f: |
| 267 | self.assertEqual(self.decompress(f.read()), self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 268 | |
Nadeem Vawda | 249ab5e | 2011-09-11 22:38:11 +0200 | [diff] [blame] | 269 | def testWriteNonDefaultCompressLevel(self): |
| 270 | expected = bz2.compress(self.TEXT, compresslevel=5) |
| 271 | with BZ2File(self.filename, "w", compresslevel=5) as bz2f: |
| 272 | bz2f.write(self.TEXT) |
| 273 | with open(self.filename, "rb") as f: |
| 274 | self.assertEqual(f.read(), expected) |
| 275 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 276 | def testWriteLines(self): |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 277 | with BZ2File(self.filename, "w") as bz2f: |
| 278 | self.assertRaises(TypeError, bz2f.writelines) |
Nadeem Vawda | 72e5865 | 2011-05-30 02:09:54 +0200 | [diff] [blame] | 279 | bz2f.writelines(self.TEXT_LINES) |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 280 | # Issue #1535500: Calling writelines() on a closed BZ2File |
| 281 | # should raise an exception. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 282 | self.assertRaises(ValueError, bz2f.writelines, ["a"]) |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 283 | with open(self.filename, 'rb') as f: |
| 284 | self.assertEqual(self.decompress(f.read()), self.TEXT) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 285 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 286 | def testWriteMethodsOnReadOnlyFile(self): |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 287 | with BZ2File(self.filename, "w") as bz2f: |
| 288 | bz2f.write(b"abc") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 289 | |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 290 | with BZ2File(self.filename, "r") as bz2f: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 291 | self.assertRaises(OSError, bz2f.write, b"a") |
| 292 | self.assertRaises(OSError, bz2f.writelines, [b"a"]) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 293 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 294 | def testAppend(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 295 | with BZ2File(self.filename, "w") as bz2f: |
| 296 | self.assertRaises(TypeError, bz2f.write) |
| 297 | bz2f.write(self.TEXT) |
| 298 | with BZ2File(self.filename, "a") as bz2f: |
| 299 | self.assertRaises(TypeError, bz2f.write) |
| 300 | bz2f.write(self.TEXT) |
| 301 | with open(self.filename, 'rb') as f: |
| 302 | self.assertEqual(self.decompress(f.read()), self.TEXT * 2) |
| 303 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 304 | def testSeekForward(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 305 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 306 | with BZ2File(self.filename) as bz2f: |
| 307 | self.assertRaises(TypeError, bz2f.seek) |
| 308 | bz2f.seek(150) |
| 309 | self.assertEqual(bz2f.read(), self.TEXT[150:]) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 310 | |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 311 | def testSeekForwardAcrossStreams(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 312 | self.createTempFile(streams=2) |
| 313 | with BZ2File(self.filename) as bz2f: |
| 314 | self.assertRaises(TypeError, bz2f.seek) |
| 315 | bz2f.seek(len(self.TEXT) + 150) |
| 316 | self.assertEqual(bz2f.read(), self.TEXT[150:]) |
| 317 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 318 | def testSeekBackwards(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 319 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 320 | with BZ2File(self.filename) as bz2f: |
| 321 | bz2f.read(500) |
| 322 | bz2f.seek(-150, 1) |
| 323 | self.assertEqual(bz2f.read(), self.TEXT[500-150:]) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 324 | |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 325 | def testSeekBackwardsAcrossStreams(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 326 | self.createTempFile(streams=2) |
| 327 | with BZ2File(self.filename) as bz2f: |
| 328 | readto = len(self.TEXT) + 100 |
| 329 | while readto > 0: |
| 330 | readto -= len(bz2f.read(readto)) |
| 331 | bz2f.seek(-150, 1) |
| 332 | self.assertEqual(bz2f.read(), self.TEXT[100-150:] + self.TEXT) |
| 333 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 334 | def testSeekBackwardsFromEnd(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 335 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 336 | with BZ2File(self.filename) as bz2f: |
| 337 | bz2f.seek(-150, 2) |
| 338 | self.assertEqual(bz2f.read(), self.TEXT[len(self.TEXT)-150:]) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 339 | |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 340 | def testSeekBackwardsFromEndAcrossStreams(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 341 | self.createTempFile(streams=2) |
| 342 | with BZ2File(self.filename) as bz2f: |
| 343 | bz2f.seek(-1000, 2) |
| 344 | self.assertEqual(bz2f.read(), (self.TEXT * 2)[-1000:]) |
| 345 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 346 | def testSeekPostEnd(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 347 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 348 | with BZ2File(self.filename) as bz2f: |
| 349 | bz2f.seek(150000) |
| 350 | self.assertEqual(bz2f.tell(), len(self.TEXT)) |
| 351 | self.assertEqual(bz2f.read(), b"") |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 352 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 353 | def testSeekPostEndMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 354 | self.createTempFile(streams=5) |
| 355 | with BZ2File(self.filename) as bz2f: |
| 356 | bz2f.seek(150000) |
| 357 | self.assertEqual(bz2f.tell(), len(self.TEXT) * 5) |
| 358 | self.assertEqual(bz2f.read(), b"") |
| 359 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 360 | def testSeekPostEndTwice(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 361 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 362 | with BZ2File(self.filename) as bz2f: |
| 363 | bz2f.seek(150000) |
| 364 | bz2f.seek(150000) |
| 365 | self.assertEqual(bz2f.tell(), len(self.TEXT)) |
| 366 | self.assertEqual(bz2f.read(), b"") |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 367 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 368 | def testSeekPostEndTwiceMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 369 | self.createTempFile(streams=5) |
| 370 | with BZ2File(self.filename) as bz2f: |
| 371 | bz2f.seek(150000) |
| 372 | bz2f.seek(150000) |
| 373 | self.assertEqual(bz2f.tell(), len(self.TEXT) * 5) |
| 374 | self.assertEqual(bz2f.read(), b"") |
| 375 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 376 | def testSeekPreStart(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 377 | self.createTempFile() |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 378 | with BZ2File(self.filename) as bz2f: |
| 379 | bz2f.seek(-150) |
| 380 | self.assertEqual(bz2f.tell(), 0) |
| 381 | self.assertEqual(bz2f.read(), self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 382 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 383 | def testSeekPreStartMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 384 | self.createTempFile(streams=2) |
| 385 | with BZ2File(self.filename) as bz2f: |
| 386 | bz2f.seek(-150) |
| 387 | self.assertEqual(bz2f.tell(), 0) |
| 388 | self.assertEqual(bz2f.read(), self.TEXT * 2) |
| 389 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 390 | def testFileno(self): |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 391 | self.createTempFile() |
Victor Stinner | 938f635 | 2011-06-30 18:25:07 +0200 | [diff] [blame] | 392 | with open(self.filename, 'rb') as rawf: |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 393 | bz2f = BZ2File(rawf) |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 394 | try: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 395 | self.assertEqual(bz2f.fileno(), rawf.fileno()) |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 396 | finally: |
| 397 | bz2f.close() |
| 398 | self.assertRaises(ValueError, bz2f.fileno) |
| 399 | |
| 400 | def testSeekable(self): |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 401 | bz2f = BZ2File(BytesIO(self.DATA)) |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 402 | try: |
| 403 | self.assertTrue(bz2f.seekable()) |
| 404 | bz2f.read() |
| 405 | self.assertTrue(bz2f.seekable()) |
| 406 | finally: |
| 407 | bz2f.close() |
| 408 | self.assertRaises(ValueError, bz2f.seekable) |
| 409 | |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 410 | bz2f = BZ2File(BytesIO(), "w") |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 411 | try: |
| 412 | self.assertFalse(bz2f.seekable()) |
| 413 | finally: |
| 414 | bz2f.close() |
| 415 | self.assertRaises(ValueError, bz2f.seekable) |
| 416 | |
Nadeem Vawda | ae557d7 | 2012-02-12 01:51:38 +0200 | [diff] [blame] | 417 | src = BytesIO(self.DATA) |
| 418 | src.seekable = lambda: False |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 419 | bz2f = BZ2File(src) |
Nadeem Vawda | ae557d7 | 2012-02-12 01:51:38 +0200 | [diff] [blame] | 420 | try: |
| 421 | self.assertFalse(bz2f.seekable()) |
| 422 | finally: |
| 423 | bz2f.close() |
| 424 | self.assertRaises(ValueError, bz2f.seekable) |
| 425 | |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 426 | def testReadable(self): |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 427 | bz2f = BZ2File(BytesIO(self.DATA)) |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 428 | try: |
| 429 | self.assertTrue(bz2f.readable()) |
| 430 | bz2f.read() |
| 431 | self.assertTrue(bz2f.readable()) |
| 432 | finally: |
| 433 | bz2f.close() |
| 434 | self.assertRaises(ValueError, bz2f.readable) |
| 435 | |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 436 | bz2f = BZ2File(BytesIO(), "w") |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 437 | try: |
| 438 | self.assertFalse(bz2f.readable()) |
| 439 | finally: |
| 440 | bz2f.close() |
| 441 | self.assertRaises(ValueError, bz2f.readable) |
| 442 | |
| 443 | def testWritable(self): |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 444 | bz2f = BZ2File(BytesIO(self.DATA)) |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 445 | try: |
| 446 | self.assertFalse(bz2f.writable()) |
| 447 | bz2f.read() |
| 448 | self.assertFalse(bz2f.writable()) |
| 449 | finally: |
| 450 | bz2f.close() |
| 451 | self.assertRaises(ValueError, bz2f.writable) |
| 452 | |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 453 | bz2f = BZ2File(BytesIO(), "w") |
Nadeem Vawda | 44ae4a2 | 2011-11-30 17:39:30 +0200 | [diff] [blame] | 454 | try: |
| 455 | self.assertTrue(bz2f.writable()) |
| 456 | finally: |
| 457 | bz2f.close() |
| 458 | self.assertRaises(ValueError, bz2f.writable) |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 459 | |
Neal Norwitz | 7cbd247 | 2003-02-12 23:09:53 +0000 | [diff] [blame] | 460 | def testOpenDel(self): |
| 461 | self.createTempFile() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 462 | for i in range(10000): |
Neal Norwitz | 7cbd247 | 2003-02-12 23:09:53 +0000 | [diff] [blame] | 463 | o = BZ2File(self.filename) |
| 464 | del o |
| 465 | |
Gustavo Niemeyer | 572f523 | 2003-04-29 14:53:08 +0000 | [diff] [blame] | 466 | def testOpenNonexistent(self): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 467 | self.assertRaises(OSError, BZ2File, "/non/existent") |
Gustavo Niemeyer | 572f523 | 2003-04-29 14:53:08 +0000 | [diff] [blame] | 468 | |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 469 | def testReadlinesNoNewline(self): |
| 470 | # Issue #1191043: readlines() fails on a file containing no newline. |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 471 | data = b'BZh91AY&SY\xd9b\x89]\x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00!\x9ah3M\x13<]\xc9\x14\xe1BCe\x8a%t' |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 472 | with open(self.filename, "wb") as f: |
| 473 | f.write(data) |
| 474 | with BZ2File(self.filename) as bz2f: |
| 475 | lines = bz2f.readlines() |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 476 | self.assertEqual(lines, [b'Test']) |
Brian Curtin | d2e948d | 2010-10-13 02:56:26 +0000 | [diff] [blame] | 477 | with BZ2File(self.filename) as bz2f: |
| 478 | xlines = list(bz2f.readlines()) |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 479 | self.assertEqual(xlines, [b'Test']) |
Georg Brandl | 33a5f2a | 2005-08-21 14:16:04 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | 308705e | 2009-01-10 16:22:51 +0000 | [diff] [blame] | 481 | def testContextProtocol(self): |
Antoine Pitrou | 308705e | 2009-01-10 16:22:51 +0000 | [diff] [blame] | 482 | f = None |
| 483 | with BZ2File(self.filename, "wb") as f: |
| 484 | f.write(b"xxx") |
| 485 | f = BZ2File(self.filename, "rb") |
| 486 | f.close() |
| 487 | try: |
| 488 | with f: |
| 489 | pass |
| 490 | except ValueError: |
| 491 | pass |
| 492 | else: |
| 493 | self.fail("__enter__ on a closed file didn't raise an exception") |
| 494 | try: |
| 495 | with BZ2File(self.filename, "wb") as f: |
| 496 | 1/0 |
| 497 | except ZeroDivisionError: |
| 498 | pass |
| 499 | else: |
| 500 | self.fail("1/0 didn't raise an exception") |
| 501 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 502 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Antoine Pitrou | c66363f | 2009-10-27 17:47:14 +0000 | [diff] [blame] | 503 | def testThreading(self): |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 504 | # Issue #7205: Using a BZ2File from several threads shouldn't deadlock. |
Antoine Pitrou | c66363f | 2009-10-27 17:47:14 +0000 | [diff] [blame] | 505 | data = b"1" * 2**20 |
| 506 | nthreads = 10 |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 507 | with BZ2File(self.filename, 'wb') as f: |
Antoine Pitrou | c66363f | 2009-10-27 17:47:14 +0000 | [diff] [blame] | 508 | def comp(): |
| 509 | for i in range(5): |
| 510 | f.write(data) |
| 511 | threads = [threading.Thread(target=comp) for i in range(nthreads)] |
Serhiy Storchaka | 263dcd2 | 2015-04-01 13:01:14 +0300 | [diff] [blame] | 512 | with support.start_threads(threads): |
| 513 | pass |
Antoine Pitrou | c66363f | 2009-10-27 17:47:14 +0000 | [diff] [blame] | 514 | |
Nadeem Vawda | 72750a8 | 2012-01-18 01:57:14 +0200 | [diff] [blame] | 515 | def testWithoutThreading(self): |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 516 | module = support.import_fresh_module("bz2", blocked=("threading",)) |
| 517 | with module.BZ2File(self.filename, "wb") as f: |
Nadeem Vawda | 72750a8 | 2012-01-18 01:57:14 +0200 | [diff] [blame] | 518 | f.write(b"abc") |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 519 | with module.BZ2File(self.filename, "rb") as f: |
Nadeem Vawda | 72750a8 | 2012-01-18 01:57:14 +0200 | [diff] [blame] | 520 | self.assertEqual(f.read(), b"abc") |
| 521 | |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 522 | def testMixedIterationAndReads(self): |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 523 | self.createTempFile() |
| 524 | linelen = len(self.TEXT_LINES[0]) |
| 525 | halflen = linelen // 2 |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 526 | with BZ2File(self.filename) as bz2f: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 527 | bz2f.read(halflen) |
| 528 | self.assertEqual(next(bz2f), self.TEXT_LINES[0][halflen:]) |
| 529 | self.assertEqual(bz2f.read(), self.TEXT[linelen:]) |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 530 | with BZ2File(self.filename) as bz2f: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 531 | bz2f.readline() |
| 532 | self.assertEqual(next(bz2f), self.TEXT_LINES[1]) |
| 533 | self.assertEqual(bz2f.readline(), self.TEXT_LINES[2]) |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 534 | with BZ2File(self.filename) as bz2f: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 535 | bz2f.readlines() |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 536 | self.assertRaises(StopIteration, next, bz2f) |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 537 | self.assertEqual(bz2f.readlines(), []) |
| 538 | |
Nadeem Vawda | f3ecb83 | 2011-05-30 01:58:12 +0200 | [diff] [blame] | 539 | def testMultiStreamOrdering(self): |
| 540 | # Test the ordering of streams when reading a multi-stream archive. |
| 541 | data1 = b"foo" * 1000 |
| 542 | data2 = b"bar" * 1000 |
| 543 | with BZ2File(self.filename, "w") as bz2f: |
| 544 | bz2f.write(data1) |
| 545 | with BZ2File(self.filename, "a") as bz2f: |
| 546 | bz2f.write(data2) |
| 547 | with BZ2File(self.filename) as bz2f: |
| 548 | self.assertEqual(bz2f.read(), data1 + data2) |
| 549 | |
Nadeem Vawda | 10c8791 | 2012-06-20 01:48:50 +0200 | [diff] [blame] | 550 | def testOpenBytesFilename(self): |
| 551 | str_filename = self.filename |
| 552 | try: |
| 553 | bytes_filename = str_filename.encode("ascii") |
| 554 | except UnicodeEncodeError: |
| 555 | self.skipTest("Temporary file name needs to be ASCII") |
| 556 | with BZ2File(bytes_filename, "wb") as f: |
| 557 | f.write(self.DATA) |
| 558 | with BZ2File(bytes_filename, "rb") as f: |
| 559 | self.assertEqual(f.read(), self.DATA) |
| 560 | # Sanity check that we are actually operating on the right file. |
| 561 | with BZ2File(str_filename, "rb") as f: |
| 562 | self.assertEqual(f.read(), self.DATA) |
| 563 | |
Berker Peksag | 8bdd448 | 2016-10-02 20:07:06 +0300 | [diff] [blame^] | 564 | def testOpenPathLikeFilename(self): |
| 565 | filename = pathlib.Path(self.filename) |
| 566 | with BZ2File(filename, "wb") as f: |
| 567 | f.write(self.DATA) |
| 568 | with BZ2File(filename, "rb") as f: |
| 569 | self.assertEqual(f.read(), self.DATA) |
| 570 | |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 571 | def testDecompressLimited(self): |
| 572 | """Decompressed data buffering should be limited""" |
Serhiy Storchaka | 5f1a518 | 2016-09-11 14:41:02 +0300 | [diff] [blame] | 573 | bomb = bz2.compress(b'\0' * int(2e6), compresslevel=9) |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 574 | self.assertLess(len(bomb), _compression.BUFFER_SIZE) |
| 575 | |
| 576 | decomp = BZ2File(BytesIO(bomb)) |
Serhiy Storchaka | 5f1a518 | 2016-09-11 14:41:02 +0300 | [diff] [blame] | 577 | self.assertEqual(decomp.read(1), b'\0') |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 578 | max_decomp = 1 + DEFAULT_BUFFER_SIZE |
| 579 | self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp, |
| 580 | "Excessive amount of data was decompressed") |
| 581 | |
Nadeem Vawda | 10c8791 | 2012-06-20 01:48:50 +0200 | [diff] [blame] | 582 | |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 583 | # Tests for a BZ2File wrapping another file object: |
| 584 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 585 | def testReadBytesIO(self): |
Nadeem Vawda | 07d291a | 2011-05-30 01:44:45 +0200 | [diff] [blame] | 586 | with BytesIO(self.DATA) as bio: |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 587 | with BZ2File(bio) as bz2f: |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 588 | self.assertRaises(TypeError, bz2f.read, float()) |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 589 | self.assertEqual(bz2f.read(), self.TEXT) |
| 590 | self.assertFalse(bio.closed) |
| 591 | |
| 592 | def testPeekBytesIO(self): |
Nadeem Vawda | 07d291a | 2011-05-30 01:44:45 +0200 | [diff] [blame] | 593 | with BytesIO(self.DATA) as bio: |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 594 | with BZ2File(bio) as bz2f: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 595 | pdata = bz2f.peek() |
| 596 | self.assertNotEqual(len(pdata), 0) |
| 597 | self.assertTrue(self.TEXT.startswith(pdata)) |
| 598 | self.assertEqual(bz2f.read(), self.TEXT) |
| 599 | |
| 600 | def testWriteBytesIO(self): |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 601 | with BytesIO() as bio: |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 602 | with BZ2File(bio, "w") as bz2f: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 603 | self.assertRaises(TypeError, bz2f.write) |
| 604 | bz2f.write(self.TEXT) |
| 605 | self.assertEqual(self.decompress(bio.getvalue()), self.TEXT) |
| 606 | self.assertFalse(bio.closed) |
| 607 | |
| 608 | def testSeekForwardBytesIO(self): |
Nadeem Vawda | 07d291a | 2011-05-30 01:44:45 +0200 | [diff] [blame] | 609 | with BytesIO(self.DATA) as bio: |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 610 | with BZ2File(bio) as bz2f: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 611 | self.assertRaises(TypeError, bz2f.seek) |
| 612 | bz2f.seek(150) |
| 613 | self.assertEqual(bz2f.read(), self.TEXT[150:]) |
| 614 | |
| 615 | def testSeekBackwardsBytesIO(self): |
Nadeem Vawda | 07d291a | 2011-05-30 01:44:45 +0200 | [diff] [blame] | 616 | with BytesIO(self.DATA) as bio: |
Nadeem Vawda | aebcdba | 2012-06-04 23:31:20 +0200 | [diff] [blame] | 617 | with BZ2File(bio) as bz2f: |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 618 | bz2f.read(500) |
| 619 | bz2f.seek(-150, 1) |
| 620 | self.assertEqual(bz2f.read(), self.TEXT[500-150:]) |
Georg Brandl | 33a5f2a | 2005-08-21 14:16:04 +0000 | [diff] [blame] | 621 | |
Serhiy Storchaka | 7c3922f | 2013-01-22 17:01:59 +0200 | [diff] [blame] | 622 | def test_read_truncated(self): |
| 623 | # Drop the eos_magic field (6 bytes) and CRC (4 bytes). |
| 624 | truncated = self.DATA[:-10] |
Serhiy Storchaka | 57f9b7a | 2013-01-22 17:07:49 +0200 | [diff] [blame] | 625 | with BZ2File(BytesIO(truncated)) as f: |
Serhiy Storchaka | 7c3922f | 2013-01-22 17:01:59 +0200 | [diff] [blame] | 626 | self.assertRaises(EOFError, f.read) |
Serhiy Storchaka | 57f9b7a | 2013-01-22 17:07:49 +0200 | [diff] [blame] | 627 | with BZ2File(BytesIO(truncated)) as f: |
Serhiy Storchaka | 7c3922f | 2013-01-22 17:01:59 +0200 | [diff] [blame] | 628 | self.assertEqual(f.read(len(self.TEXT)), self.TEXT) |
| 629 | self.assertRaises(EOFError, f.read, 1) |
| 630 | # Incomplete 4-byte file header, and block header of at least 146 bits. |
| 631 | for i in range(22): |
Serhiy Storchaka | 57f9b7a | 2013-01-22 17:07:49 +0200 | [diff] [blame] | 632 | with BZ2File(BytesIO(truncated[:i])) as f: |
Serhiy Storchaka | 7c3922f | 2013-01-22 17:01:59 +0200 | [diff] [blame] | 633 | self.assertRaises(EOFError, f.read, 1) |
| 634 | |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 635 | |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 636 | class BZ2CompressorTest(BaseTest): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 637 | def testCompress(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 638 | bz2c = BZ2Compressor() |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 639 | self.assertRaises(TypeError, bz2c.compress) |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 640 | data = bz2c.compress(self.TEXT) |
| 641 | data += bz2c.flush() |
| 642 | self.assertEqual(self.decompress(data), self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 643 | |
Nadeem Vawda | 638fb9b | 2013-01-02 23:02:00 +0100 | [diff] [blame] | 644 | def testCompressEmptyString(self): |
| 645 | bz2c = BZ2Compressor() |
| 646 | data = bz2c.compress(b'') |
| 647 | data += bz2c.flush() |
| 648 | self.assertEqual(data, self.EMPTY_DATA) |
| 649 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 650 | def testCompressChunks10(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 651 | bz2c = BZ2Compressor() |
| 652 | n = 0 |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 653 | data = b'' |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 654 | while True: |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 655 | str = self.TEXT[n*10:(n+1)*10] |
| 656 | if not str: |
| 657 | break |
| 658 | data += bz2c.compress(str) |
| 659 | n += 1 |
| 660 | data += bz2c.flush() |
| 661 | self.assertEqual(self.decompress(data), self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | 1a3ff48 | 2011-10-04 10:28:37 +0200 | [diff] [blame] | 663 | @bigmemtest(size=_4G + 100, memuse=2) |
Nadeem Vawda | ea4b46f | 2011-04-12 23:02:42 +0200 | [diff] [blame] | 664 | def testCompress4G(self, size): |
| 665 | # "Test BZ2Compressor.compress()/flush() with >4GiB input" |
| 666 | bz2c = BZ2Compressor() |
| 667 | data = b"x" * size |
| 668 | try: |
| 669 | compressed = bz2c.compress(data) |
| 670 | compressed += bz2c.flush() |
| 671 | finally: |
| 672 | data = None # Release memory |
| 673 | data = bz2.decompress(compressed) |
| 674 | try: |
| 675 | self.assertEqual(len(data), size) |
| 676 | self.assertEqual(len(data.strip(b"x")), 0) |
| 677 | finally: |
| 678 | data = None |
| 679 | |
Nadeem Vawda | 3797065 | 2013-10-28 21:35:23 +0100 | [diff] [blame] | 680 | def testPickle(self): |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 681 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 682 | with self.assertRaises(TypeError): |
| 683 | pickle.dumps(BZ2Compressor(), proto) |
Nadeem Vawda | 3797065 | 2013-10-28 21:35:23 +0100 | [diff] [blame] | 684 | |
| 685 | |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 686 | class BZ2DecompressorTest(BaseTest): |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 687 | def test_Constructor(self): |
| 688 | self.assertRaises(TypeError, BZ2Decompressor, 42) |
| 689 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 690 | def testDecompress(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 691 | bz2d = BZ2Decompressor() |
Walter Dörwald | bb9c739 | 2004-11-01 17:10:19 +0000 | [diff] [blame] | 692 | self.assertRaises(TypeError, bz2d.decompress) |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 693 | text = bz2d.decompress(self.DATA) |
| 694 | self.assertEqual(text, self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 695 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 696 | def testDecompressChunks10(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 697 | bz2d = BZ2Decompressor() |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 698 | text = b'' |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 699 | n = 0 |
Nadeem Vawda | 98838ba | 2011-05-30 01:12:24 +0200 | [diff] [blame] | 700 | while True: |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 701 | str = self.DATA[n*10:(n+1)*10] |
| 702 | if not str: |
| 703 | break |
| 704 | text += bz2d.decompress(str) |
| 705 | n += 1 |
| 706 | self.assertEqual(text, self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 707 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 708 | def testDecompressUnusedData(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 709 | bz2d = BZ2Decompressor() |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 710 | unused_data = b"this is unused data" |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 711 | text = bz2d.decompress(self.DATA+unused_data) |
| 712 | self.assertEqual(text, self.TEXT) |
| 713 | self.assertEqual(bz2d.unused_data, unused_data) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 714 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 715 | def testEOFError(self): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 716 | bz2d = BZ2Decompressor() |
| 717 | text = bz2d.decompress(self.DATA) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 718 | self.assertRaises(EOFError, bz2d.decompress, b"anything") |
Nadeem Vawda | a1952d4 | 2012-10-28 15:20:48 +0100 | [diff] [blame] | 719 | self.assertRaises(EOFError, bz2d.decompress, b"") |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 720 | |
Serhiy Storchaka | 4847e4e | 2014-01-10 13:37:54 +0200 | [diff] [blame] | 721 | @bigmemtest(size=_4G + 100, memuse=3.3) |
Nadeem Vawda | ea4b46f | 2011-04-12 23:02:42 +0200 | [diff] [blame] | 722 | def testDecompress4G(self, size): |
| 723 | # "Test BZ2Decompressor.decompress() with >4GiB input" |
| 724 | blocksize = 10 * 1024 * 1024 |
| 725 | block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little') |
| 726 | try: |
| 727 | data = block * (size // blocksize + 1) |
| 728 | compressed = bz2.compress(data) |
| 729 | bz2d = BZ2Decompressor() |
| 730 | decompressed = bz2d.decompress(compressed) |
| 731 | self.assertTrue(decompressed == data) |
| 732 | finally: |
| 733 | data = None |
| 734 | compressed = None |
| 735 | decompressed = None |
| 736 | |
Nadeem Vawda | 3797065 | 2013-10-28 21:35:23 +0100 | [diff] [blame] | 737 | def testPickle(self): |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 738 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 739 | with self.assertRaises(TypeError): |
| 740 | pickle.dumps(BZ2Decompressor(), proto) |
Nadeem Vawda | 3797065 | 2013-10-28 21:35:23 +0100 | [diff] [blame] | 741 | |
Antoine Pitrou | e71258a | 2015-02-26 13:08:07 +0100 | [diff] [blame] | 742 | def testDecompressorChunksMaxsize(self): |
| 743 | bzd = BZ2Decompressor() |
| 744 | max_length = 100 |
| 745 | out = [] |
| 746 | |
| 747 | # Feed some input |
| 748 | len_ = len(self.BIG_DATA) - 64 |
| 749 | out.append(bzd.decompress(self.BIG_DATA[:len_], |
| 750 | max_length=max_length)) |
| 751 | self.assertFalse(bzd.needs_input) |
| 752 | self.assertEqual(len(out[-1]), max_length) |
| 753 | |
| 754 | # Retrieve more data without providing more input |
| 755 | out.append(bzd.decompress(b'', max_length=max_length)) |
| 756 | self.assertFalse(bzd.needs_input) |
| 757 | self.assertEqual(len(out[-1]), max_length) |
| 758 | |
| 759 | # Retrieve more data while providing more input |
| 760 | out.append(bzd.decompress(self.BIG_DATA[len_:], |
| 761 | max_length=max_length)) |
| 762 | self.assertLessEqual(len(out[-1]), max_length) |
| 763 | |
| 764 | # Retrieve remaining uncompressed data |
| 765 | while not bzd.eof: |
| 766 | out.append(bzd.decompress(b'', max_length=max_length)) |
| 767 | self.assertLessEqual(len(out[-1]), max_length) |
| 768 | |
| 769 | out = b"".join(out) |
| 770 | self.assertEqual(out, self.BIG_TEXT) |
| 771 | self.assertEqual(bzd.unused_data, b"") |
| 772 | |
| 773 | def test_decompressor_inputbuf_1(self): |
| 774 | # Test reusing input buffer after moving existing |
| 775 | # contents to beginning |
| 776 | bzd = BZ2Decompressor() |
| 777 | out = [] |
| 778 | |
| 779 | # Create input buffer and fill it |
| 780 | self.assertEqual(bzd.decompress(self.DATA[:100], |
| 781 | max_length=0), b'') |
| 782 | |
| 783 | # Retrieve some results, freeing capacity at beginning |
| 784 | # of input buffer |
| 785 | out.append(bzd.decompress(b'', 2)) |
| 786 | |
| 787 | # Add more data that fits into input buffer after |
| 788 | # moving existing data to beginning |
| 789 | out.append(bzd.decompress(self.DATA[100:105], 15)) |
| 790 | |
| 791 | # Decompress rest of data |
| 792 | out.append(bzd.decompress(self.DATA[105:])) |
| 793 | self.assertEqual(b''.join(out), self.TEXT) |
| 794 | |
| 795 | def test_decompressor_inputbuf_2(self): |
| 796 | # Test reusing input buffer by appending data at the |
| 797 | # end right away |
| 798 | bzd = BZ2Decompressor() |
| 799 | out = [] |
| 800 | |
| 801 | # Create input buffer and empty it |
| 802 | self.assertEqual(bzd.decompress(self.DATA[:200], |
| 803 | max_length=0), b'') |
| 804 | out.append(bzd.decompress(b'')) |
| 805 | |
| 806 | # Fill buffer with new data |
| 807 | out.append(bzd.decompress(self.DATA[200:280], 2)) |
| 808 | |
| 809 | # Append some more data, not enough to require resize |
| 810 | out.append(bzd.decompress(self.DATA[280:300], 2)) |
| 811 | |
| 812 | # Decompress rest of data |
| 813 | out.append(bzd.decompress(self.DATA[300:])) |
| 814 | self.assertEqual(b''.join(out), self.TEXT) |
| 815 | |
| 816 | def test_decompressor_inputbuf_3(self): |
| 817 | # Test reusing input buffer after extending it |
| 818 | |
| 819 | bzd = BZ2Decompressor() |
| 820 | out = [] |
| 821 | |
| 822 | # Create almost full input buffer |
| 823 | out.append(bzd.decompress(self.DATA[:200], 5)) |
| 824 | |
| 825 | # Add even more data to it, requiring resize |
| 826 | out.append(bzd.decompress(self.DATA[200:300], 5)) |
| 827 | |
| 828 | # Decompress rest of data |
| 829 | out.append(bzd.decompress(self.DATA[300:])) |
| 830 | self.assertEqual(b''.join(out), self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 831 | |
Martin Panter | 38317d3 | 2016-10-01 02:45:17 +0000 | [diff] [blame] | 832 | def test_failure(self): |
| 833 | bzd = BZ2Decompressor() |
| 834 | self.assertRaises(Exception, bzd.decompress, self.BAD_DATA * 30) |
| 835 | # Previously, a second call could crash due to internal inconsistency |
| 836 | self.assertRaises(Exception, bzd.decompress, self.BAD_DATA * 30) |
| 837 | |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 838 | class CompressDecompressTest(BaseTest): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 839 | def testCompress(self): |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 840 | data = bz2.compress(self.TEXT) |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 841 | self.assertEqual(self.decompress(data), self.TEXT) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 842 | |
Nadeem Vawda | 638fb9b | 2013-01-02 23:02:00 +0100 | [diff] [blame] | 843 | def testCompressEmptyString(self): |
| 844 | text = bz2.compress(b'') |
| 845 | self.assertEqual(text, self.EMPTY_DATA) |
| 846 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 847 | def testDecompress(self): |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 848 | text = bz2.decompress(self.DATA) |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 849 | self.assertEqual(text, self.TEXT) |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 850 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 851 | def testDecompressEmpty(self): |
Guido van Rossum | 522a6c6 | 2007-05-22 23:13:45 +0000 | [diff] [blame] | 852 | text = bz2.decompress(b"") |
| 853 | self.assertEqual(text, b"") |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 854 | |
Nadeem Vawda | 638fb9b | 2013-01-02 23:02:00 +0100 | [diff] [blame] | 855 | def testDecompressToEmptyString(self): |
| 856 | text = bz2.decompress(self.EMPTY_DATA) |
| 857 | self.assertEqual(text, b'') |
| 858 | |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 859 | def testDecompressIncomplete(self): |
Tim Peters | 499d09a | 2002-11-09 06:31:56 +0000 | [diff] [blame] | 860 | self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10]) |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 861 | |
Nadeem Vawda | 1de19ac | 2013-12-04 23:01:15 +0100 | [diff] [blame] | 862 | def testDecompressBadData(self): |
| 863 | self.assertRaises(OSError, bz2.decompress, self.BAD_DATA) |
| 864 | |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 865 | def testDecompressMultiStream(self): |
Nadeem Vawda | 55b4338 | 2011-05-27 01:52:15 +0200 | [diff] [blame] | 866 | text = bz2.decompress(self.DATA * 5) |
| 867 | self.assertEqual(text, self.TEXT * 5) |
| 868 | |
Nadeem Vawda | 1de19ac | 2013-12-04 23:01:15 +0100 | [diff] [blame] | 869 | def testDecompressTrailingJunk(self): |
| 870 | text = bz2.decompress(self.DATA + self.BAD_DATA) |
| 871 | self.assertEqual(text, self.TEXT) |
| 872 | |
| 873 | def testDecompressMultiStreamTrailingJunk(self): |
| 874 | text = bz2.decompress(self.DATA * 5 + self.BAD_DATA) |
| 875 | self.assertEqual(text, self.TEXT * 5) |
| 876 | |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 877 | |
| 878 | class OpenTest(BaseTest): |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 879 | "Test the open function." |
| 880 | |
| 881 | def open(self, *args, **kwargs): |
| 882 | return bz2.open(*args, **kwargs) |
| 883 | |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 884 | def test_binary_modes(self): |
Nadeem Vawda | 8a9e99c | 2013-10-19 00:11:06 +0200 | [diff] [blame] | 885 | for mode in ("wb", "xb"): |
| 886 | if mode == "xb": |
| 887 | unlink(self.filename) |
| 888 | with self.open(self.filename, mode) as f: |
| 889 | f.write(self.TEXT) |
| 890 | with open(self.filename, "rb") as f: |
| 891 | file_data = self.decompress(f.read()) |
| 892 | self.assertEqual(file_data, self.TEXT) |
| 893 | with self.open(self.filename, "rb") as f: |
| 894 | self.assertEqual(f.read(), self.TEXT) |
| 895 | with self.open(self.filename, "ab") as f: |
| 896 | f.write(self.TEXT) |
| 897 | with open(self.filename, "rb") as f: |
| 898 | file_data = self.decompress(f.read()) |
| 899 | self.assertEqual(file_data, self.TEXT * 2) |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 900 | |
| 901 | def test_implicit_binary_modes(self): |
| 902 | # Test implicit binary modes (no "b" or "t" in mode string). |
Nadeem Vawda | 8a9e99c | 2013-10-19 00:11:06 +0200 | [diff] [blame] | 903 | for mode in ("w", "x"): |
| 904 | if mode == "x": |
| 905 | unlink(self.filename) |
| 906 | with self.open(self.filename, mode) as f: |
| 907 | f.write(self.TEXT) |
| 908 | with open(self.filename, "rb") as f: |
| 909 | file_data = self.decompress(f.read()) |
| 910 | self.assertEqual(file_data, self.TEXT) |
| 911 | with self.open(self.filename, "r") as f: |
| 912 | self.assertEqual(f.read(), self.TEXT) |
| 913 | with self.open(self.filename, "a") as f: |
| 914 | f.write(self.TEXT) |
| 915 | with open(self.filename, "rb") as f: |
| 916 | file_data = self.decompress(f.read()) |
| 917 | self.assertEqual(file_data, self.TEXT * 2) |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 918 | |
| 919 | def test_text_modes(self): |
| 920 | text = self.TEXT.decode("ascii") |
| 921 | text_native_eol = text.replace("\n", os.linesep) |
Nadeem Vawda | 8a9e99c | 2013-10-19 00:11:06 +0200 | [diff] [blame] | 922 | for mode in ("wt", "xt"): |
| 923 | if mode == "xt": |
| 924 | unlink(self.filename) |
| 925 | with self.open(self.filename, mode) as f: |
| 926 | f.write(text) |
| 927 | with open(self.filename, "rb") as f: |
| 928 | file_data = self.decompress(f.read()).decode("ascii") |
| 929 | self.assertEqual(file_data, text_native_eol) |
| 930 | with self.open(self.filename, "rt") as f: |
| 931 | self.assertEqual(f.read(), text) |
| 932 | with self.open(self.filename, "at") as f: |
| 933 | f.write(text) |
| 934 | with open(self.filename, "rb") as f: |
| 935 | file_data = self.decompress(f.read()).decode("ascii") |
| 936 | self.assertEqual(file_data, text_native_eol * 2) |
| 937 | |
| 938 | def test_x_mode(self): |
| 939 | for mode in ("x", "xb", "xt"): |
| 940 | unlink(self.filename) |
| 941 | with self.open(self.filename, mode) as f: |
| 942 | pass |
| 943 | with self.assertRaises(FileExistsError): |
| 944 | with self.open(self.filename, mode) as f: |
| 945 | pass |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 946 | |
| 947 | def test_fileobj(self): |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 948 | with self.open(BytesIO(self.DATA), "r") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 949 | self.assertEqual(f.read(), self.TEXT) |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 950 | with self.open(BytesIO(self.DATA), "rb") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 951 | self.assertEqual(f.read(), self.TEXT) |
| 952 | text = self.TEXT.decode("ascii") |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 953 | with self.open(BytesIO(self.DATA), "rt") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 954 | self.assertEqual(f.read(), text) |
| 955 | |
| 956 | def test_bad_params(self): |
| 957 | # Test invalid parameter combinations. |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 958 | self.assertRaises(ValueError, |
| 959 | self.open, self.filename, "wbt") |
| 960 | self.assertRaises(ValueError, |
Nadeem Vawda | 8a9e99c | 2013-10-19 00:11:06 +0200 | [diff] [blame] | 961 | self.open, self.filename, "xbt") |
| 962 | self.assertRaises(ValueError, |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 963 | self.open, self.filename, "rb", encoding="utf-8") |
| 964 | self.assertRaises(ValueError, |
| 965 | self.open, self.filename, "rb", errors="ignore") |
| 966 | self.assertRaises(ValueError, |
| 967 | self.open, self.filename, "rb", newline="\n") |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 968 | |
| 969 | def test_encoding(self): |
| 970 | # Test non-default encoding. |
| 971 | text = self.TEXT.decode("ascii") |
| 972 | text_native_eol = text.replace("\n", os.linesep) |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 973 | with self.open(self.filename, "wt", encoding="utf-16-le") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 974 | f.write(text) |
| 975 | with open(self.filename, "rb") as f: |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 976 | file_data = self.decompress(f.read()).decode("utf-16-le") |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 977 | self.assertEqual(file_data, text_native_eol) |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 978 | with self.open(self.filename, "rt", encoding="utf-16-le") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 979 | self.assertEqual(f.read(), text) |
| 980 | |
| 981 | def test_encoding_error_handler(self): |
| 982 | # Test with non-default encoding error handler. |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 983 | with self.open(self.filename, "wb") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 984 | f.write(b"foo\xffbar") |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 985 | with self.open(self.filename, "rt", encoding="ascii", errors="ignore") \ |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 986 | as f: |
| 987 | self.assertEqual(f.read(), "foobar") |
| 988 | |
| 989 | def test_newline(self): |
| 990 | # Test with explicit newline (universal newline mode disabled). |
| 991 | text = self.TEXT.decode("ascii") |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 992 | with self.open(self.filename, "wt", newline="\n") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 993 | f.write(text) |
Nadeem Vawda | 3b4a4f5 | 2012-10-08 19:20:49 +0200 | [diff] [blame] | 994 | with self.open(self.filename, "rt", newline="\r") as f: |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 995 | self.assertEqual(f.readlines(), [text]) |
| 996 | |
| 997 | |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 998 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 999 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1000 | BZ2FileTest, |
| 1001 | BZ2CompressorTest, |
| 1002 | BZ2DecompressorTest, |
Nadeem Vawda | af518c1 | 2012-06-04 23:32:38 +0200 | [diff] [blame] | 1003 | CompressDecompressTest, |
| 1004 | OpenTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1005 | ) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1006 | support.reap_children() |
Gustavo Niemeyer | f8ca836 | 2002-11-05 16:50:05 +0000 | [diff] [blame] | 1007 | |
| 1008 | if __name__ == '__main__': |
| 1009 | test_main() |