blob: 88c415bc4eb26c518db25216e0668439835fc101 [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 assertEqual32(self, seen, expected):
51 # 32-bit values masked -- checksums on 32- vs 64- bit machines
52 # This is important if bit 31 (0x08000000L) is set.
Guido van Rossume2a383d2007-01-15 16:59:06 +000053 self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000054
55 def test_penguins(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000056 self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
57 self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94)
58 self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6)
59 self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000060
Guido van Rossum776152b2007-05-22 22:44:07 +000061 self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0))
62 self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000063
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000064 def test_crc32_adler32_unsigned(self):
Antoine Pitrou77b338b2009-12-14 18:00:06 +000065 foo = b'abcdefghijklmnop'
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000066 # explicitly test signed behavior
Gregory P. Smith27275032008-03-20 06:20:09 +000067 self.assertEqual(zlib.crc32(foo), 2486878355)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000068 self.assertEqual(zlib.crc32(b'spam'), 1138425661)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000069 self.assertEqual(zlib.adler32(foo+foo), 3573550353)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000070 self.assertEqual(zlib.adler32(b'spam'), 72286642)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000071
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000072 def test_same_as_binascii_crc32(self):
Martin v. Löwis15b16a32008-12-02 06:00:15 +000073 foo = b'abcdefghijklmnop'
Gregory P. Smith27275032008-03-20 06:20:09 +000074 crc = 2486878355
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000075 self.assertEqual(binascii.crc32(foo), crc)
76 self.assertEqual(zlib.crc32(foo), crc)
Martin v. Löwis15b16a32008-12-02 06:00:15 +000077 self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam'))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000078
79
Antoine Pitrouf3d22752011-02-21 18:09:00 +000080# Issue #10276 - check that inputs >=4GB are handled correctly.
81class ChecksumBigBufferTestCase(unittest.TestCase):
82
Nadeem Vawdabc8c8172012-02-23 14:16:15 +020083 @bigmemtest(size=_4G + 4, memuse=1, dry_run=False)
84 def test_big_buffer(self, size):
Nadeem Vawdab063a482012-02-23 13:36:25 +020085 data = b"nyan" * (_1G + 1)
86 self.assertEqual(zlib.crc32(data), 1044521549)
87 self.assertEqual(zlib.adler32(data), 2256789997)
Antoine Pitrouf3d22752011-02-21 18:09:00 +000088
Christian Heimesb186d002008-03-18 15:15:01 +000089
Guido van Rossum7d9ea502003-02-03 20:45:52 +000090class ExceptionTestCase(unittest.TestCase):
91 # make sure we generate some expected errors
Guido van Rossum8ce8a782007-11-01 19:42:39 +000092 def test_badlevel(self):
93 # specifying compression level out of range causes an error
94 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
95 # accepts 0 too)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000096 self.assertRaises(zlib.error, zlib.compress, b'ERROR', 10)
97
98 def test_badargs(self):
99 self.assertRaises(TypeError, zlib.adler32)
100 self.assertRaises(TypeError, zlib.crc32)
101 self.assertRaises(TypeError, zlib.compress)
102 self.assertRaises(TypeError, zlib.decompress)
103 for arg in (42, None, '', 'abc', (), []):
104 self.assertRaises(TypeError, zlib.adler32, arg)
105 self.assertRaises(TypeError, zlib.crc32, arg)
106 self.assertRaises(TypeError, zlib.compress, arg)
107 self.assertRaises(TypeError, zlib.decompress, arg)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000108
109 def test_badcompressobj(self):
110 # verify failure on building compress object with bad params
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000111 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000112 # specifying total bits too large causes an error
113 self.assertRaises(ValueError,
114 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000115
116 def test_baddecompressobj(self):
117 # verify failure on building decompress object with bad params
Antoine Pitrou90ee4df2010-04-06 17:23:13 +0000118 self.assertRaises(ValueError, zlib.decompressobj, -1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000119
Christian Heimes5e696852008-04-09 08:37:03 +0000120 def test_decompressobj_badflush(self):
121 # verify failure on calling decompressobj.flush with bad params
122 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
123 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
124
Martin Pantere99e9772015-11-20 08:13:35 +0000125 @support.cpython_only
126 def test_overflow(self):
127 with self.assertRaisesRegex(OverflowError, 'int too large'):
128 zlib.decompress(b'', 15, sys.maxsize + 1)
129 with self.assertRaisesRegex(OverflowError, 'int too large'):
130 zlib.decompressobj().flush(sys.maxsize + 1)
131
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000132
Antoine Pitrou89562712010-05-07 17:04:02 +0000133class BaseCompressTestCase(object):
134 def check_big_compress_buffer(self, size, compress_func):
135 _1M = 1024 * 1024
Antoine Pitrou89562712010-05-07 17:04:02 +0000136 # Generate 10MB worth of random, and expand it by repeating it.
137 # The assumption is that zlib's memory is not big enough to exploit
138 # such spread out redundancy.
139 data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little')
140 for i in range(10)])
141 data = data * (size // len(data) + 1)
142 try:
143 compress_func(data)
144 finally:
145 # Release memory
146 data = None
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000147
Antoine Pitrou89562712010-05-07 17:04:02 +0000148 def check_big_decompress_buffer(self, size, decompress_func):
149 data = b'x' * size
150 try:
151 compressed = zlib.compress(data, 1)
152 finally:
153 # Release memory
154 data = None
155 data = decompress_func(compressed)
156 # Sanity check
157 try:
158 self.assertEqual(len(data), size)
159 self.assertEqual(len(data.strip(b'x')), 0)
160 finally:
161 data = None
162
163
164class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000165 # Test compression in one go (whole message compression)
166 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000167 x = zlib.compress(HAMLET_SCENE)
168 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000169
170 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000171 # compress more data
172 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000173 x = zlib.compress(data)
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000174 self.assertEqual(zlib.compress(bytearray(data)), x)
175 for ob in x, bytearray(x):
176 self.assertEqual(zlib.decompress(ob), data)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000177
Antoine Pitrou53b21662010-05-11 23:46:02 +0000178 def test_incomplete_stream(self):
179 # An useful error message is given
180 x = zlib.compress(HAMLET_SCENE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000181 self.assertRaisesRegex(zlib.error,
Antoine Pitrou53b21662010-05-11 23:46:02 +0000182 "Error -5 while decompressing data: incomplete or truncated stream",
183 zlib.decompress, x[:-1])
184
Antoine Pitrou89562712010-05-07 17:04:02 +0000185 # Memory use of the following functions takes into account overallocation
186
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200187 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000188 def test_big_compress_buffer(self, size):
189 compress = lambda s: zlib.compress(s, 1)
190 self.check_big_compress_buffer(size, compress)
191
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200192 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000193 def test_big_decompress_buffer(self, size):
194 self.check_big_decompress_buffer(size, zlib.decompress)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000195
Nadeem Vawda197e22c2012-02-23 14:23:17 +0200196 @bigmemtest(size=_4G + 100, memuse=1, dry_run=False)
Victor Stinner8848c7a2011-01-04 02:07:36 +0000197 def test_length_overflow(self, size):
Victor Stinner8848c7a2011-01-04 02:07:36 +0000198 data = b'x' * size
199 try:
200 self.assertRaises(OverflowError, zlib.compress, data, 1)
Nadeem Vawda154bdf92011-05-14 23:07:36 +0200201 self.assertRaises(OverflowError, zlib.decompress, data)
Victor Stinner8848c7a2011-01-04 02:07:36 +0000202 finally:
203 data = None
204
Martin Pantere99e9772015-11-20 08:13:35 +0000205 @bigmemtest(size=_4G, memuse=1)
206 def test_large_bufsize(self, size):
207 # Test decompress(bufsize) parameter greater than the internal limit
208 data = HAMLET_SCENE * 10
209 compressed = zlib.compress(data, 1)
210 self.assertEqual(zlib.decompress(compressed, 15, size), data)
211
212 def test_custom_bufsize(self):
213 data = HAMLET_SCENE * 10
214 compressed = zlib.compress(data, 1)
215 self.assertEqual(zlib.decompress(compressed, 15, CustomInt()), data)
216
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000217
Antoine Pitrou89562712010-05-07 17:04:02 +0000218class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000219 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000220 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000221 # straightforward compress/decompress objects
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000222 datasrc = HAMLET_SCENE * 128
223 datazip = zlib.compress(datasrc)
224 # should compress both bytes and bytearray data
225 for data in (datasrc, bytearray(datasrc)):
226 co = zlib.compressobj()
227 x1 = co.compress(data)
228 x2 = co.flush()
229 self.assertRaises(zlib.error, co.flush) # second flush should not work
230 self.assertEqual(x1 + x2, datazip)
231 for v1, v2 in ((x1, x2), (bytearray(x1), bytearray(x2))):
232 dco = zlib.decompressobj()
233 y1 = dco.decompress(v1 + v2)
234 y2 = dco.flush()
235 self.assertEqual(data, y1 + y2)
236 self.assertIsInstance(dco.unconsumed_tail, bytes)
237 self.assertIsInstance(dco.unused_data, bytes)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000238
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000239 def test_compressoptions(self):
240 # specify lots of options to compressobj()
241 level = 2
242 method = zlib.DEFLATED
243 wbits = -12
Martin Panterbf19d162015-09-09 01:01:13 +0000244 memLevel = 9
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000245 strategy = zlib.Z_FILTERED
Martin Panterbf19d162015-09-09 01:01:13 +0000246 co = zlib.compressobj(level, method, wbits, memLevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000247 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000248 x2 = co.flush()
249 dco = zlib.decompressobj(wbits)
250 y1 = dco.decompress(x1 + x2)
251 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000252 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000253
Martin Panterbf19d162015-09-09 01:01:13 +0000254 # keyword arguments should also be supported
255 zlib.compressobj(level=level, method=method, wbits=wbits,
256 memLevel=memLevel, strategy=strategy, zdict=b"")
257
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000258 def test_compressincremental(self):
259 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000260 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000261 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000262 bufs = []
263 for i in range(0, len(data), 256):
264 bufs.append(co.compress(data[i:i+256]))
265 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000266 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000267
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000268 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000269 y1 = dco.decompress(b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000270 y2 = dco.flush()
271 self.assertEqual(data, y1 + y2)
272
Neil Schemenauer6412b122004-06-05 19:34:28 +0000273 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000274 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000275 source = source or HAMLET_SCENE
276 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000277 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000278 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000279 for i in range(0, len(data), cx):
280 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000281 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000282 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000283
Gregory P. Smith693fc462008-09-06 20:13:06 +0000284 decombuf = zlib.decompress(combuf)
285 # Test type of return value
Ezio Melottie9615932010-01-24 19:26:24 +0000286 self.assertIsInstance(decombuf, bytes)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000287
288 self.assertEqual(data, decombuf)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000289
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000290 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000291 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000292 for i in range(0, len(combuf), dcx):
293 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000294 self.assertEqual(b'', dco.unconsumed_tail, ########
295 "(A) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000296 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000297 self.assertEqual(b'', dco.unused_data)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000298 if flush:
299 bufs.append(dco.flush())
300 else:
301 while True:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000302 chunk = dco.decompress(b'')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000303 if chunk:
304 bufs.append(chunk)
305 else:
306 break
Guido van Rossum776152b2007-05-22 22:44:07 +0000307 self.assertEqual(b'', dco.unconsumed_tail, ########
308 "(B) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000309 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000310 self.assertEqual(b'', dco.unused_data)
Guido van Rossum776152b2007-05-22 22:44:07 +0000311 self.assertEqual(data, b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000312 # Failure means: "decompressobj with init options failed"
313
Neil Schemenauer6412b122004-06-05 19:34:28 +0000314 def test_decompincflush(self):
315 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000316
Neil Schemenauer6412b122004-06-05 19:34:28 +0000317 def test_decompimax(self, source=None, cx=256, dcx=64):
318 # compress in steps, decompress in length-restricted steps
319 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000320 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000321 data = source * 128
322 co = zlib.compressobj()
323 bufs = []
324 for i in range(0, len(data), cx):
325 bufs.append(co.compress(data[i:i+cx]))
326 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000327 combuf = b''.join(bufs)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000328 self.assertEqual(data, zlib.decompress(combuf),
329 'compressed data failure')
330
331 dco = zlib.decompressobj()
332 bufs = []
333 cb = combuf
334 while cb:
335 #max_length = 1 + len(cb)//10
336 chunk = dco.decompress(cb, dcx)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000337 self.assertFalse(len(chunk) > dcx,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000338 'chunk too big (%d>%d)' % (len(chunk), dcx))
339 bufs.append(chunk)
340 cb = dco.unconsumed_tail
341 bufs.append(dco.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000342 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000343
344 def test_decompressmaxlen(self, flush=False):
345 # Check a decompression object with max_length specified
346 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000347 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000348 bufs = []
349 for i in range(0, len(data), 256):
350 bufs.append(co.compress(data[i:i+256]))
351 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000352 combuf = b''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000353 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000354 'compressed data failure')
355
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000356 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000357 bufs = []
358 cb = combuf
359 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000360 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000361 chunk = dco.decompress(cb, max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000362 self.assertFalse(len(chunk) > max_length,
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000363 'chunk too big (%d>%d)' % (len(chunk),max_length))
364 bufs.append(chunk)
365 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000366 if flush:
367 bufs.append(dco.flush())
368 else:
369 while chunk:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000370 chunk = dco.decompress(b'', max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000371 self.assertFalse(len(chunk) > max_length,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000372 'chunk too big (%d>%d)' % (len(chunk),max_length))
373 bufs.append(chunk)
Guido van Rossum776152b2007-05-22 22:44:07 +0000374 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000375
Neil Schemenauer6412b122004-06-05 19:34:28 +0000376 def test_decompressmaxlenflush(self):
377 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000378
379 def test_maxlenmisc(self):
380 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000381 dco = zlib.decompressobj()
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000382 self.assertRaises(ValueError, dco.decompress, b"", -1)
Guido van Rossum776152b2007-05-22 22:44:07 +0000383 self.assertEqual(b'', dco.unconsumed_tail)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000384
Martin Pantere99e9772015-11-20 08:13:35 +0000385 def test_maxlen_large(self):
386 # Sizes up to sys.maxsize should be accepted, although zlib is
387 # internally limited to expressing sizes with unsigned int
388 data = HAMLET_SCENE * 10
389 self.assertGreater(len(data), zlib.DEF_BUF_SIZE)
390 compressed = zlib.compress(data, 1)
391 dco = zlib.decompressobj()
392 self.assertEqual(dco.decompress(compressed, sys.maxsize), data)
393
394 def test_maxlen_custom(self):
395 data = HAMLET_SCENE * 10
396 compressed = zlib.compress(data, 1)
397 dco = zlib.decompressobj()
398 self.assertEqual(dco.decompress(compressed, CustomInt()), data[:100])
399
Nadeem Vawda7619e882011-05-14 14:05:20 +0200400 def test_clear_unconsumed_tail(self):
401 # Issue #12050: calling decompress() without providing max_length
402 # should clear the unconsumed_tail attribute.
403 cdata = b"x\x9cKLJ\x06\x00\x02M\x01" # "abc"
404 dco = zlib.decompressobj()
405 ddata = dco.decompress(cdata, 1)
406 ddata += dco.decompress(dco.unconsumed_tail)
407 self.assertEqual(dco.unconsumed_tail, b"")
408
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000409 def test_flushes(self):
410 # Test flush() with the various options, using all the
411 # different levels in order to provide more variations.
412 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
413 sync_opt = [getattr(zlib, opt) for opt in sync_opt
414 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000415 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000416
417 for sync in sync_opt:
418 for level in range(10):
419 obj = zlib.compressobj( level )
420 a = obj.compress( data[:3000] )
421 b = obj.flush( sync )
422 c = obj.compress( data[3000:] )
423 d = obj.flush()
Guido van Rossum776152b2007-05-22 22:44:07 +0000424 self.assertEqual(zlib.decompress(b''.join([a,b,c,d])),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000425 data, ("Decompress failed: flush "
426 "mode=%i, level=%i") % (sync, level))
427 del obj
428
Serhiy Storchaka43767632013-11-03 21:31:38 +0200429 @unittest.skipUnless(hasattr(zlib, 'Z_SYNC_FLUSH'),
430 'requires zlib.Z_SYNC_FLUSH')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000431 def test_odd_flush(self):
432 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
433 import random
Serhiy Storchaka43767632013-11-03 21:31:38 +0200434 # Testing on 17K of "random" data
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000435
Serhiy Storchaka43767632013-11-03 21:31:38 +0200436 # Create compressor and decompressor objects
437 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
438 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000439
Serhiy Storchaka43767632013-11-03 21:31:38 +0200440 # Try 17K of data
441 # generate random data stream
442 try:
443 # In 2.3 and later, WichmannHill is the RNG of the bug report
444 gen = random.WichmannHill()
445 except AttributeError:
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000446 try:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200447 # 2.2 called it Random
448 gen = random.Random()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000449 except AttributeError:
Serhiy Storchaka43767632013-11-03 21:31:38 +0200450 # others might simply have a single RNG
451 gen = random
452 gen.seed(1)
453 data = genblock(1, 17 * 1024, generator=gen)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000454
Serhiy Storchaka43767632013-11-03 21:31:38 +0200455 # compress, sync-flush, and decompress
456 first = co.compress(data)
457 second = co.flush(zlib.Z_SYNC_FLUSH)
458 expanded = dco.decompress(first + second)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000459
Serhiy Storchaka43767632013-11-03 21:31:38 +0200460 # if decompressed data is different from the input data, choke.
461 self.assertEqual(expanded, data, "17K random source doesn't match")
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000462
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000463 def test_empty_flush(self):
464 # Test that calling .flush() on unused objects works.
465 # (Bug #1083110 -- calling .flush() on decompress objects
466 # caused a core dump.)
467
468 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000469 self.assertTrue(co.flush()) # Returns a zlib header
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000470 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000471 self.assertEqual(dco.flush(), b"") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000472
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200473 def test_dictionary(self):
474 h = HAMLET_SCENE
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200475 # Build a simulated dictionary out of the words in HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200476 words = h.split()
477 random.shuffle(words)
478 zdict = b''.join(words)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200479 # Use it to compress HAMLET.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200480 co = zlib.compressobj(zdict=zdict)
481 cd = co.compress(h) + co.flush()
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200482 # Verify that it will decompress with the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200483 dco = zlib.decompressobj(zdict=zdict)
484 self.assertEqual(dco.decompress(cd) + dco.flush(), h)
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200485 # Verify that it fails when not given the dictionary.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200486 dco = zlib.decompressobj()
487 self.assertRaises(zlib.error, dco.decompress, cd)
488
489 def test_dictionary_streaming(self):
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200490 # This simulates the reuse of a compressor object for compressing
491 # several separate data streams.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200492 co = zlib.compressobj(zdict=HAMLET_SCENE)
493 do = zlib.decompressobj(zdict=HAMLET_SCENE)
494 piece = HAMLET_SCENE[1000:1500]
495 d0 = co.compress(piece) + co.flush(zlib.Z_SYNC_FLUSH)
496 d1 = co.compress(piece[100:]) + co.flush(zlib.Z_SYNC_FLUSH)
497 d2 = co.compress(piece[:-100]) + co.flush(zlib.Z_SYNC_FLUSH)
498 self.assertEqual(do.decompress(d0), piece)
499 self.assertEqual(do.decompress(d1), piece[100:])
500 self.assertEqual(do.decompress(d2), piece[:-100])
501
Antoine Pitrouc09c92f2010-05-11 23:36:40 +0000502 def test_decompress_incomplete_stream(self):
503 # This is 'foo', deflated
504 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E'
505 # For the record
506 self.assertEqual(zlib.decompress(x), b'foo')
507 self.assertRaises(zlib.error, zlib.decompress, x[:-5])
508 # Omitting the stream end works with decompressor objects
509 # (see issue #8672).
510 dco = zlib.decompressobj()
511 y = dco.decompress(x[:-5])
512 y += dco.flush()
513 self.assertEqual(y, b'foo')
514
Nadeem Vawda1c385462011-08-13 15:22:40 +0200515 def test_decompress_eof(self):
516 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
517 dco = zlib.decompressobj()
518 self.assertFalse(dco.eof)
519 dco.decompress(x[:-5])
520 self.assertFalse(dco.eof)
521 dco.decompress(x[-5:])
522 self.assertTrue(dco.eof)
523 dco.flush()
524 self.assertTrue(dco.eof)
525
526 def test_decompress_eof_incomplete_stream(self):
527 x = b'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E' # 'foo'
528 dco = zlib.decompressobj()
529 self.assertFalse(dco.eof)
530 dco.decompress(x[:-5])
531 self.assertFalse(dco.eof)
532 dco.flush()
533 self.assertFalse(dco.eof)
534
Nadeem Vawda39079942012-11-05 00:37:42 +0100535 def test_decompress_unused_data(self):
536 # Repeated calls to decompress() after EOF should accumulate data in
537 # dco.unused_data, instead of just storing the arg to the last call.
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100538 source = b'abcdefghijklmnopqrstuvwxyz'
539 remainder = b'0123456789'
540 y = zlib.compress(source)
541 x = y + remainder
542 for maxlen in 0, 1000:
543 for step in 1, 2, len(y), len(x):
544 dco = zlib.decompressobj()
545 data = b''
546 for i in range(0, len(x), step):
547 if i < len(y):
548 self.assertEqual(dco.unused_data, b'')
549 if maxlen == 0:
550 data += dco.decompress(x[i : i + step])
551 self.assertEqual(dco.unconsumed_tail, b'')
552 else:
553 data += dco.decompress(
554 dco.unconsumed_tail + x[i : i + step], maxlen)
555 data += dco.flush()
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100556 self.assertTrue(dco.eof)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100557 self.assertEqual(data, source)
558 self.assertEqual(dco.unconsumed_tail, b'')
559 self.assertEqual(dco.unused_data, remainder)
Nadeem Vawda39079942012-11-05 00:37:42 +0100560
Nadeem Vawda7ee95552012-11-11 03:15:32 +0100561 def test_flush_with_freed_input(self):
562 # Issue #16411: decompressor accesses input to last decompress() call
563 # in flush(), even if this object has been freed in the meanwhile.
564 input1 = b'abcdefghijklmnopqrstuvwxyz'
565 input2 = b'QWERTYUIOPASDFGHJKLZXCVBNM'
566 data = zlib.compress(input1)
567 dco = zlib.decompressobj()
568 dco.decompress(data, 1)
569 del data
570 data = zlib.compress(input2)
571 self.assertEqual(dco.flush(), input1[1:])
572
Martin Pantere99e9772015-11-20 08:13:35 +0000573 @bigmemtest(size=_4G, memuse=1)
574 def test_flush_large_length(self, size):
575 # Test flush(length) parameter greater than internal limit UINT_MAX
576 input = HAMLET_SCENE * 10
577 data = zlib.compress(input, 1)
578 dco = zlib.decompressobj()
579 dco.decompress(data, 1)
580 self.assertEqual(dco.flush(size), input[1:])
581
582 def test_flush_custom_length(self):
583 input = HAMLET_SCENE * 10
584 data = zlib.compress(input, 1)
585 dco = zlib.decompressobj()
586 dco.decompress(data, 1)
587 self.assertEqual(dco.flush(CustomInt()), input[1:])
588
Serhiy Storchaka43767632013-11-03 21:31:38 +0200589 @requires_Compress_copy
590 def test_compresscopy(self):
591 # Test copying a compression object
592 data0 = HAMLET_SCENE
593 data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii")
594 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
595 bufs0 = []
596 bufs0.append(c0.compress(data0))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000597
Serhiy Storchaka43767632013-11-03 21:31:38 +0200598 c1 = c0.copy()
599 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600
Serhiy Storchaka43767632013-11-03 21:31:38 +0200601 bufs0.append(c0.compress(data0))
602 bufs0.append(c0.flush())
603 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604
Serhiy Storchaka43767632013-11-03 21:31:38 +0200605 bufs1.append(c1.compress(data1))
606 bufs1.append(c1.flush())
607 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608
Serhiy Storchaka43767632013-11-03 21:31:38 +0200609 self.assertEqual(zlib.decompress(s0),data0+data0)
610 self.assertEqual(zlib.decompress(s1),data0+data1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611
Serhiy Storchaka43767632013-11-03 21:31:38 +0200612 @requires_Compress_copy
613 def test_badcompresscopy(self):
614 # Test copying a compression object in an inconsistent state
615 c = zlib.compressobj()
616 c.compress(HAMLET_SCENE)
617 c.flush()
618 self.assertRaises(ValueError, c.copy)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000619
Serhiy Storchaka43767632013-11-03 21:31:38 +0200620 @requires_Decompress_copy
621 def test_decompresscopy(self):
622 # Test copying a decompression object
623 data = HAMLET_SCENE
624 comp = zlib.compress(data)
625 # Test type of return value
626 self.assertIsInstance(comp, bytes)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627
Serhiy Storchaka43767632013-11-03 21:31:38 +0200628 d0 = zlib.decompressobj()
629 bufs0 = []
630 bufs0.append(d0.decompress(comp[:32]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631
Serhiy Storchaka43767632013-11-03 21:31:38 +0200632 d1 = d0.copy()
633 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634
Serhiy Storchaka43767632013-11-03 21:31:38 +0200635 bufs0.append(d0.decompress(comp[32:]))
636 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000637
Serhiy Storchaka43767632013-11-03 21:31:38 +0200638 bufs1.append(d1.decompress(comp[32:]))
639 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000640
Serhiy Storchaka43767632013-11-03 21:31:38 +0200641 self.assertEqual(s0,s1)
642 self.assertEqual(s0,data)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000643
Serhiy Storchaka43767632013-11-03 21:31:38 +0200644 @requires_Decompress_copy
645 def test_baddecompresscopy(self):
646 # Test copying a compression object in an inconsistent state
647 data = zlib.compress(HAMLET_SCENE)
648 d = zlib.decompressobj()
649 d.decompress(data)
650 d.flush()
651 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000652
Serhiy Storchakad7a44152015-11-12 11:23:04 +0200653 def test_compresspickle(self):
654 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
655 with self.assertRaises((TypeError, pickle.PicklingError)):
656 pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto)
657
658 def test_decompresspickle(self):
659 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
660 with self.assertRaises((TypeError, pickle.PicklingError)):
661 pickle.dumps(zlib.decompressobj(), proto)
662
Antoine Pitrou89562712010-05-07 17:04:02 +0000663 # Memory use of the following functions takes into account overallocation
664
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200665 @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
Antoine Pitrou89562712010-05-07 17:04:02 +0000666 def test_big_compress_buffer(self, size):
667 c = zlib.compressobj(1)
668 compress = lambda s: c.compress(s) + c.flush()
669 self.check_big_compress_buffer(size, compress)
670
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200671 @bigmemtest(size=_1G + 1024 * 1024, memuse=2)
Antoine Pitrou89562712010-05-07 17:04:02 +0000672 def test_big_decompress_buffer(self, size):
673 d = zlib.decompressobj()
674 decompress = lambda s: d.decompress(s) + d.flush()
675 self.check_big_decompress_buffer(size, decompress)
676
Nadeem Vawda197e22c2012-02-23 14:23:17 +0200677 @bigmemtest(size=_4G + 100, memuse=1, dry_run=False)
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200678 def test_length_overflow(self, size):
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200679 data = b'x' * size
Nadeem Vawda1161a9c2011-05-15 00:48:24 +0200680 c = zlib.compressobj(1)
681 d = zlib.decompressobj()
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200682 try:
Nadeem Vawda1161a9c2011-05-15 00:48:24 +0200683 self.assertRaises(OverflowError, c.compress, data)
684 self.assertRaises(OverflowError, d.decompress, data)
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200685 finally:
686 data = None
687
Antoine Pitrou89562712010-05-07 17:04:02 +0000688
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000689def genblock(seed, length, step=1024, generator=random):
690 """length-byte stream of random data from a seed (in step-byte blocks)."""
691 if seed is not None:
692 generator.seed(seed)
693 randint = generator.randint
694 if length < step or step < 2:
695 step = length
Guido van Rossum776152b2007-05-22 22:44:07 +0000696 blocks = bytes()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000697 for i in range(0, length, step):
Guido van Rossum776152b2007-05-22 22:44:07 +0000698 blocks += bytes(randint(0, 255) for x in range(step))
699 return blocks
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000700
701
702
703def choose_lines(source, number, seed=None, generator=random):
704 """Return a list of number lines randomly chosen from the source"""
705 if seed is not None:
706 generator.seed(seed)
707 sources = source.split('\n')
708 return [generator.choice(sources) for n in range(number)]
709
710
711
Guido van Rossum776152b2007-05-22 22:44:07 +0000712HAMLET_SCENE = b"""
Fred Drake004d5e62000-10-23 17:22:08 +0000713LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000714
715 O, fear me not.
716 I stay too long: but here my father comes.
717
718 Enter POLONIUS
719
720 A double blessing is a double grace,
721 Occasion smiles upon a second leave.
722
Fred Drake004d5e62000-10-23 17:22:08 +0000723LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000724
725 Yet here, Laertes! aboard, aboard, for shame!
726 The wind sits in the shoulder of your sail,
727 And you are stay'd for. There; my blessing with thee!
728 And these few precepts in thy memory
729 See thou character. Give thy thoughts no tongue,
730 Nor any unproportioned thought his act.
731 Be thou familiar, but by no means vulgar.
732 Those friends thou hast, and their adoption tried,
733 Grapple them to thy soul with hoops of steel;
734 But do not dull thy palm with entertainment
735 Of each new-hatch'd, unfledged comrade. Beware
736 Of entrance to a quarrel, but being in,
737 Bear't that the opposed may beware of thee.
738 Give every man thy ear, but few thy voice;
739 Take each man's censure, but reserve thy judgment.
740 Costly thy habit as thy purse can buy,
741 But not express'd in fancy; rich, not gaudy;
742 For the apparel oft proclaims the man,
743 And they in France of the best rank and station
744 Are of a most select and generous chief in that.
745 Neither a borrower nor a lender be;
746 For loan oft loses both itself and friend,
747 And borrowing dulls the edge of husbandry.
748 This above all: to thine ownself be true,
749 And it must follow, as the night the day,
750 Thou canst not then be false to any man.
751 Farewell: my blessing season this in thee!
752
Fred Drake004d5e62000-10-23 17:22:08 +0000753LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000754
755 Most humbly do I take my leave, my lord.
756
Fred Drake004d5e62000-10-23 17:22:08 +0000757LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000758
759 The time invites you; go; your servants tend.
760
Fred Drake004d5e62000-10-23 17:22:08 +0000761LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000762
763 Farewell, Ophelia; and remember well
764 What I have said to you.
765
Fred Drake004d5e62000-10-23 17:22:08 +0000766OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000767
768 'Tis in my memory lock'd,
769 And you yourself shall keep the key of it.
770
Fred Drake004d5e62000-10-23 17:22:08 +0000771LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000772
773 Farewell.
774"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000775
776
Martin Pantere99e9772015-11-20 08:13:35 +0000777class CustomInt:
778 def __int__(self):
779 return 100
780
781
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000782if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500783 unittest.main()