1 | #!/usr/bin/env python
|
---|
2 | from test import test_support
|
---|
3 | from test.test_support import TESTFN, _4G, bigmemtest, import_module, findfile
|
---|
4 |
|
---|
5 | import unittest
|
---|
6 | from cStringIO import StringIO
|
---|
7 | import os
|
---|
8 | import subprocess
|
---|
9 | import sys
|
---|
10 |
|
---|
11 | try:
|
---|
12 | import threading
|
---|
13 | except ImportError:
|
---|
14 | threading = None
|
---|
15 |
|
---|
16 | bz2 = import_module('bz2')
|
---|
17 | from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
|
---|
18 |
|
---|
19 | has_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx", "riscos")
|
---|
20 |
|
---|
21 | class BaseTest(unittest.TestCase):
|
---|
22 | "Base for other testcases."
|
---|
23 | TEXT = 'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:\ndaemon:x:2:2:daemon:/sbin:\nadm:x:3:4:adm:/var/adm:\nlp:x:4:7:lp:/var/spool/lpd:\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\nhalt:x:7:0:halt:/sbin:/sbin/halt\nmail:x:8:12:mail:/var/spool/mail:\nnews:x:9:13:news:/var/spool/news:\nuucp:x:10:14:uucp:/var/spool/uucp:\noperator:x:11:0:operator:/root:\ngames:x:12:100:games:/usr/games:\ngopher:x:13:30:gopher:/usr/lib/gopher-data:\nftp:x:14:50:FTP User:/var/ftp:/bin/bash\nnobody:x:65534:65534:Nobody:/home:\npostfix:x:100:101:postfix:/var/spool/postfix:\nniemeyer:x:500:500::/home/niemeyer:/bin/bash\npostgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\nmysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\nwww:x:103:104::/var/www:/bin/false\n'
|
---|
24 | DATA = 'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1\x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10\x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
|
---|
25 | DATA_CRLF = 'BZh91AY&SY\xaez\xbbN\x00\x01H\xdf\x80\x00\x12@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe0@\x01\xbc\xc6`\x86*\x8d=M\xa9\x9a\x86\xd0L@\x0fI\xa6!\xa1\x13\xc8\x88jdi\x8d@\x03@\x1a\x1a\x0c\x0c\x83 \x00\xc4h2\x19\x01\x82D\x84e\t\xe8\x99\x89\x19\x1ah\x00\r\x1a\x11\xaf\x9b\x0fG\xf5(\x1b\x1f?\t\x12\xcf\xb5\xfc\x95E\x00ps\x89\x12^\xa4\xdd\xa2&\x05(\x87\x04\x98\x89u\xe40%\xb6\x19\'\x8c\xc4\x89\xca\x07\x0e\x1b!\x91UIFU%C\x994!DI\xd2\xfa\xf0\xf1N8W\xde\x13A\xf5\x9cr%?\x9f3;I45A\xd1\x8bT\xb1<l\xba\xcb_\xc00xY\x17r\x17\x88\x08\x08@\xa0\ry@\x10\x04$)`\xf2\xce\x89z\xb0s\xec\x9b.iW\x9d\x81\xb5-+t\x9f\x1a\'\x97dB\xf5x\xb5\xbe.[.\xd7\x0e\x81\xe7\x08\x1cN`\x88\x10\xca\x87\xc3!"\x80\x92R\xa1/\xd1\xc0\xe6mf\xac\xbd\x99\xcca\xb3\x8780>\xa4\xc7\x8d\x1a\\"\xad\xa1\xabyBg\x15\xb9l\x88\x88\x91k"\x94\xa4\xd4\x89\xae*\xa6\x0b\x10\x0c\xd6\xd4m\xe86\xec\xb5j\x8a\x86j\';\xca.\x01I\xf2\xaaJ\xe8\x88\x8cU+t3\xfb\x0c\n\xa33\x13r2\r\x16\xe0\xb3(\xbf\x1d\x83r\xe7M\xf0D\x1365\xd8\x88\xd3\xa4\x92\xcb2\x06\x04\\\xc1\xb0\xea//\xbek&\xd8\xe6+t\xe5\xa1\x13\xada\x16\xder5"w]\xa2i\xb7[\x97R \xe2IT\xcd;Z\x04dk4\xad\x8a\t\xd3\x81z\x10\xf1:^`\xab\x1f\xc5\xdc\x91N\x14$+\x9e\xae\xd3\x80'
|
---|
26 | EMPTY_DATA = 'BZh9\x17rE8P\x90\x00\x00\x00\x00'
|
---|
27 |
|
---|
28 | if has_cmdline_bunzip2:
|
---|
29 | def decompress(self, data):
|
---|
30 | pop = subprocess.Popen("bunzip2", shell=True,
|
---|
31 | stdin=subprocess.PIPE,
|
---|
32 | stdout=subprocess.PIPE,
|
---|
33 | stderr=subprocess.STDOUT)
|
---|
34 | pop.stdin.write(data)
|
---|
35 | pop.stdin.close()
|
---|
36 | ret = pop.stdout.read()
|
---|
37 | pop.stdout.close()
|
---|
38 | if pop.wait() != 0:
|
---|
39 | ret = bz2.decompress(data)
|
---|
40 | return ret
|
---|
41 |
|
---|
42 | else:
|
---|
43 | # bunzip2 isn't available to run on Windows.
|
---|
44 | def decompress(self, data):
|
---|
45 | return bz2.decompress(data)
|
---|
46 |
|
---|
47 |
|
---|
48 | class BZ2FileTest(BaseTest):
|
---|
49 | "Test BZ2File type miscellaneous methods."
|
---|
50 |
|
---|
51 | def setUp(self):
|
---|
52 | self.filename = TESTFN
|
---|
53 |
|
---|
54 | def tearDown(self):
|
---|
55 | if os.path.isfile(self.filename):
|
---|
56 | os.unlink(self.filename)
|
---|
57 |
|
---|
58 | def createTempFile(self, crlf=0):
|
---|
59 | with open(self.filename, "wb") as f:
|
---|
60 | if crlf:
|
---|
61 | data = self.DATA_CRLF
|
---|
62 | else:
|
---|
63 | data = self.DATA
|
---|
64 | f.write(data)
|
---|
65 |
|
---|
66 | def testRead(self):
|
---|
67 | # "Test BZ2File.read()"
|
---|
68 | self.createTempFile()
|
---|
69 | with BZ2File(self.filename) as bz2f:
|
---|
70 | self.assertRaises(TypeError, bz2f.read, None)
|
---|
71 | self.assertEqual(bz2f.read(), self.TEXT)
|
---|
72 |
|
---|
73 | def testRead0(self):
|
---|
74 | # Test BBZ2File.read(0)"
|
---|
75 | self.createTempFile()
|
---|
76 | with BZ2File(self.filename) as bz2f:
|
---|
77 | self.assertRaises(TypeError, bz2f.read, None)
|
---|
78 | self.assertEqual(bz2f.read(0), "")
|
---|
79 |
|
---|
80 | def testReadChunk10(self):
|
---|
81 | # "Test BZ2File.read() in chunks of 10 bytes"
|
---|
82 | self.createTempFile()
|
---|
83 | with BZ2File(self.filename) as bz2f:
|
---|
84 | text = ''
|
---|
85 | while 1:
|
---|
86 | str = bz2f.read(10)
|
---|
87 | if not str:
|
---|
88 | break
|
---|
89 | text += str
|
---|
90 | self.assertEqual(text, self.TEXT)
|
---|
91 |
|
---|
92 | def testRead100(self):
|
---|
93 | # "Test BZ2File.read(100)"
|
---|
94 | self.createTempFile()
|
---|
95 | with BZ2File(self.filename) as bz2f:
|
---|
96 | self.assertEqual(bz2f.read(100), self.TEXT[:100])
|
---|
97 |
|
---|
98 | def testReadLine(self):
|
---|
99 | # "Test BZ2File.readline()"
|
---|
100 | self.createTempFile()
|
---|
101 | with BZ2File(self.filename) as bz2f:
|
---|
102 | self.assertRaises(TypeError, bz2f.readline, None)
|
---|
103 | sio = StringIO(self.TEXT)
|
---|
104 | for line in sio.readlines():
|
---|
105 | self.assertEqual(bz2f.readline(), line)
|
---|
106 |
|
---|
107 | def testReadLines(self):
|
---|
108 | # "Test BZ2File.readlines()"
|
---|
109 | self.createTempFile()
|
---|
110 | with BZ2File(self.filename) as bz2f:
|
---|
111 | self.assertRaises(TypeError, bz2f.readlines, None)
|
---|
112 | sio = StringIO(self.TEXT)
|
---|
113 | self.assertEqual(bz2f.readlines(), sio.readlines())
|
---|
114 |
|
---|
115 | def testIterator(self):
|
---|
116 | # "Test iter(BZ2File)"
|
---|
117 | self.createTempFile()
|
---|
118 | with BZ2File(self.filename) as bz2f:
|
---|
119 | sio = StringIO(self.TEXT)
|
---|
120 | self.assertEqual(list(iter(bz2f)), sio.readlines())
|
---|
121 |
|
---|
122 | def testClosedIteratorDeadlock(self):
|
---|
123 | # "Test that iteration on a closed bz2file releases the lock."
|
---|
124 | # http://bugs.python.org/issue3309
|
---|
125 | self.createTempFile()
|
---|
126 | bz2f = BZ2File(self.filename)
|
---|
127 | bz2f.close()
|
---|
128 | self.assertRaises(ValueError, bz2f.next)
|
---|
129 | # This call will deadlock of the above .next call failed to
|
---|
130 | # release the lock.
|
---|
131 | self.assertRaises(ValueError, bz2f.readlines)
|
---|
132 |
|
---|
133 | def testXReadLines(self):
|
---|
134 | # "Test BZ2File.xreadlines()"
|
---|
135 | self.createTempFile()
|
---|
136 | bz2f = BZ2File(self.filename)
|
---|
137 | sio = StringIO(self.TEXT)
|
---|
138 | self.assertEqual(list(bz2f.xreadlines()), sio.readlines())
|
---|
139 | bz2f.close()
|
---|
140 |
|
---|
141 | def testUniversalNewlinesLF(self):
|
---|
142 | # "Test BZ2File.read() with universal newlines (\\n)"
|
---|
143 | self.createTempFile()
|
---|
144 | bz2f = BZ2File(self.filename, "rU")
|
---|
145 | self.assertEqual(bz2f.read(), self.TEXT)
|
---|
146 | self.assertEqual(bz2f.newlines, "\n")
|
---|
147 | bz2f.close()
|
---|
148 |
|
---|
149 | def testUniversalNewlinesCRLF(self):
|
---|
150 | # "Test BZ2File.read() with universal newlines (\\r\\n)"
|
---|
151 | self.createTempFile(crlf=1)
|
---|
152 | bz2f = BZ2File(self.filename, "rU")
|
---|
153 | self.assertEqual(bz2f.read(), self.TEXT)
|
---|
154 | self.assertEqual(bz2f.newlines, "\r\n")
|
---|
155 | bz2f.close()
|
---|
156 |
|
---|
157 | def testWrite(self):
|
---|
158 | # "Test BZ2File.write()"
|
---|
159 | with BZ2File(self.filename, "w") as bz2f:
|
---|
160 | self.assertRaises(TypeError, bz2f.write)
|
---|
161 | bz2f.write(self.TEXT)
|
---|
162 | with open(self.filename, 'rb') as f:
|
---|
163 | self.assertEqual(self.decompress(f.read()), self.TEXT)
|
---|
164 |
|
---|
165 | def testWriteChunks10(self):
|
---|
166 | # "Test BZ2File.write() with chunks of 10 bytes"
|
---|
167 | with BZ2File(self.filename, "w") as bz2f:
|
---|
168 | n = 0
|
---|
169 | while 1:
|
---|
170 | str = self.TEXT[n*10:(n+1)*10]
|
---|
171 | if not str:
|
---|
172 | break
|
---|
173 | bz2f.write(str)
|
---|
174 | n += 1
|
---|
175 | with open(self.filename, 'rb') as f:
|
---|
176 | self.assertEqual(self.decompress(f.read()), self.TEXT)
|
---|
177 |
|
---|
178 | def testWriteLines(self):
|
---|
179 | # "Test BZ2File.writelines()"
|
---|
180 | with BZ2File(self.filename, "w") as bz2f:
|
---|
181 | self.assertRaises(TypeError, bz2f.writelines)
|
---|
182 | sio = StringIO(self.TEXT)
|
---|
183 | bz2f.writelines(sio.readlines())
|
---|
184 | # patch #1535500
|
---|
185 | self.assertRaises(ValueError, bz2f.writelines, ["a"])
|
---|
186 | with open(self.filename, 'rb') as f:
|
---|
187 | self.assertEqual(self.decompress(f.read()), self.TEXT)
|
---|
188 |
|
---|
189 | def testWriteMethodsOnReadOnlyFile(self):
|
---|
190 | with BZ2File(self.filename, "w") as bz2f:
|
---|
191 | bz2f.write("abc")
|
---|
192 |
|
---|
193 | with BZ2File(self.filename, "r") as bz2f:
|
---|
194 | self.assertRaises(IOError, bz2f.write, "a")
|
---|
195 | self.assertRaises(IOError, bz2f.writelines, ["a"])
|
---|
196 |
|
---|
197 | def testSeekForward(self):
|
---|
198 | # "Test BZ2File.seek(150, 0)"
|
---|
199 | self.createTempFile()
|
---|
200 | with BZ2File(self.filename) as bz2f:
|
---|
201 | self.assertRaises(TypeError, bz2f.seek)
|
---|
202 | bz2f.seek(150)
|
---|
203 | self.assertEqual(bz2f.read(), self.TEXT[150:])
|
---|
204 |
|
---|
205 | def testSeekBackwards(self):
|
---|
206 | # "Test BZ2File.seek(-150, 1)"
|
---|
207 | self.createTempFile()
|
---|
208 | with BZ2File(self.filename) as bz2f:
|
---|
209 | bz2f.read(500)
|
---|
210 | bz2f.seek(-150, 1)
|
---|
211 | self.assertEqual(bz2f.read(), self.TEXT[500-150:])
|
---|
212 |
|
---|
213 | def testSeekBackwardsFromEnd(self):
|
---|
214 | # "Test BZ2File.seek(-150, 2)"
|
---|
215 | self.createTempFile()
|
---|
216 | with BZ2File(self.filename) as bz2f:
|
---|
217 | bz2f.seek(-150, 2)
|
---|
218 | self.assertEqual(bz2f.read(), self.TEXT[len(self.TEXT)-150:])
|
---|
219 |
|
---|
220 | def testSeekPostEnd(self):
|
---|
221 | # "Test BZ2File.seek(150000)"
|
---|
222 | self.createTempFile()
|
---|
223 | with BZ2File(self.filename) as bz2f:
|
---|
224 | bz2f.seek(150000)
|
---|
225 | self.assertEqual(bz2f.tell(), len(self.TEXT))
|
---|
226 | self.assertEqual(bz2f.read(), "")
|
---|
227 |
|
---|
228 | def testSeekPostEndTwice(self):
|
---|
229 | # "Test BZ2File.seek(150000) twice"
|
---|
230 | self.createTempFile()
|
---|
231 | with BZ2File(self.filename) as bz2f:
|
---|
232 | bz2f.seek(150000)
|
---|
233 | bz2f.seek(150000)
|
---|
234 | self.assertEqual(bz2f.tell(), len(self.TEXT))
|
---|
235 | self.assertEqual(bz2f.read(), "")
|
---|
236 |
|
---|
237 | def testSeekPreStart(self):
|
---|
238 | # "Test BZ2File.seek(-150, 0)"
|
---|
239 | self.createTempFile()
|
---|
240 | with BZ2File(self.filename) as bz2f:
|
---|
241 | bz2f.seek(-150)
|
---|
242 | self.assertEqual(bz2f.tell(), 0)
|
---|
243 | self.assertEqual(bz2f.read(), self.TEXT)
|
---|
244 |
|
---|
245 | def testOpenDel(self):
|
---|
246 | # "Test opening and deleting a file many times"
|
---|
247 | self.createTempFile()
|
---|
248 | for i in xrange(10000):
|
---|
249 | o = BZ2File(self.filename)
|
---|
250 | del o
|
---|
251 |
|
---|
252 | def testOpenNonexistent(self):
|
---|
253 | # "Test opening a nonexistent file"
|
---|
254 | self.assertRaises(IOError, BZ2File, "/non/existent")
|
---|
255 |
|
---|
256 | def testModeU(self):
|
---|
257 | # Bug #1194181: bz2.BZ2File opened for write with mode "U"
|
---|
258 | self.createTempFile()
|
---|
259 | bz2f = BZ2File(self.filename, "U")
|
---|
260 | bz2f.close()
|
---|
261 | f = file(self.filename)
|
---|
262 | f.seek(0, 2)
|
---|
263 | self.assertEqual(f.tell(), len(self.DATA))
|
---|
264 | f.close()
|
---|
265 |
|
---|
266 | def testBug1191043(self):
|
---|
267 | # readlines() for files containing no newline
|
---|
268 | data = 'BZh91AY&SY\xd9b\x89]\x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00!\x9ah3M\x13<]\xc9\x14\xe1BCe\x8a%t'
|
---|
269 | with open(self.filename, "wb") as f:
|
---|
270 | f.write(data)
|
---|
271 | with BZ2File(self.filename) as bz2f:
|
---|
272 | lines = bz2f.readlines()
|
---|
273 | self.assertEqual(lines, ['Test'])
|
---|
274 | with BZ2File(self.filename) as bz2f:
|
---|
275 | xlines = list(bz2f.readlines())
|
---|
276 | self.assertEqual(xlines, ['Test'])
|
---|
277 |
|
---|
278 | def testContextProtocol(self):
|
---|
279 | # BZ2File supports the context management protocol
|
---|
280 | f = None
|
---|
281 | with BZ2File(self.filename, "wb") as f:
|
---|
282 | f.write(b"xxx")
|
---|
283 | f = BZ2File(self.filename, "rb")
|
---|
284 | f.close()
|
---|
285 | try:
|
---|
286 | with f:
|
---|
287 | pass
|
---|
288 | except ValueError:
|
---|
289 | pass
|
---|
290 | else:
|
---|
291 | self.fail("__enter__ on a closed file didn't raise an exception")
|
---|
292 | try:
|
---|
293 | with BZ2File(self.filename, "wb") as f:
|
---|
294 | 1 // 0
|
---|
295 | except ZeroDivisionError:
|
---|
296 | pass
|
---|
297 | else:
|
---|
298 | self.fail("1 // 0 didn't raise an exception")
|
---|
299 |
|
---|
300 | @unittest.skipUnless(threading, 'Threading required for this test.')
|
---|
301 | def testThreading(self):
|
---|
302 | # Using a BZ2File from several threads doesn't deadlock (issue #7205).
|
---|
303 | data = "1" * 2**20
|
---|
304 | nthreads = 10
|
---|
305 | with bz2.BZ2File(self.filename, 'wb') as f:
|
---|
306 | def comp():
|
---|
307 | for i in range(5):
|
---|
308 | f.write(data)
|
---|
309 | threads = [threading.Thread(target=comp) for i in range(nthreads)]
|
---|
310 | for t in threads:
|
---|
311 | t.start()
|
---|
312 | for t in threads:
|
---|
313 | t.join()
|
---|
314 |
|
---|
315 | def testMixedIterationReads(self):
|
---|
316 | # Issue #8397: mixed iteration and reads should be forbidden.
|
---|
317 | with bz2.BZ2File(self.filename, 'wb') as f:
|
---|
318 | # The internal buffer size is hard-wired to 8192 bytes, we must
|
---|
319 | # write out more than that for the test to stop half through
|
---|
320 | # the buffer.
|
---|
321 | f.write(self.TEXT * 100)
|
---|
322 | with bz2.BZ2File(self.filename, 'rb') as f:
|
---|
323 | next(f)
|
---|
324 | self.assertRaises(ValueError, f.read)
|
---|
325 | self.assertRaises(ValueError, f.readline)
|
---|
326 | self.assertRaises(ValueError, f.readlines)
|
---|
327 |
|
---|
328 | class BZ2CompressorTest(BaseTest):
|
---|
329 | def testCompress(self):
|
---|
330 | # "Test BZ2Compressor.compress()/flush()"
|
---|
331 | bz2c = BZ2Compressor()
|
---|
332 | self.assertRaises(TypeError, bz2c.compress)
|
---|
333 | data = bz2c.compress(self.TEXT)
|
---|
334 | data += bz2c.flush()
|
---|
335 | self.assertEqual(self.decompress(data), self.TEXT)
|
---|
336 |
|
---|
337 | def testCompressEmptyString(self):
|
---|
338 | # "Test BZ2Compressor.compress()/flush() of empty string"
|
---|
339 | bz2c = BZ2Compressor()
|
---|
340 | data = bz2c.compress('')
|
---|
341 | data += bz2c.flush()
|
---|
342 | self.assertEqual(data, self.EMPTY_DATA)
|
---|
343 |
|
---|
344 | def testCompressChunks10(self):
|
---|
345 | # "Test BZ2Compressor.compress()/flush() with chunks of 10 bytes"
|
---|
346 | bz2c = BZ2Compressor()
|
---|
347 | n = 0
|
---|
348 | data = ''
|
---|
349 | while 1:
|
---|
350 | str = self.TEXT[n*10:(n+1)*10]
|
---|
351 | if not str:
|
---|
352 | break
|
---|
353 | data += bz2c.compress(str)
|
---|
354 | n += 1
|
---|
355 | data += bz2c.flush()
|
---|
356 | self.assertEqual(self.decompress(data), self.TEXT)
|
---|
357 |
|
---|
358 | @bigmemtest(_4G, memuse=1.25)
|
---|
359 | def testBigmem(self, size):
|
---|
360 | text = "a" * size
|
---|
361 | bz2c = bz2.BZ2Compressor()
|
---|
362 | data = bz2c.compress(text) + bz2c.flush()
|
---|
363 | del text
|
---|
364 | text = self.decompress(data)
|
---|
365 | self.assertEqual(len(text), size)
|
---|
366 | self.assertEqual(text.strip("a"), "")
|
---|
367 |
|
---|
368 |
|
---|
369 | class BZ2DecompressorTest(BaseTest):
|
---|
370 | def test_Constructor(self):
|
---|
371 | self.assertRaises(TypeError, BZ2Decompressor, 42)
|
---|
372 |
|
---|
373 | def testDecompress(self):
|
---|
374 | # "Test BZ2Decompressor.decompress()"
|
---|
375 | bz2d = BZ2Decompressor()
|
---|
376 | self.assertRaises(TypeError, bz2d.decompress)
|
---|
377 | text = bz2d.decompress(self.DATA)
|
---|
378 | self.assertEqual(text, self.TEXT)
|
---|
379 |
|
---|
380 | def testDecompressChunks10(self):
|
---|
381 | # "Test BZ2Decompressor.decompress() with chunks of 10 bytes"
|
---|
382 | bz2d = BZ2Decompressor()
|
---|
383 | text = ''
|
---|
384 | n = 0
|
---|
385 | while 1:
|
---|
386 | str = self.DATA[n*10:(n+1)*10]
|
---|
387 | if not str:
|
---|
388 | break
|
---|
389 | text += bz2d.decompress(str)
|
---|
390 | n += 1
|
---|
391 | self.assertEqual(text, self.TEXT)
|
---|
392 |
|
---|
393 | def testDecompressUnusedData(self):
|
---|
394 | # "Test BZ2Decompressor.decompress() with unused data"
|
---|
395 | bz2d = BZ2Decompressor()
|
---|
396 | unused_data = "this is unused data"
|
---|
397 | text = bz2d.decompress(self.DATA+unused_data)
|
---|
398 | self.assertEqual(text, self.TEXT)
|
---|
399 | self.assertEqual(bz2d.unused_data, unused_data)
|
---|
400 |
|
---|
401 | def testEOFError(self):
|
---|
402 | # "Calling BZ2Decompressor.decompress() after EOS must raise EOFError"
|
---|
403 | bz2d = BZ2Decompressor()
|
---|
404 | text = bz2d.decompress(self.DATA)
|
---|
405 | self.assertRaises(EOFError, bz2d.decompress, "anything")
|
---|
406 | self.assertRaises(EOFError, bz2d.decompress, "")
|
---|
407 |
|
---|
408 | @bigmemtest(_4G, memuse=1.25)
|
---|
409 | def testBigmem(self, size):
|
---|
410 | # Issue #14398: decompression fails when output data is >=2GB.
|
---|
411 | if size < _4G:
|
---|
412 | self.skipTest("Test needs 5GB of memory to run.")
|
---|
413 | compressed = bz2.compress("a" * _4G)
|
---|
414 | text = bz2.BZ2Decompressor().decompress(compressed)
|
---|
415 | self.assertEqual(len(text), _4G)
|
---|
416 | self.assertEqual(text.strip("a"), "")
|
---|
417 |
|
---|
418 |
|
---|
419 | class FuncTest(BaseTest):
|
---|
420 | "Test module functions"
|
---|
421 |
|
---|
422 | def testCompress(self):
|
---|
423 | # "Test compress() function"
|
---|
424 | data = bz2.compress(self.TEXT)
|
---|
425 | self.assertEqual(self.decompress(data), self.TEXT)
|
---|
426 |
|
---|
427 | def testCompressEmptyString(self):
|
---|
428 | # "Test compress() of empty string"
|
---|
429 | text = bz2.compress('')
|
---|
430 | self.assertEqual(text, self.EMPTY_DATA)
|
---|
431 |
|
---|
432 | def testDecompress(self):
|
---|
433 | # "Test decompress() function"
|
---|
434 | text = bz2.decompress(self.DATA)
|
---|
435 | self.assertEqual(text, self.TEXT)
|
---|
436 |
|
---|
437 | def testDecompressEmpty(self):
|
---|
438 | # "Test decompress() function with empty string"
|
---|
439 | text = bz2.decompress("")
|
---|
440 | self.assertEqual(text, "")
|
---|
441 |
|
---|
442 | def testDecompressToEmptyString(self):
|
---|
443 | # "Test decompress() of minimal bz2 data to empty string"
|
---|
444 | text = bz2.decompress(self.EMPTY_DATA)
|
---|
445 | self.assertEqual(text, '')
|
---|
446 |
|
---|
447 | def testDecompressIncomplete(self):
|
---|
448 | # "Test decompress() function with incomplete data"
|
---|
449 | self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10])
|
---|
450 |
|
---|
451 | @bigmemtest(_4G, memuse=1.25)
|
---|
452 | def testCompressBigmem(self, size):
|
---|
453 | text = "a" * size
|
---|
454 | data = bz2.compress(text)
|
---|
455 | del text
|
---|
456 | text = self.decompress(data)
|
---|
457 | self.assertEqual(len(text), size)
|
---|
458 | self.assertEqual(text.strip("a"), "")
|
---|
459 |
|
---|
460 | @bigmemtest(_4G, memuse=1.25)
|
---|
461 | def testDecompressBigmem(self, size):
|
---|
462 | # Issue #14398: decompression fails when output data is >=2GB.
|
---|
463 | if size < _4G:
|
---|
464 | self.skipTest("Test needs 5GB of memory to run.")
|
---|
465 | compressed = bz2.compress("a" * _4G)
|
---|
466 | text = bz2.decompress(compressed)
|
---|
467 | self.assertEqual(len(text), _4G)
|
---|
468 | self.assertEqual(text.strip("a"), "")
|
---|
469 |
|
---|
470 | def test_main():
|
---|
471 | test_support.run_unittest(
|
---|
472 | BZ2FileTest,
|
---|
473 | BZ2CompressorTest,
|
---|
474 | BZ2DecompressorTest,
|
---|
475 | FuncTest
|
---|
476 | )
|
---|
477 | test_support.reap_children()
|
---|
478 |
|
---|
479 | if __name__ == '__main__':
|
---|
480 | test_main()
|
---|
481 |
|
---|
482 | # vim:ts=4:sw=4
|
---|