Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 3 | import binascii |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 4 | import random |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 5 | import sys |
Victor Stinner | 8848c7a | 2011-01-04 02:07:36 +0000 | [diff] [blame] | 6 | from test.support import precisionbigmemtest, _1G, _4G |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 7 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 8 | zlib = support.import_module('zlib') |
| 9 | |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 10 | try: |
| 11 | import mmap |
| 12 | except ImportError: |
| 13 | mmap = None |
| 14 | |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 16 | class ChecksumTestCase(unittest.TestCase): |
| 17 | # checksum test cases |
| 18 | def test_crc32start(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 19 | self.assertEqual(zlib.crc32(b""), zlib.crc32(b"", 0)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 20 | self.assertTrue(zlib.crc32(b"abc", 0xffffffff)) |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 22 | def test_crc32empty(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 23 | self.assertEqual(zlib.crc32(b"", 0), 0) |
| 24 | self.assertEqual(zlib.crc32(b"", 1), 1) |
| 25 | self.assertEqual(zlib.crc32(b"", 432), 432) |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 27 | def test_adler32start(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 28 | self.assertEqual(zlib.adler32(b""), zlib.adler32(b"", 1)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 29 | self.assertTrue(zlib.adler32(b"abc", 0xffffffff)) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 31 | def test_adler32empty(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 32 | self.assertEqual(zlib.adler32(b"", 0), 0) |
| 33 | self.assertEqual(zlib.adler32(b"", 1), 1) |
| 34 | self.assertEqual(zlib.adler32(b"", 432), 432) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 36 | def assertEqual32(self, seen, expected): |
| 37 | # 32-bit values masked -- checksums on 32- vs 64- bit machines |
| 38 | # This is important if bit 31 (0x08000000L) is set. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 39 | self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 40 | |
| 41 | def test_penguins(self): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 42 | self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120) |
| 43 | self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94) |
| 44 | self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6) |
| 45 | self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 47 | self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0)) |
| 48 | self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 49 | |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 50 | def test_crc32_adler32_unsigned(self): |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 51 | foo = b'abcdefghijklmnop' |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 52 | # explicitly test signed behavior |
Gregory P. Smith | 2727503 | 2008-03-20 06:20:09 +0000 | [diff] [blame] | 53 | self.assertEqual(zlib.crc32(foo), 2486878355) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 54 | self.assertEqual(zlib.crc32(b'spam'), 1138425661) |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 55 | self.assertEqual(zlib.adler32(foo+foo), 3573550353) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 56 | self.assertEqual(zlib.adler32(b'spam'), 72286642) |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 57 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 58 | def test_same_as_binascii_crc32(self): |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 59 | foo = b'abcdefghijklmnop' |
Gregory P. Smith | 2727503 | 2008-03-20 06:20:09 +0000 | [diff] [blame] | 60 | crc = 2486878355 |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 61 | self.assertEqual(binascii.crc32(foo), crc) |
| 62 | self.assertEqual(zlib.crc32(foo), crc) |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 63 | self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam')) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 64 | |
| 65 | |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 66 | # Issue #10276 - check that inputs >=4GB are handled correctly. |
| 67 | class ChecksumBigBufferTestCase(unittest.TestCase): |
| 68 | |
| 69 | def setUp(self): |
| 70 | with open(support.TESTFN, "wb+") as f: |
| 71 | f.seek(_4G) |
| 72 | f.write(b"asdf") |
Victor Stinner | a6cd0cf | 2011-05-02 01:05:37 +0200 | [diff] [blame] | 73 | f.flush() |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 74 | self.mapping = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) |
| 75 | |
| 76 | def tearDown(self): |
| 77 | self.mapping.close() |
| 78 | support.unlink(support.TESTFN) |
| 79 | |
| 80 | @unittest.skipUnless(mmap, "mmap() is not available.") |
| 81 | @unittest.skipUnless(sys.maxsize > _4G, "Can't run on a 32-bit system.") |
| 82 | @unittest.skipUnless(support.is_resource_enabled("largefile"), |
| 83 | "May use lots of disk space.") |
| 84 | def test_big_buffer(self): |
| 85 | self.assertEqual(zlib.crc32(self.mapping), 3058686908) |
| 86 | self.assertEqual(zlib.adler32(self.mapping), 82837919) |
| 87 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 89 | class ExceptionTestCase(unittest.TestCase): |
| 90 | # make sure we generate some expected errors |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 91 | def test_badlevel(self): |
| 92 | # specifying compression level out of range causes an error |
| 93 | # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib |
| 94 | # accepts 0 too) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 95 | self.assertRaises(zlib.error, zlib.compress, b'ERROR', 10) |
| 96 | |
| 97 | def test_badargs(self): |
| 98 | self.assertRaises(TypeError, zlib.adler32) |
| 99 | self.assertRaises(TypeError, zlib.crc32) |
| 100 | self.assertRaises(TypeError, zlib.compress) |
| 101 | self.assertRaises(TypeError, zlib.decompress) |
| 102 | for arg in (42, None, '', 'abc', (), []): |
| 103 | self.assertRaises(TypeError, zlib.adler32, arg) |
| 104 | self.assertRaises(TypeError, zlib.crc32, arg) |
| 105 | self.assertRaises(TypeError, zlib.compress, arg) |
| 106 | self.assertRaises(TypeError, zlib.decompress, arg) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 107 | |
| 108 | def test_badcompressobj(self): |
| 109 | # verify failure on building compress object with bad params |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 110 | self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 111 | # specifying total bits too large causes an error |
| 112 | self.assertRaises(ValueError, |
| 113 | zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 114 | |
| 115 | def test_baddecompressobj(self): |
| 116 | # verify failure on building decompress object with bad params |
Antoine Pitrou | 90ee4df | 2010-04-06 17:23:13 +0000 | [diff] [blame] | 117 | self.assertRaises(ValueError, zlib.decompressobj, -1) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 118 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 119 | def test_decompressobj_badflush(self): |
| 120 | # verify failure on calling decompressobj.flush with bad params |
| 121 | self.assertRaises(ValueError, zlib.decompressobj().flush, 0) |
| 122 | self.assertRaises(ValueError, zlib.decompressobj().flush, -1) |
| 123 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 124 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 125 | class BaseCompressTestCase(object): |
| 126 | def check_big_compress_buffer(self, size, compress_func): |
| 127 | _1M = 1024 * 1024 |
| 128 | fmt = "%%0%dx" % (2 * _1M) |
| 129 | # Generate 10MB worth of random, and expand it by repeating it. |
| 130 | # The assumption is that zlib's memory is not big enough to exploit |
| 131 | # such spread out redundancy. |
| 132 | data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little') |
| 133 | for i in range(10)]) |
| 134 | data = data * (size // len(data) + 1) |
| 135 | try: |
| 136 | compress_func(data) |
| 137 | finally: |
| 138 | # Release memory |
| 139 | data = None |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 141 | def check_big_decompress_buffer(self, size, decompress_func): |
| 142 | data = b'x' * size |
| 143 | try: |
| 144 | compressed = zlib.compress(data, 1) |
| 145 | finally: |
| 146 | # Release memory |
| 147 | data = None |
| 148 | data = decompress_func(compressed) |
| 149 | # Sanity check |
| 150 | try: |
| 151 | self.assertEqual(len(data), size) |
| 152 | self.assertEqual(len(data.strip(b'x')), 0) |
| 153 | finally: |
| 154 | data = None |
| 155 | |
| 156 | |
| 157 | class CompressTestCase(BaseCompressTestCase, unittest.TestCase): |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 158 | # Test compression in one go (whole message compression) |
| 159 | def test_speech(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 160 | x = zlib.compress(HAMLET_SCENE) |
| 161 | self.assertEqual(zlib.decompress(x), HAMLET_SCENE) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 162 | |
| 163 | def test_speech128(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 164 | # compress more data |
| 165 | data = HAMLET_SCENE * 128 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 166 | x = zlib.compress(data) |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 167 | self.assertEqual(zlib.compress(bytearray(data)), x) |
| 168 | for ob in x, bytearray(x): |
| 169 | self.assertEqual(zlib.decompress(ob), data) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | 53b2166 | 2010-05-11 23:46:02 +0000 | [diff] [blame] | 171 | def test_incomplete_stream(self): |
| 172 | # An useful error message is given |
| 173 | x = zlib.compress(HAMLET_SCENE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 174 | self.assertRaisesRegex(zlib.error, |
Antoine Pitrou | 53b2166 | 2010-05-11 23:46:02 +0000 | [diff] [blame] | 175 | "Error -5 while decompressing data: incomplete or truncated stream", |
| 176 | zlib.decompress, x[:-1]) |
| 177 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 178 | # Memory use of the following functions takes into account overallocation |
| 179 | |
| 180 | @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3) |
| 181 | def test_big_compress_buffer(self, size): |
| 182 | compress = lambda s: zlib.compress(s, 1) |
| 183 | self.check_big_compress_buffer(size, compress) |
| 184 | |
| 185 | @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=2) |
| 186 | def test_big_decompress_buffer(self, size): |
| 187 | self.check_big_decompress_buffer(size, zlib.decompress) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 188 | |
Victor Stinner | 8848c7a | 2011-01-04 02:07:36 +0000 | [diff] [blame] | 189 | @precisionbigmemtest(size=_4G + 100, memuse=1) |
| 190 | def test_length_overflow(self, size): |
| 191 | if size < _4G + 100: |
| 192 | self.skipTest("not enough free memory, need at least 4 GB") |
| 193 | data = b'x' * size |
| 194 | try: |
| 195 | self.assertRaises(OverflowError, zlib.compress, data, 1) |
Nadeem Vawda | 154bdf9 | 2011-05-14 23:07:36 +0200 | [diff] [blame] | 196 | self.assertRaises(OverflowError, zlib.decompress, data) |
Victor Stinner | 8848c7a | 2011-01-04 02:07:36 +0000 | [diff] [blame] | 197 | finally: |
| 198 | data = None |
| 199 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 201 | class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase): |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 202 | # Test compression object |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 203 | def test_pair(self): |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 204 | # straightforward compress/decompress objects |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 205 | datasrc = HAMLET_SCENE * 128 |
| 206 | datazip = zlib.compress(datasrc) |
| 207 | # should compress both bytes and bytearray data |
| 208 | for data in (datasrc, bytearray(datasrc)): |
| 209 | co = zlib.compressobj() |
| 210 | x1 = co.compress(data) |
| 211 | x2 = co.flush() |
| 212 | self.assertRaises(zlib.error, co.flush) # second flush should not work |
| 213 | self.assertEqual(x1 + x2, datazip) |
| 214 | for v1, v2 in ((x1, x2), (bytearray(x1), bytearray(x2))): |
| 215 | dco = zlib.decompressobj() |
| 216 | y1 = dco.decompress(v1 + v2) |
| 217 | y2 = dco.flush() |
| 218 | self.assertEqual(data, y1 + y2) |
| 219 | self.assertIsInstance(dco.unconsumed_tail, bytes) |
| 220 | self.assertIsInstance(dco.unused_data, bytes) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 221 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 222 | def test_compressoptions(self): |
| 223 | # specify lots of options to compressobj() |
| 224 | level = 2 |
| 225 | method = zlib.DEFLATED |
| 226 | wbits = -12 |
| 227 | memlevel = 9 |
| 228 | strategy = zlib.Z_FILTERED |
| 229 | co = zlib.compressobj(level, method, wbits, memlevel, strategy) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 230 | x1 = co.compress(HAMLET_SCENE) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 231 | x2 = co.flush() |
| 232 | dco = zlib.decompressobj(wbits) |
| 233 | y1 = dco.decompress(x1 + x2) |
| 234 | y2 = dco.flush() |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 235 | self.assertEqual(HAMLET_SCENE, y1 + y2) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 237 | def test_compressincremental(self): |
| 238 | # compress object in steps, decompress object as one-shot |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 239 | data = HAMLET_SCENE * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 240 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 241 | bufs = [] |
| 242 | for i in range(0, len(data), 256): |
| 243 | bufs.append(co.compress(data[i:i+256])) |
| 244 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 245 | combuf = b''.join(bufs) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 246 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 247 | dco = zlib.decompressobj() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 248 | y1 = dco.decompress(b''.join(bufs)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 249 | y2 = dco.flush() |
| 250 | self.assertEqual(data, y1 + y2) |
| 251 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 252 | def test_decompinc(self, flush=False, source=None, cx=256, dcx=64): |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 253 | # compress object in steps, decompress object in steps |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 254 | source = source or HAMLET_SCENE |
| 255 | data = source * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 256 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 257 | bufs = [] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 258 | for i in range(0, len(data), cx): |
| 259 | bufs.append(co.compress(data[i:i+cx])) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 260 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 261 | combuf = b''.join(bufs) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 262 | |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 263 | decombuf = zlib.decompress(combuf) |
| 264 | # Test type of return value |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 265 | self.assertIsInstance(decombuf, bytes) |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 266 | |
| 267 | self.assertEqual(data, decombuf) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 268 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 269 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 270 | bufs = [] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 271 | for i in range(0, len(combuf), dcx): |
| 272 | bufs.append(dco.decompress(combuf[i:i+dcx])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 273 | self.assertEqual(b'', dco.unconsumed_tail, ######## |
| 274 | "(A) uct should be b'': not %d long" % |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 275 | len(dco.unconsumed_tail)) |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 276 | self.assertEqual(b'', dco.unused_data) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 277 | if flush: |
| 278 | bufs.append(dco.flush()) |
| 279 | else: |
| 280 | while True: |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 281 | chunk = dco.decompress(b'') |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 282 | if chunk: |
| 283 | bufs.append(chunk) |
| 284 | else: |
| 285 | break |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 286 | self.assertEqual(b'', dco.unconsumed_tail, ######## |
| 287 | "(B) uct should be b'': not %d long" % |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 288 | len(dco.unconsumed_tail)) |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 289 | self.assertEqual(b'', dco.unused_data) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 290 | self.assertEqual(data, b''.join(bufs)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 291 | # Failure means: "decompressobj with init options failed" |
| 292 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 293 | def test_decompincflush(self): |
| 294 | self.test_decompinc(flush=True) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 295 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 296 | def test_decompimax(self, source=None, cx=256, dcx=64): |
| 297 | # compress in steps, decompress in length-restricted steps |
| 298 | source = source or HAMLET_SCENE |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 299 | # Check a decompression object with max_length specified |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 300 | data = source * 128 |
| 301 | co = zlib.compressobj() |
| 302 | bufs = [] |
| 303 | for i in range(0, len(data), cx): |
| 304 | bufs.append(co.compress(data[i:i+cx])) |
| 305 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 306 | combuf = b''.join(bufs) |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 307 | self.assertEqual(data, zlib.decompress(combuf), |
| 308 | 'compressed data failure') |
| 309 | |
| 310 | dco = zlib.decompressobj() |
| 311 | bufs = [] |
| 312 | cb = combuf |
| 313 | while cb: |
| 314 | #max_length = 1 + len(cb)//10 |
| 315 | chunk = dco.decompress(cb, dcx) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 316 | self.assertFalse(len(chunk) > dcx, |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 317 | 'chunk too big (%d>%d)' % (len(chunk), dcx)) |
| 318 | bufs.append(chunk) |
| 319 | cb = dco.unconsumed_tail |
| 320 | bufs.append(dco.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 321 | self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved') |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 322 | |
| 323 | def test_decompressmaxlen(self, flush=False): |
| 324 | # Check a decompression object with max_length specified |
| 325 | data = HAMLET_SCENE * 128 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 326 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 327 | bufs = [] |
| 328 | for i in range(0, len(data), 256): |
| 329 | bufs.append(co.compress(data[i:i+256])) |
| 330 | bufs.append(co.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 331 | combuf = b''.join(bufs) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 332 | self.assertEqual(data, zlib.decompress(combuf), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 333 | 'compressed data failure') |
| 334 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 335 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 336 | bufs = [] |
| 337 | cb = combuf |
| 338 | while cb: |
Guido van Rossum | f359410 | 2003-02-27 18:39:18 +0000 | [diff] [blame] | 339 | max_length = 1 + len(cb)//10 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 340 | chunk = dco.decompress(cb, max_length) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 341 | self.assertFalse(len(chunk) > max_length, |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 342 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 343 | bufs.append(chunk) |
| 344 | cb = dco.unconsumed_tail |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 345 | if flush: |
| 346 | bufs.append(dco.flush()) |
| 347 | else: |
| 348 | while chunk: |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 349 | chunk = dco.decompress(b'', max_length) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 350 | self.assertFalse(len(chunk) > max_length, |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 351 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 352 | bufs.append(chunk) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 353 | self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved') |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 354 | |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 355 | def test_decompressmaxlenflush(self): |
| 356 | self.test_decompressmaxlen(flush=True) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 357 | |
| 358 | def test_maxlenmisc(self): |
| 359 | # Misc tests of max_length |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame] | 360 | dco = zlib.decompressobj() |
Antoine Pitrou | 77b338b | 2009-12-14 18:00:06 +0000 | [diff] [blame] | 361 | self.assertRaises(ValueError, dco.decompress, b"", -1) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 362 | self.assertEqual(b'', dco.unconsumed_tail) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 363 | |
Nadeem Vawda | 7619e88 | 2011-05-14 14:05:20 +0200 | [diff] [blame] | 364 | def test_clear_unconsumed_tail(self): |
| 365 | # Issue #12050: calling decompress() without providing max_length |
| 366 | # should clear the unconsumed_tail attribute. |
| 367 | cdata = b"x\x9cKLJ\x06\x00\x02M\x01" # "abc" |
| 368 | dco = zlib.decompressobj() |
| 369 | ddata = dco.decompress(cdata, 1) |
| 370 | ddata += dco.decompress(dco.unconsumed_tail) |
| 371 | self.assertEqual(dco.unconsumed_tail, b"") |
| 372 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 373 | def test_flushes(self): |
| 374 | # Test flush() with the various options, using all the |
| 375 | # different levels in order to provide more variations. |
| 376 | sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH'] |
| 377 | sync_opt = [getattr(zlib, opt) for opt in sync_opt |
| 378 | if hasattr(zlib, opt)] |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 379 | data = HAMLET_SCENE * 8 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 380 | |
| 381 | for sync in sync_opt: |
| 382 | for level in range(10): |
| 383 | obj = zlib.compressobj( level ) |
| 384 | a = obj.compress( data[:3000] ) |
| 385 | b = obj.flush( sync ) |
| 386 | c = obj.compress( data[3000:] ) |
| 387 | d = obj.flush() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 388 | self.assertEqual(zlib.decompress(b''.join([a,b,c,d])), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 389 | data, ("Decompress failed: flush " |
| 390 | "mode=%i, level=%i") % (sync, level)) |
| 391 | del obj |
| 392 | |
| 393 | def test_odd_flush(self): |
| 394 | # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1 |
| 395 | import random |
| 396 | |
| 397 | if hasattr(zlib, 'Z_SYNC_FLUSH'): |
| 398 | # Testing on 17K of "random" data |
| 399 | |
| 400 | # Create compressor and decompressor objects |
Neil Schemenauer | 6412b12 | 2004-06-05 19:34:28 +0000 | [diff] [blame] | 401 | co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 402 | dco = zlib.decompressobj() |
| 403 | |
| 404 | # Try 17K of data |
| 405 | # generate random data stream |
| 406 | try: |
| 407 | # In 2.3 and later, WichmannHill is the RNG of the bug report |
| 408 | gen = random.WichmannHill() |
| 409 | except AttributeError: |
| 410 | try: |
| 411 | # 2.2 called it Random |
| 412 | gen = random.Random() |
| 413 | except AttributeError: |
| 414 | # others might simply have a single RNG |
| 415 | gen = random |
| 416 | gen.seed(1) |
| 417 | data = genblock(1, 17 * 1024, generator=gen) |
| 418 | |
| 419 | # compress, sync-flush, and decompress |
| 420 | first = co.compress(data) |
| 421 | second = co.flush(zlib.Z_SYNC_FLUSH) |
| 422 | expanded = dco.decompress(first + second) |
| 423 | |
| 424 | # if decompressed data is different from the input data, choke. |
| 425 | self.assertEqual(expanded, data, "17K random source doesn't match") |
| 426 | |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 427 | def test_empty_flush(self): |
| 428 | # Test that calling .flush() on unused objects works. |
| 429 | # (Bug #1083110 -- calling .flush() on decompress objects |
| 430 | # caused a core dump.) |
| 431 | |
| 432 | co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 433 | self.assertTrue(co.flush()) # Returns a zlib header |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 434 | dco = zlib.decompressobj() |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 435 | self.assertEqual(dco.flush(), b"") # Returns nothing |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | c09c92f | 2010-05-11 23:36:40 +0000 | [diff] [blame] | 437 | def test_decompress_incomplete_stream(self): |
| 438 | # This is 'foo', deflated |
| 439 | x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' |
| 440 | # For the record |
| 441 | self.assertEqual(zlib.decompress(x), b'foo') |
| 442 | self.assertRaises(zlib.error, zlib.decompress, x[:-5]) |
| 443 | # Omitting the stream end works with decompressor objects |
| 444 | # (see issue #8672). |
| 445 | dco = zlib.decompressobj() |
| 446 | y = dco.decompress(x[:-5]) |
| 447 | y += dco.flush() |
| 448 | self.assertEqual(y, b'foo') |
| 449 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 450 | if hasattr(zlib.compressobj(), "copy"): |
| 451 | def test_compresscopy(self): |
| 452 | # Test copying a compression object |
| 453 | data0 = HAMLET_SCENE |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 454 | data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 455 | c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION) |
| 456 | bufs0 = [] |
| 457 | bufs0.append(c0.compress(data0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 458 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 459 | c1 = c0.copy() |
| 460 | bufs1 = bufs0[:] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 461 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 462 | bufs0.append(c0.compress(data0)) |
| 463 | bufs0.append(c0.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 464 | s0 = b''.join(bufs0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 465 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 466 | bufs1.append(c1.compress(data1)) |
| 467 | bufs1.append(c1.flush()) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 468 | s1 = b''.join(bufs1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 469 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 470 | self.assertEqual(zlib.decompress(s0),data0+data0) |
| 471 | self.assertEqual(zlib.decompress(s1),data0+data1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 472 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 473 | def test_badcompresscopy(self): |
| 474 | # Test copying a compression object in an inconsistent state |
| 475 | c = zlib.compressobj() |
| 476 | c.compress(HAMLET_SCENE) |
| 477 | c.flush() |
| 478 | self.assertRaises(ValueError, c.copy) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 480 | if hasattr(zlib.decompressobj(), "copy"): |
| 481 | def test_decompresscopy(self): |
| 482 | # Test copying a decompression object |
| 483 | data = HAMLET_SCENE |
| 484 | comp = zlib.compress(data) |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 485 | # Test type of return value |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 486 | self.assertIsInstance(comp, bytes) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 487 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 488 | d0 = zlib.decompressobj() |
| 489 | bufs0 = [] |
| 490 | bufs0.append(d0.decompress(comp[:32])) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 491 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 492 | d1 = d0.copy() |
| 493 | bufs1 = bufs0[:] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 494 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 495 | bufs0.append(d0.decompress(comp[32:])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 496 | s0 = b''.join(bufs0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 497 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 498 | bufs1.append(d1.decompress(comp[32:])) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 499 | s1 = b''.join(bufs1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 500 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 501 | self.assertEqual(s0,s1) |
| 502 | self.assertEqual(s0,data) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 503 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 504 | def test_baddecompresscopy(self): |
| 505 | # Test copying a compression object in an inconsistent state |
| 506 | data = zlib.compress(HAMLET_SCENE) |
| 507 | d = zlib.decompressobj() |
| 508 | d.decompress(data) |
| 509 | d.flush() |
| 510 | self.assertRaises(ValueError, d.copy) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 511 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 512 | # Memory use of the following functions takes into account overallocation |
| 513 | |
| 514 | @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3) |
| 515 | def test_big_compress_buffer(self, size): |
| 516 | c = zlib.compressobj(1) |
| 517 | compress = lambda s: c.compress(s) + c.flush() |
| 518 | self.check_big_compress_buffer(size, compress) |
| 519 | |
| 520 | @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=2) |
| 521 | def test_big_decompress_buffer(self, size): |
| 522 | d = zlib.decompressobj() |
| 523 | decompress = lambda s: d.decompress(s) + d.flush() |
| 524 | self.check_big_decompress_buffer(size, decompress) |
| 525 | |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 526 | @precisionbigmemtest(size=_4G + 100, memuse=1) |
| 527 | def test_length_overflow(self, size): |
| 528 | if size < _4G + 100: |
| 529 | self.skipTest("not enough free memory, need at least 4 GB") |
| 530 | data = b'x' * size |
Nadeem Vawda | 1161a9c | 2011-05-15 00:48:24 +0200 | [diff] [blame] | 531 | c = zlib.compressobj(1) |
| 532 | d = zlib.decompressobj() |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 533 | try: |
Nadeem Vawda | 1161a9c | 2011-05-15 00:48:24 +0200 | [diff] [blame] | 534 | self.assertRaises(OverflowError, c.compress, data) |
| 535 | self.assertRaises(OverflowError, d.decompress, data) |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 536 | finally: |
| 537 | data = None |
| 538 | |
Antoine Pitrou | 8956271 | 2010-05-07 17:04:02 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 540 | def genblock(seed, length, step=1024, generator=random): |
| 541 | """length-byte stream of random data from a seed (in step-byte blocks).""" |
| 542 | if seed is not None: |
| 543 | generator.seed(seed) |
| 544 | randint = generator.randint |
| 545 | if length < step or step < 2: |
| 546 | step = length |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 547 | blocks = bytes() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 548 | for i in range(0, length, step): |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 549 | blocks += bytes(randint(0, 255) for x in range(step)) |
| 550 | return blocks |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 551 | |
| 552 | |
| 553 | |
| 554 | def choose_lines(source, number, seed=None, generator=random): |
| 555 | """Return a list of number lines randomly chosen from the source""" |
| 556 | if seed is not None: |
| 557 | generator.seed(seed) |
| 558 | sources = source.split('\n') |
| 559 | return [generator.choice(sources) for n in range(number)] |
| 560 | |
| 561 | |
| 562 | |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 563 | HAMLET_SCENE = b""" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 564 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 565 | |
| 566 | O, fear me not. |
| 567 | I stay too long: but here my father comes. |
| 568 | |
| 569 | Enter POLONIUS |
| 570 | |
| 571 | A double blessing is a double grace, |
| 572 | Occasion smiles upon a second leave. |
| 573 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 574 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 575 | |
| 576 | Yet here, Laertes! aboard, aboard, for shame! |
| 577 | The wind sits in the shoulder of your sail, |
| 578 | And you are stay'd for. There; my blessing with thee! |
| 579 | And these few precepts in thy memory |
| 580 | See thou character. Give thy thoughts no tongue, |
| 581 | Nor any unproportioned thought his act. |
| 582 | Be thou familiar, but by no means vulgar. |
| 583 | Those friends thou hast, and their adoption tried, |
| 584 | Grapple them to thy soul with hoops of steel; |
| 585 | But do not dull thy palm with entertainment |
| 586 | Of each new-hatch'd, unfledged comrade. Beware |
| 587 | Of entrance to a quarrel, but being in, |
| 588 | Bear't that the opposed may beware of thee. |
| 589 | Give every man thy ear, but few thy voice; |
| 590 | Take each man's censure, but reserve thy judgment. |
| 591 | Costly thy habit as thy purse can buy, |
| 592 | But not express'd in fancy; rich, not gaudy; |
| 593 | For the apparel oft proclaims the man, |
| 594 | And they in France of the best rank and station |
| 595 | Are of a most select and generous chief in that. |
| 596 | Neither a borrower nor a lender be; |
| 597 | For loan oft loses both itself and friend, |
| 598 | And borrowing dulls the edge of husbandry. |
| 599 | This above all: to thine ownself be true, |
| 600 | And it must follow, as the night the day, |
| 601 | Thou canst not then be false to any man. |
| 602 | Farewell: my blessing season this in thee! |
| 603 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 604 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 605 | |
| 606 | Most humbly do I take my leave, my lord. |
| 607 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 608 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 609 | |
| 610 | The time invites you; go; your servants tend. |
| 611 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 612 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 613 | |
| 614 | Farewell, Ophelia; and remember well |
| 615 | What I have said to you. |
| 616 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 617 | OPHELIA |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 618 | |
| 619 | 'Tis in my memory lock'd, |
| 620 | And you yourself shall keep the key of it. |
| 621 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 622 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 623 | |
| 624 | Farewell. |
| 625 | """ |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 626 | |
| 627 | |
| 628 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 629 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 630 | ChecksumTestCase, |
Antoine Pitrou | f3d2275 | 2011-02-21 18:09:00 +0000 | [diff] [blame] | 631 | ChecksumBigBufferTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 632 | ExceptionTestCase, |
| 633 | CompressTestCase, |
| 634 | CompressObjectTestCase |
| 635 | ) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 636 | |
| 637 | if __name__ == "__main__": |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 638 | unittest.main() # XXX |
| 639 | ###test_main() |