blob: bb9292bac4501c85b0eb401aa63c5b047e91873d [file] [log] [blame]
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00003import binascii
Serhiy Storchakad7a44152015-11-12 11:23:04 +02004import pickle
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00005import random
Antoine Pitrouf3d22752011-02-21 18:09:00 +00006import sys
Antoine Pitrou94190bb2011-10-04 10:22:36 +02007from test.support import bigmemtest, _1G, _4G
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00008
R. David Murraya21e4ca2009-03-31 23:16:50 +00009zlib = support.import_module('zlib')
10
Serhiy Storchaka43767632013-11-03 21:31:38 +020011requires_Compress_copy = unittest.skipUnless(
12 hasattr(zlib.compressobj(), "copy"),
13 'requires Compress.copy()')
14requires_Decompress_copy = unittest.skipUnless(
15 hasattr(zlib.decompressobj(), "copy"),
16 'requires Decompress.copy()')
17
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000018
Nadeem Vawda64d25dd2011-09-12 00:04:13 +020019class VersionTestCase(unittest.TestCase):
20
21 def test_library_version(self):
Nadeem Vawda131c7072012-01-25 23:16:50 +020022 # Test that the major version of the actual library in use matches the
23 # major version that we were compiled against. We can't guarantee that
24 # the minor versions will match (even on the machine on which the module
25 # was compiled), and the API is stable between minor versions, so
Nadeem Vawdad770fe42012-01-28 17:32:47 +020026 # testing only the major versions avoids spurious failures.
Nadeem Vawda131c7072012-01-25 23:16:50 +020027 self.assertEqual(zlib.ZLIB_RUNTIME_VERSION[0], zlib.ZLIB_VERSION[0])
Nadeem Vawda64d25dd2011-09-12 00:04:13 +020028
29
Guido van Rossum7d9ea502003-02-03 20:45:52 +000030class ChecksumTestCase(unittest.TestCase):
31 # checksum test cases
32 def test_crc32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000033 self.assertEqual(zlib.crc32(b""), zlib.crc32(b"", 0))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000034 self.assertTrue(zlib.crc32(b"abc", 0xffffffff))
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +000035
Guido van Rossum7d9ea502003-02-03 20:45:52 +000036 def test_crc32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000037 self.assertEqual(zlib.crc32(b"", 0), 0)
38 self.assertEqual(zlib.crc32(b"", 1), 1)
39 self.assertEqual(zlib.crc32(b"", 432), 432)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000040
Guido van Rossum7d9ea502003-02-03 20:45:52 +000041 def test_adler32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000042 self.assertEqual(zlib.adler32(b""), zlib.adler32(b"", 1))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000043 self.assertTrue(zlib.adler32(b"abc", 0xffffffff))
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000044
Guido van Rossum7d9ea502003-02-03 20:45:52 +000045 def test_adler32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000046 self.assertEqual(zlib.adler32(b"", 0), 0)
47 self.assertEqual(zlib.adler32(b"", 1), 1)
48 self.assertEqual(zlib.adler32(b"", 432), 432)
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000049
Guido van Rossum7d9ea502003-02-03 20:45:52 +000050 def test_penguins(self):
Martin Panterb82032f2015-12-11 05:19:29 +000051 self.assertEqual(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
52 self.assertEqual(zlib.crc32(b"penguin", 1), 0x43b6aa94)
53 self.assertEqual(zlib.adler32(b"penguin", 0), 0x0bcf02f6)
54 self.assertEqual(zlib.adler32(b"penguin", 1), 0x0bd602f7)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000055
Guido van Rossum776152b2007-05-22 22:44:07 +000056 self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0))
57 self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000058
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000059 def test_crc32_adler32_unsigned(self):
Antoine Pitrou77b338b2009-12-14 18:00:06 +000060 foo = b'abcdefghijklmnop'
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000061 # explicitly test signed behavior
Gregory P. Smith27275032008-03-20 06:20:09 +000062 self.assertEqual(zlib.crc32(foo), 2486878355)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000063 self.assertEqual(zlib.crc32(b'spam'), 1138425661)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000064 self.assertEqual(zlib.adler32(foo+foo), 3573550353)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000065 self.assertEqual(zlib.adler32(b'spam'), 72286642)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000066
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000067 def test_same_as_binascii_crc32(self):
Martin v. Löwis15b16a32008-12-02 06:00:15 +000068 foo = b'abcdefghijklmnop'
Gregory P. Smith27275032008-03-20 06:20:09 +000069 crc = 2486878355
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000070 self.assertEqual(binascii.crc32(foo), crc)
71 self.assertEqual(zlib.crc32(foo), crc)
Martin v. Löwis15b16a32008-12-02 06:00:15 +000072 self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam'))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000073
74
Antoine Pitrouf3d22752011-02-21 18:09:00 +000075# Issue #10276 - check that inputs >=4GB are handled correctly.
76class ChecksumBigBufferTestCase(unittest.TestCase):
77
Nadeem Vawdabc8c8172012-02-23 14:16:15 +020078 @bigmemtest(size=_4G + 4, memuse=1, dry_run=False)
79 def test_big_buffer(self, size):
Nadeem Vawdab063a482012-02-23 13:36:25 +020080 data = b"nyan" * (_1G + 1)
81 self.assertEqual(zlib.crc32(data), 1044521549)
82 self.assertEqual(zlib.adler32(data), 2256789997)
Antoine Pitrouf3d22752011-02-21 18:09:00 +000083
Christian Heimesb186d002008-03-18 15:15:01 +000084
Guido van Rossum7d9ea502003-02-03 20:45:52 +000085class ExceptionTestCase(unittest.TestCase):
86 # make sure we generate some expected errors
Guido van Rossum8ce8a782007-11-01 19:42:39 +000087 def test_badlevel(self):
88 # specifying compression level out of range causes an error
89 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
90 # accepts 0 too)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000091 self.assertRaises(zlib.error, zlib.compress, b'ERROR', 10)
92
93 def test_badargs(self):
94 self.assertRaises(TypeError, zlib.adler32)
95 self.assertRaises(TypeError, zlib.crc32)
96 self.assertRaises(TypeError, zlib.compress)
97 self.assertRaises(TypeError, zlib.decompress)
98 for arg in (42, None, '', 'abc', (), []):
99 self.assertRaises(TypeError, zlib.adler32, arg)
100 self.assertRaises(TypeError, zlib.crc32, arg)
101 self.assertRaises(TypeError, zlib.compress, arg)
102 self.assertRaises(TypeError, zlib.decompress, arg)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000103
104 def test_badcompressobj(self):
105 # verify failure on building compress object with bad params
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000106 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000107 # specifying total bits too large causes an error
108 self.assertRaises(ValueError,
109 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000110
111 def test_baddecompressobj(self):
112 # verify failure on building decompress object with bad params
Antoine Pitrou90ee4df2010-04-06 17:23:13 +0000113 self.assertRaises(ValueError, zlib.decompressobj, -1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000114
Christian Heimes5e696852008-04-09 08:37:03 +0000115 def test_decompressobj_badflush(self):
116 # verify failure on calling decompressobj.flush with bad params
117 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
118 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
119
Martin Pantere99e9772015-11-20 08:13:35 +0000120 @support.cpython_only
121 def test_overflow(self):
122 with self.assertRaisesRegex(OverflowError, 'int too large'):
123 zlib.decompress(b'', 15, sys.maxsize + 1)
124 with self.assertRaisesRegex(OverflowError, 'int too large'):
125 zlib.decompressobj().flush(sys.maxsize + 1)
126
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000127
Antoine Pitrou89562712010-05-07 17:04:02 +0000128class BaseCompressTestCase(object):
129 def check_big_compress_buffer(self, size, compress_func):
130 _1M = 1024 * 1024
Antoine Pitrou89562712010-05-07 17:04:02 +0000131 # Generate 10MB worth of random, and expand it by repeating it.
132 # The assumption is that zlib's memory is not big enough to exploit
133 # such spread out redundancy.
134 data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little')
135 for i in range(10)])
136 data = data * (size // len(data) + 1)
137 try:
138 compress_func(data)
139 finally:
140 # Release memory
141 data = None
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000142
Antoine Pitrou89562712010-05-07 17:04:02 +0000143 def check_big_decompress_buffer(self, size, decompress_func):
144 data = b'x' * size
145 try:
146 compressed = zlib.compress(data, 1)
147 finally:
148 # Release memory
149 data = None
150 data = decompress_func(compressed)
151 # Sanity check
152 try:
153 self.assertEqual(len(data), size)
154 self.assertEqual(len(data.strip(b'x')), 0)
155 finally:
156 data = None
157
158
159class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000160 # Test compression in one go (whole message compression)
161 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000162 x = zlib.compress(HAMLET_SCENE)
163 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000164
Martin Panter1fe0d132016-02-10 10:06:36 +0000165 def test_keywords(self):
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300166 x = zlib.compress(HAMLET_SCENE, level=3)
Martin Panter1fe0d132016-02-10 10:06:36 +0000167 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300168 with self.assertRaises(TypeError):
169 zlib.compress(data=HAMLET_SCENE, level=3)
Martin Panter1fe0d132016-02-10 10:06:36 +0000170
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000171 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000172 # compress more data
173 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000174 x = zlib.compress(data)
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000175 self.assertEqual(zlib.compress(bytearray(data)), x)
176 for ob in x, bytearray(x):
177 self.assertEqual(zlib.decompress(ob), data)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000178
Antoine Pitrou53b21662010-05-11 23:46:02 +0000179 def test_incomplete_stream(self):
Martin Panter6245cb32016-04-15 02:14:19 +0000180 # A useful error message is given
Antoine Pitrou53b21662010-05-11 23:46:02 +0000181 x = zlib.compress(HAMLET_SCENE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000182 self.assertRaisesRegex(zlib.error,
Antoine Pitrou53b21662010-05-11 23:46:02 +0000183 "Error -5 while decompressing data: incomplete or truncated stream",
184 zlib.decompress, x[:-1])
185
Antoine Pitrou89562712010-05-07 17:04:02 +0000186 # Memory use of the following functions takes into account overallocation
187
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200188 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000189 def test_big_compress_buffer(self, size):
190 compress = lambda s: zlib.compress(s, 1)
191 self.check_big_compress_buffer(size, compress)
192
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200193 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000194 def test_big_decompress_buffer(self, size):
195 self.check_big_decompress_buffer(size, zlib.decompress)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000196
Nadeem Vawda197e22c2012-02-23 14:23:17 +0200197 @bigmemtest(size=_4G + 100, memuse=1, dry_run=False)
Victor Stinner8848c7a2011-01-04 02:07:36 +0000198 def test_length_overflow(self, size):
Victor Stinner8848c7a2011-01-04 02:07:36 +0000199 data = b'x' * size
200 try:
201 self.assertRaises(OverflowError, zlib.compress, data, 1)
Nadeem Vawda154bdf92011-05-14 23:07:36 +0200202 self.assertRaises(OverflowError, zlib.decompress, data)
Victor Stinner8848c7a2011-01-04 02:07:36 +0000203 finally:
204 data = None
205
Martin Pantere99e9772015-11-20 08:13:35 +0000206 @bigmemtest(size=_4G, memuse=1)
207 def test_large_bufsize(self, size):
208 # Test decompress(bufsize) parameter greater than the internal limit
209 data = HAMLET_SCENE * 10
210 compressed = zlib.compress(data, 1)
211 self.assertEqual(zlib.decompress(compressed, 15, size), data)
212
213 def test_custom_bufsize(self):
214 data = HAMLET_SCENE * 10
215 compressed = zlib.compress(data, 1)
216 self.assertEqual(zlib.decompress(compressed, 15, CustomInt()), data)
217
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000218
Antoine Pitrou89562712010-05-07 17:04:02 +0000219class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000220 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000221 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000222 # straightforward compress/decompress objects
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000223 datasrc = HAMLET_SCENE * 128
224 datazip = zlib.compress(datasrc)
225 # should compress both bytes and bytearray data
226 for data in (datasrc, bytearray(datasrc)):
227 co = zlib.compressobj()
228 x1 = co.compress(data)
229 x2 = co.flush()
230 self.assertRaises(zlib.error, co.flush) # second flush should not work
231 self.assertEqual(x1 + x2, datazip)
232 for v1, v2 in ((x1, x2), (bytearray(x1), bytearray(x2))):
233 dco = zlib.decompressobj()
234 y1 = dco.decompress(v1 + v2)
235 y2 = dco.flush()
236 self.assertEqual(data, y1 + y2)
237 self.assertIsInstance(dco.unconsumed_tail, bytes)
238 self.assertIsInstance(dco.unused_data, bytes)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000239
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000240 def test_compressoptions(self):
241 # specify lots of options to compressobj()
242 level = 2
243 method = zlib.DEFLATED
244 wbits = -12
Martin Panterbf19d162015-09-09 01:01:13 +0000245 memLevel = 9
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000246 strategy = zlib.Z_FILTERED
Martin Panterbf19d162015-09-09 01:01:13 +0000247 co = zlib.compressobj(level, method, wbits, memLevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000248 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000249 x2 = co.flush()
250 dco = zlib.decompressobj(wbits)
251 y1 = dco.decompress(x1 + x2)
252 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000253 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000254
Martin Panterbf19d162015-09-09 01:01:13 +0000255 # keyword arguments should also be supported
256 zlib.compressobj(level=level, method=method, wbits=wbits,
257 memLevel=memLevel, strategy=strategy, zdict=b"")
258
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000259 def test_compressincremental(self):
260 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000261 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000262 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000263 bufs = []
264 for i in range(0, len(data), 256):
265 bufs.append(co.compress(data[i:i+256]))
266 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000267 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000268
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000269 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000270 y1 = dco.decompress(b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000271 y2 = dco.flush()
272 self.assertEqual(data, y1 + y2)
273
Neil Schemenauer6412b122004-06-05 19:34:28 +0000274 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000275 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000276 source = source or HAMLET_SCENE
277 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000278 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000279 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000280 for i in range(0, len(data), cx):
281 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000282 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000283 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000284
Gregory P. Smith693fc462008-09-06 20:13:06 +0000285 decombuf = zlib.decompress(combuf)
286 # Test type of return value
Ezio Melottie9615932010-01-24 19:26:24 +0000287 self.assertIsInstance(decombuf, bytes)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000288
289 self.assertEqual(data, decombuf)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000290
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000291 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000292 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000293 for i in range(0, len(combuf), dcx):
294 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000295 self.assertEqual(b'', dco.unconsumed_tail, ########
296 "(A) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000297 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000298 self.assertEqual(b'', dco.unused_data)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000299 if flush:
300 bufs.append(dco.flush())
301 else:
302 while True:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000303 chunk = dco.decompress(b'')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000304 if chunk:
305 bufs.append(chunk)
306 else:
307 break
Guido van Rossum776152b2007-05-22 22:44:07 +0000308 self.assertEqual(b'', dco.unconsumed_tail, ########
309 "(B) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000310 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000311 self.assertEqual(b'', dco.unused_data)
Guido van Rossum776152b2007-05-22 22:44:07 +0000312 self.assertEqual(data, b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000313 # Failure means: "decompressobj with init options failed"
314
Neil Schemenauer6412b122004-06-05 19:34:28 +0000315 def test_decompincflush(self):
316 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000317
Neil Schemenauer6412b122004-06-05 19:34:28 +0000318 def test_decompimax(self, source=None, cx=256, dcx=64):
319 # compress in steps, decompress in length-restricted steps
320 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000321 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000322 data = source * 128
323 co = zlib.compressobj()
324 bufs = []
325 for i in range(0, len(data), cx):
326 bufs.append(co.compress(data[i:i+cx]))
327 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000328 combuf = b''.join(bufs)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000329 self.assertEqual(data, zlib.decompress(combuf),
330 'compressed data failure')
331
332 dco = zlib.decompressobj()
333 bufs = []
334 cb = combuf
335 while cb:
336 #max_length = 1 + len(cb)//10
337 chunk = dco.decompress(cb, dcx)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000338 self.assertFalse(len(chunk) > dcx,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000339 'chunk too big (%d>%d)' % (len(chunk), dcx))
340 bufs.append(chunk)
341 cb = dco.unconsumed_tail
342 bufs.append(dco.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000343 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000344
345 def test_decompressmaxlen(self, flush=False):
346 # Check a decompression object with max_length specified
347 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000348 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000349 bufs = []
350 for i in range(0, len(data), 256):
351 bufs.append(co.compress(data[i:i+256]))
352 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000353 combuf = b''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000354 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000355 'compressed data failure')
356
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000357 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000358 bufs = []
359 cb = combuf
360 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000361 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000362 chunk = dco.decompress(cb, max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000363 self.assertFalse(len(chunk) > max_length,
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000364 'chunk too big (%d>%d)' % (len(chunk),max_length))
365 bufs.append(chunk)
366 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000367 if flush:
368 bufs.append(dco.flush())
369 else:
370 while chunk:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000371 chunk = dco.decompress(b'', max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000372 self.assertFalse(len(chunk) > max_length,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000373 'chunk too big (%d>%d)' % (len(chunk),max_length))
374 bufs.append(chunk)
Guido van Rossum776152b2007-05-22 22:44:07 +0000375 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000376
Neil Schemenauer6412b122004-06-05 19:34:28 +0000377 def test_decompressmaxlenflush(self):
378 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000379
380 def test_maxlenmisc(self):
381 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000382 dco = zlib.decompressobj()
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000383 self.assertRaises(ValueError, dco.decompress, b"", -1)
Guido van Rossum776152b2007-05-22 22:44:07 +0000384 self.assertEqual(b'', dco.unconsumed_tail)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000385
Martin Pantere99e9772015-11-20 08:13:35 +0000386 def test_maxlen_large(self):
387 # Sizes up to sys.maxsize should be accepted, although zlib is
388 # internally limited to expressing sizes with unsigned int
389 data = HAMLET_SCENE * 10
390 self.assertGreater(len(data), zlib.DEF_BUF_SIZE)
391 compressed = zlib.compress(data, 1)
392 dco = zlib.decompressobj()
393 self.assertEqual(dco.decompress(compressed, sys.maxsize), data)
394
395 def test_maxlen_custom(self):
396 data = HAMLET_SCENE * 10
397 compressed = zlib.compress(data, 1)
398 dco = zlib.decompressobj()
399 self.assertEqual(dco.decompress(compressed, CustomInt()), data[:100])
400
Nadeem Vawda7619e882011-05-14 14:05:20 +0200401 def test_clear_unconsumed_tail(self):
402 # Issue #12050: calling decompress() without providing max_length
403 # should clear the unconsumed_tail attribute.
404 cdata = b"x\x9cKLJ\x06\x00\x02M\x01" # "abc"
405 dco = zlib.decompressobj()
406 ddata = dco.decompress(cdata, 1)
407 ddata += dco.decompress(dco.unconsumed_tail)
408 self.assertEqual(dco.unconsumed_tail, b"")
409
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000410 def test_flushes(self):
411 # Test flush() with the various options, using all the
412 # different levels in order to provide more variations.
413 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
414 sync_opt = [getattr(zlib, opt) for opt in sync_opt
415 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000416 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000417
418 for sync in sync_opt:
419 for level in range(10):
420 obj = zlib.compressobj( level )
421 a = obj.compress( data[:3000] )
422 b = obj.flush( sync )
423 c = obj.compress( data[3000:] )
424 d = obj.flush()
Guido van Rossum776152b2007-05-22 22:44:07 +0000425 self.assertEqual(zlib.decompress(b''.join([a,b,c,d])),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000426 data, ("Decompress failed: flush "
427 "mode=%i, level=%i") % (sync, level))
428 del obj
429
Serhiy Storchaka43767632013-11-03 21:31:38 +0200430 @unittest.skipUnless(hasattr(zlib, 'Z_SYNC_FLUSH'),
431 'requires zlib.Z_SYNC_FLUSH')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000432 def test_odd_flush(self):
433 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
434 import random
Serhiy Storchaka43767632013-11-03 21:31:38 +0200435 # Testing on 17K of "random" data
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000436
Serhiy Storchaka43767632013-11-03 21:31:38 +0200437 # Create compressor and decompressor objects
438 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
439 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000440
Serhiy Storchaka43767632013-11-03 21:31:38 +0200441 # Try 17K of data
442 # generate random data stream
443 try:
444 # In 2.3 and later, WichmannHill is the RNG of the bug report
445 gen = random.WichmannHill()
446 except AttributeError:
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000447 try:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200448 # 2.2 called it Random
449 gen = random.Random()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000450 except AttributeError:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200451 # others might simply have a single RNG
452 gen = random
453 gen.seed(1)
454 data = genblock(1, 17 * 1024, generator=gen)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000455
Serhiy Storchaka43767632013-11-03 21:31:38 +0200456 # compress, sync-flush, and decompress
457 first = co.compress(data)
458 second = co.flush(zlib.Z_SYNC_FLUSH)
459 expanded = dco.decompress(first + second)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000460
Serhiy Storchaka43767632013-11-03 21:31:38 +0200461 # if decompressed data is different from the input data, choke.
462 self.assertEqual(expanded, data, "17K random source doesn't match")
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000463
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000464 def test_empty_flush(self):
465 # Test that calling .flush() on unused objects works.
466 # (Bug #1083110 -- calling .flush() on decompress objects
467 # caused a core dump.)
468
469 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000470 self.assertTrue(co.flush()) # Returns a zlib header
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000471 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000472 self.assertEqual(dco.flush(), b"") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000473
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200474 def test_dictionary(self):
475 h = HAMLET_SCENE
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200476 # Build a simulated dictionary out of the words in HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200477 words = h.split()
478 random.shuffle(words)
479 zdict = b''.join(words)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200480 # Use it to compress HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200481 co = zlib.compressobj(zdict=zdict)
482 cd = co.compress(h) + co.flush()
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200483 # Verify that it will decompress with the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484 dco = zlib.decompressobj(zdict=zdict)
485 self.assertEqual(dco.decompress(cd) + dco.flush(), h)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200486 # Verify that it fails when not given the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200487 dco = zlib.decompressobj()
488 self.assertRaises(zlib.error, dco.decompress, cd)
489
490 def test_dictionary_streaming(self):
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200491 # This simulates the reuse of a compressor object for compressing
492 # several separate data streams.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200493 co = zlib.compressobj(zdict=HAMLET_SCENE)
494 do = zlib.decompressobj(zdict=HAMLET_SCENE)
495 piece = HAMLET_SCENE[1000:1500]
496 d0 = co.compress(piece) + co.flush(zlib.Z_SYNC_FLUSH)
497 d1 = co.compress(piece[100:]) + co.flush(zlib.Z_SYNC_FLUSH)
498 d2 = co.compress(piece[:-100]) + co.flush(zlib.Z_SYNC_FLUSH)
499 self.assertEqual(do.decompress(d0), piece)
500 self.assertEqual(do.decompress(d1), piece[100:])
501 self.assertEqual(do.decompress(d2), piece[:-100])
502
Antoine Pitrouc09c92f2010-05-11 23:36:40 +0000503 def test_decompress_incomplete_stream(self):
504 # This is 'foo', deflated
505 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E'
506 # For the record
507 self.assertEqual(zlib.decompress(x), b'foo')
508 self.assertRaises(zlib.error, zlib.decompress, x[:-5])
509 # Omitting the stream end works with decompressor objects
510 # (see issue #8672).
511 dco = zlib.decompressobj()
512 y = dco.decompress(x[:-5])
513 y += dco.flush()
514 self.assertEqual(y, b'foo')
515
Nadeem Vawda1c385462011-08-13 15:22:40 +0200516 def test_decompress_eof(self):
517 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
518 dco = zlib.decompressobj()
519 self.assertFalse(dco.eof)
520 dco.decompress(x[:-5])
521 self.assertFalse(dco.eof)
522 dco.decompress(x[-5:])
523 self.assertTrue(dco.eof)
524 dco.flush()
525 self.assertTrue(dco.eof)
526
527 def test_decompress_eof_incomplete_stream(self):
528 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
529 dco = zlib.decompressobj()
530 self.assertFalse(dco.eof)
531 dco.decompress(x[:-5])
532 self.assertFalse(dco.eof)
533 dco.flush()
534 self.assertFalse(dco.eof)
535
Nadeem Vawda39079942012-11-05 00:37:42 +0100536 def test_decompress_unused_data(self):
537 # Repeated calls to decompress() after EOF should accumulate data in
538 # dco.unused_data, instead of just storing the arg to the last call.
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100539 source = b'abcdefghijklmnopqrstuvwxyz'
540 remainder = b'0123456789'
541 y = zlib.compress(source)
542 x = y + remainder
543 for maxlen in 0, 1000:
544 for step in 1, 2, len(y), len(x):
545 dco = zlib.decompressobj()
546 data = b''
547 for i in range(0, len(x), step):
548 if i < len(y):
549 self.assertEqual(dco.unused_data, b'')
550 if maxlen == 0:
551 data += dco.decompress(x[i : i + step])
552 self.assertEqual(dco.unconsumed_tail, b'')
553 else:
554 data += dco.decompress(
555 dco.unconsumed_tail + x[i : i + step], maxlen)
556 data += dco.flush()
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100557 self.assertTrue(dco.eof)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100558 self.assertEqual(data, source)
559 self.assertEqual(dco.unconsumed_tail, b'')
560 self.assertEqual(dco.unused_data, remainder)
Nadeem Vawda39079942012-11-05 00:37:42 +0100561
Martin Panter3f0ee832016-06-05 10:48:34 +0000562 # issue27164
563 def test_decompress_raw_with_dictionary(self):
564 zdict = b'abcdefghijklmnopqrstuvwxyz'
565 co = zlib.compressobj(wbits=-zlib.MAX_WBITS, zdict=zdict)
566 comp = co.compress(zdict) + co.flush()
567 dco = zlib.decompressobj(wbits=-zlib.MAX_WBITS, zdict=zdict)
568 uncomp = dco.decompress(comp) + dco.flush()
569 self.assertEqual(zdict, uncomp)
570
Nadeem Vawda7ee95552012-11-11 03:15:32 +0100571 def test_flush_with_freed_input(self):
572 # Issue #16411: decompressor accesses input to last decompress() call
573 # in flush(), even if this object has been freed in the meanwhile.
574 input1 = b'abcdefghijklmnopqrstuvwxyz'
575 input2 = b'QWERTYUIOPASDFGHJKLZXCVBNM'
576 data = zlib.compress(input1)
577 dco = zlib.decompressobj()
578 dco.decompress(data, 1)
579 del data
580 data = zlib.compress(input2)
581 self.assertEqual(dco.flush(), input1[1:])
582
Martin Pantere99e9772015-11-20 08:13:35 +0000583 @bigmemtest(size=_4G, memuse=1)
584 def test_flush_large_length(self, size):
585 # Test flush(length) parameter greater than internal limit UINT_MAX
586 input = HAMLET_SCENE * 10
587 data = zlib.compress(input, 1)
588 dco = zlib.decompressobj()
589 dco.decompress(data, 1)
590 self.assertEqual(dco.flush(size), input[1:])
591
592 def test_flush_custom_length(self):
593 input = HAMLET_SCENE * 10
594 data = zlib.compress(input, 1)
595 dco = zlib.decompressobj()
596 dco.decompress(data, 1)
597 self.assertEqual(dco.flush(CustomInt()), input[1:])
598
Serhiy Storchaka43767632013-11-03 21:31:38 +0200599 @requires_Compress_copy
600 def test_compresscopy(self):
601 # Test copying a compression object
602 data0 = HAMLET_SCENE
603 data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii")
604 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
605 bufs0 = []
606 bufs0.append(c0.compress(data0))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000607
Serhiy Storchaka43767632013-11-03 21:31:38 +0200608 c1 = c0.copy()
609 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000610
Serhiy Storchaka43767632013-11-03 21:31:38 +0200611 bufs0.append(c0.compress(data0))
612 bufs0.append(c0.flush())
613 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614
Serhiy Storchaka43767632013-11-03 21:31:38 +0200615 bufs1.append(c1.compress(data1))
616 bufs1.append(c1.flush())
617 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000618
Serhiy Storchaka43767632013-11-03 21:31:38 +0200619 self.assertEqual(zlib.decompress(s0),data0+data0)
620 self.assertEqual(zlib.decompress(s1),data0+data1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000621
Serhiy Storchaka43767632013-11-03 21:31:38 +0200622 @requires_Compress_copy
623 def test_badcompresscopy(self):
624 # Test copying a compression object in an inconsistent state
625 c = zlib.compressobj()
626 c.compress(HAMLET_SCENE)
627 c.flush()
628 self.assertRaises(ValueError, c.copy)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629
Serhiy Storchaka43767632013-11-03 21:31:38 +0200630 @requires_Decompress_copy
631 def test_decompresscopy(self):
632 # Test copying a decompression object
633 data = HAMLET_SCENE
634 comp = zlib.compress(data)
635 # Test type of return value
636 self.assertIsInstance(comp, bytes)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000637
Serhiy Storchaka43767632013-11-03 21:31:38 +0200638 d0 = zlib.decompressobj()
639 bufs0 = []
640 bufs0.append(d0.decompress(comp[:32]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641
Serhiy Storchaka43767632013-11-03 21:31:38 +0200642 d1 = d0.copy()
643 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644
Serhiy Storchaka43767632013-11-03 21:31:38 +0200645 bufs0.append(d0.decompress(comp[32:]))
646 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647
Serhiy Storchaka43767632013-11-03 21:31:38 +0200648 bufs1.append(d1.decompress(comp[32:]))
649 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650
Serhiy Storchaka43767632013-11-03 21:31:38 +0200651 self.assertEqual(s0,s1)
652 self.assertEqual(s0,data)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653
Serhiy Storchaka43767632013-11-03 21:31:38 +0200654 @requires_Decompress_copy
655 def test_baddecompresscopy(self):
656 # Test copying a compression object in an inconsistent state
657 data = zlib.compress(HAMLET_SCENE)
658 d = zlib.decompressobj()
659 d.decompress(data)
660 d.flush()
661 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000662
Serhiy Storchakad7a44152015-11-12 11:23:04 +0200663 def test_compresspickle(self):
664 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
665 with self.assertRaises((TypeError, pickle.PicklingError)):
666 pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto)
667
668 def test_decompresspickle(self):
669 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
670 with self.assertRaises((TypeError, pickle.PicklingError)):
671 pickle.dumps(zlib.decompressobj(), proto)
672
Antoine Pitrou89562712010-05-07 17:04:02 +0000673 # Memory use of the following functions takes into account overallocation
674
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200675 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000676 def test_big_compress_buffer(self, size):
677 c = zlib.compressobj(1)
678 compress = lambda s: c.compress(s) + c.flush()
679 self.check_big_compress_buffer(size, compress)
680
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200681 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000682 def test_big_decompress_buffer(self, size):
683 d = zlib.decompressobj()
684 decompress = lambda s: d.decompress(s) + d.flush()
685 self.check_big_decompress_buffer(size, decompress)
686
Nadeem Vawda197e22c2012-02-23 14:23:17 +0200687 @bigmemtest(size=_4G + 100, memuse=1, dry_run=False)
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200688 def test_length_overflow(self, size):
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200689 data = b'x' * size
Nadeem Vawda1161a9c2011-05-15 00:48:24 +0200690 c = zlib.compressobj(1)
691 d = zlib.decompressobj()
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200692 try:
Nadeem Vawda1161a9c2011-05-15 00:48:24 +0200693 self.assertRaises(OverflowError, c.compress, data)
694 self.assertRaises(OverflowError, d.decompress, data)
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200695 finally:
696 data = None
697
Martin Panter0fdf41d2016-05-27 07:32:11 +0000698 def test_wbits(self):
Martin Panterc618ae82016-05-27 11:20:21 +0000699 # wbits=0 only supported since zlib v1.2.3.5
700 # Register "1.2.3" as "1.2.3.0"
701 v = (zlib.ZLIB_RUNTIME_VERSION + ".0").split(".", 4)
702 supports_wbits_0 = int(v[0]) > 1 or int(v[0]) == 1 \
703 and (int(v[1]) > 2 or int(v[1]) == 2
704 and (int(v[2]) > 3 or int(v[2]) == 3 and int(v[3]) >= 5))
705
Martin Panter0fdf41d2016-05-27 07:32:11 +0000706 co = zlib.compressobj(level=1, wbits=15)
707 zlib15 = co.compress(HAMLET_SCENE) + co.flush()
708 self.assertEqual(zlib.decompress(zlib15, 15), HAMLET_SCENE)
Martin Panterc618ae82016-05-27 11:20:21 +0000709 if supports_wbits_0:
710 self.assertEqual(zlib.decompress(zlib15, 0), HAMLET_SCENE)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000711 self.assertEqual(zlib.decompress(zlib15, 32 + 15), HAMLET_SCENE)
712 with self.assertRaisesRegex(zlib.error, 'invalid window size'):
713 zlib.decompress(zlib15, 14)
714 dco = zlib.decompressobj(wbits=32 + 15)
715 self.assertEqual(dco.decompress(zlib15), HAMLET_SCENE)
716 dco = zlib.decompressobj(wbits=14)
717 with self.assertRaisesRegex(zlib.error, 'invalid window size'):
718 dco.decompress(zlib15)
719
720 co = zlib.compressobj(level=1, wbits=9)
721 zlib9 = co.compress(HAMLET_SCENE) + co.flush()
722 self.assertEqual(zlib.decompress(zlib9, 9), HAMLET_SCENE)
723 self.assertEqual(zlib.decompress(zlib9, 15), HAMLET_SCENE)
Martin Panterc618ae82016-05-27 11:20:21 +0000724 if supports_wbits_0:
725 self.assertEqual(zlib.decompress(zlib9, 0), HAMLET_SCENE)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000726 self.assertEqual(zlib.decompress(zlib9, 32 + 9), HAMLET_SCENE)
727 dco = zlib.decompressobj(wbits=32 + 9)
728 self.assertEqual(dco.decompress(zlib9), HAMLET_SCENE)
729
730 co = zlib.compressobj(level=1, wbits=-15)
731 deflate15 = co.compress(HAMLET_SCENE) + co.flush()
732 self.assertEqual(zlib.decompress(deflate15, -15), HAMLET_SCENE)
733 dco = zlib.decompressobj(wbits=-15)
734 self.assertEqual(dco.decompress(deflate15), HAMLET_SCENE)
735
736 co = zlib.compressobj(level=1, wbits=-9)
737 deflate9 = co.compress(HAMLET_SCENE) + co.flush()
738 self.assertEqual(zlib.decompress(deflate9, -9), HAMLET_SCENE)
739 self.assertEqual(zlib.decompress(deflate9, -15), HAMLET_SCENE)
740 dco = zlib.decompressobj(wbits=-9)
741 self.assertEqual(dco.decompress(deflate9), HAMLET_SCENE)
742
743 co = zlib.compressobj(level=1, wbits=16 + 15)
744 gzip = co.compress(HAMLET_SCENE) + co.flush()
745 self.assertEqual(zlib.decompress(gzip, 16 + 15), HAMLET_SCENE)
746 self.assertEqual(zlib.decompress(gzip, 32 + 15), HAMLET_SCENE)
747 dco = zlib.decompressobj(32 + 15)
748 self.assertEqual(dco.decompress(gzip), HAMLET_SCENE)
749
Antoine Pitrou89562712010-05-07 17:04:02 +0000750
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000751def genblock(seed, length, step=1024, generator=random):
752 """length-byte stream of random data from a seed (in step-byte blocks)."""
753 if seed is not None:
754 generator.seed(seed)
755 randint = generator.randint
756 if length < step or step < 2:
757 step = length
Guido van Rossum776152b2007-05-22 22:44:07 +0000758 blocks = bytes()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000759 for i in range(0, length, step):
Guido van Rossum776152b2007-05-22 22:44:07 +0000760 blocks += bytes(randint(0, 255) for x in range(step))
761 return blocks
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000762
763
764
765def choose_lines(source, number, seed=None, generator=random):
766 """Return a list of number lines randomly chosen from the source"""
767 if seed is not None:
768 generator.seed(seed)
769 sources = source.split('\n')
770 return [generator.choice(sources) for n in range(number)]
771
772
773
Guido van Rossum776152b2007-05-22 22:44:07 +0000774HAMLET_SCENE = b"""
Fred Drake004d5e62000-10-23 17:22:08 +0000775LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000776
777 O, fear me not.
778 I stay too long: but here my father comes.
779
780 Enter POLONIUS
781
782 A double blessing is a double grace,
783 Occasion smiles upon a second leave.
784
Fred Drake004d5e62000-10-23 17:22:08 +0000785LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000786
787 Yet here, Laertes! aboard, aboard, for shame!
788 The wind sits in the shoulder of your sail,
789 And you are stay'd for. There; my blessing with thee!
790 And these few precepts in thy memory
791 See thou character. Give thy thoughts no tongue,
792 Nor any unproportioned thought his act.
793 Be thou familiar, but by no means vulgar.
794 Those friends thou hast, and their adoption tried,
795 Grapple them to thy soul with hoops of steel;
796 But do not dull thy palm with entertainment
797 Of each new-hatch'd, unfledged comrade. Beware
798 Of entrance to a quarrel, but being in,
799 Bear't that the opposed may beware of thee.
800 Give every man thy ear, but few thy voice;
801 Take each man's censure, but reserve thy judgment.
802 Costly thy habit as thy purse can buy,
803 But not express'd in fancy; rich, not gaudy;
804 For the apparel oft proclaims the man,
805 And they in France of the best rank and station
806 Are of a most select and generous chief in that.
807 Neither a borrower nor a lender be;
808 For loan oft loses both itself and friend,
809 And borrowing dulls the edge of husbandry.
810 This above all: to thine ownself be true,
811 And it must follow, as the night the day,
812 Thou canst not then be false to any man.
813 Farewell: my blessing season this in thee!
814
Fred Drake004d5e62000-10-23 17:22:08 +0000815LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000816
817 Most humbly do I take my leave, my lord.
818
Fred Drake004d5e62000-10-23 17:22:08 +0000819LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000820
821 The time invites you; go; your servants tend.
822
Fred Drake004d5e62000-10-23 17:22:08 +0000823LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000824
825 Farewell, Ophelia; and remember well
826 What I have said to you.
827
Fred Drake004d5e62000-10-23 17:22:08 +0000828OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000829
830 'Tis in my memory lock'd,
831 And you yourself shall keep the key of it.
832
Fred Drake004d5e62000-10-23 17:22:08 +0000833LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000834
835 Farewell.
836"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000837
838
Martin Pantere99e9772015-11-20 08:13:35 +0000839class CustomInt:
840 def __int__(self):
841 return 100
842
843
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000844if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500845 unittest.main()