]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_bz2.py
2 from test
import test_support
3 from test
. test_support
import TESTFN
, import_module
6 from cStringIO
import StringIO
16 bz2
= import_module ( 'bz2' )
17 from bz2
import BZ2File
, BZ2Compressor
, BZ2Decompressor
19 has_cmdline_bunzip2
= sys
. platform
not in ( "win32" , "os2emx" , "riscos" )
21 class BaseTest ( unittest
. TestCase
):
22 "Base for other testcases."
23 TEXT
= 'root:x:0:0:root:/root:/bin/bash \n bin:x:1:1:bin:/bin: \n daemon:x:2:2:daemon:/sbin: \n adm:x:3:4:adm:/var/adm: \n lp:x:4:7:lp:/var/spool/lpd: \n sync:x:5:0:sync:/sbin:/bin/sync \n shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown \n halt:x:7:0:halt:/sbin:/sbin/halt \n mail:x:8:12:mail:/var/spool/mail: \n news:x:9:13:news:/var/spool/news: \n uucp:x:10:14:uucp:/var/spool/uucp: \n operator:x:11:0:operator:/root: \n games:x:12:100:games:/usr/games: \n gopher:x:13:30:gopher:/usr/lib/gopher-data: \n ftp:x:14:50:FTP User:/var/ftp:/bin/bash \n nobody:x:65534:65534:Nobody:/home: \n postfix:x:100:101:postfix:/var/spool/postfix: \n niemeyer:x:500:500::/home/niemeyer:/bin/bash \n postgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash \n mysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash \n www:x:103:104::/var/www:/bin/false \n '
24 DATA
= 'BZh91AY&SY. \xc8 N \x18\x00\x01 >_ \x80\x00\x10 @ \x02\xff\xf0\x01\x07 n \x00 ? \xe7\xff\xe0 0 \x01\x99\xaa\x00\xc0\x03 F \x86\x8c #& \x83 F \x9a\x03\x06\xa6\xd0\xa6\x93 M \x0f Q \xa7\xa8\x06\x80 4hh \x12 $ \x11\xa4 i4 \xf1 4S \xd2 <Q \xb5\x0f H \xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xaf I \x12\xe1\xc9\xf8 /E \x00 pu \x89\x12 ] \xc9\xbb DL \n Q \x0e\t 1 \x12\xdf\xa0\xc0\x97\xac 2O9 \x89\x13\x94\x0e\x1c 7 \x0e d \x95 I \x0c\xaa J \xa4\x18 L \x10\x05 # \x9c\xaf\xba\xbc / \x97\x8a #C \xc8\xe1\x8c W \xf9\xe2\xd0\xd6 M \xa7\x8b Xa<e \x84 t \xcb L \xb3\xa7\xd9\xcd\xd1\xcb\x84 . \xaf\xb3\xab\xab\xad `n} \xa0 lh \t E, \x8e Z \x15\x17 VH> \x88\xe5\xcd 9gd6 \x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08 .K \x8e v \xfb\xf7 xw \xbb\xdf\xa1\x92\xf1\xdd |/"; \xa2\xba\x9f\xd5\xb1 #A \xb6\xf6\xb3 o \xc9\xc5 y \\\xeb O \xe7\x85\x9a\xbc\xb6 f8 \x95 2 \xd5\xd7 "% \x89 >V, \xf7\xa6 z \xe2\x9f\xa3\xdf\x11\x11 " \xd6 E)I \xa9\x13 ^ \xca\xf3 r \xd0\x03 U \x92 2 \xf2 6 \xec\xb6\xed\x8b\xc3 U \x13\x9d\xc5\x17 0 \xa4\xfa ^ \x92\xac DF \x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80 ankR \x8b\xe5\xd8 3] \xa9\xc6\x08\x82 f \xf6\xb9 "6l$ \xb8 j@ \xc0\x8a\xb0 l1.. \xba k \x83 ls \x15\xbc\xf4\xc1\x13\xbe\xf8 E \xb8\x9d\r\xa8\x9d k \x84\xd3 n \xfa\xac Q \x07\xb1 %y \xaa v \xb4\x08\xe0 z \x1b\x16\xf5\x04\xe9\xcc\xb9\x08 z \x1e n7.G \xfc ] \xc9\x14\xe1 B@ \xbb !8`'
25 DATA_CRLF
= 'BZh91AY&SY \xae z \xbb N \x00\x01 H \xdf\x80\x00\x12 @ \x02\xff\xf0\x01\x07 n \x00 ? \xe7\xff\xe0 @ \x01\xbc\xc6 ` \x86 * \x8d =M \xa9\x9a\x86\xd0 L@ \x0f I \xa6 ! \xa1\x13\xc8\x88 jdi \x8d @ \x03 @ \x1a\x1a\x0c\x0c\x83 \x00\xc4 h2 \x19\x01\x82 D \x84 e \t\xe8\x99\x89\x19\x1a h \x00\r\x1a\x11\xaf\x9b\x0f G \xf5 ( \x1b\x1f ? \t\x12\xcf\xb5\xfc\x95 E \x00 ps \x89\x12 ^ \xa4\xdd\xa2 & \x05 ( \x87\x04\x98\x89 u \xe4 0% \xb6\x19\'\x8c\xc4\x89\xca\x07\x0e\x1b ! \x91 UIFU%C \x99 4!DI \xd2\xfa\xf0\xf1 N8W \xde\x13 A \xf5\x9c r%? \x9f 3;I45A \xd1\x8b T \xb1 <l \xba\xcb _ \xc0 0xY \x17 r \x17\x88\x08\x08 @ \xa0\r y@ \x10\x04 $)` \xf2\xce\x89 z \xb0 s \xec\x9b .iW \x9d\x81\xb5 -+t \x9f\x1a\'\x97 dB \xf5 x \xb5\xbe .[. \xd7\x0e\x81\xe7\x08\x1c N` \x88\x10\xca\x87\xc3 !" \x80\x92 R \xa1 / \xd1\xc0\xe6 mf \xac\xbd\x99\xcc a \xb3\x87 80> \xa4\xc7\x8d\x1a\\ " \xad\xa1\xab yBg \x15\xb9 l \x88\x88\x91 k" \x94\xa4\xd4\x89\xae * \xa6\x0b\x10\x0c\xd6\xd4 m \xe8 6 \xec\xb5 j \x8a\x86 j \' ; \xca . \x01 I \xf2\xaa J \xe8\x88\x8c U+t3 \xfb\x0c\n\xa3 3 \x13 r2 \r\x16\xe0\xb3 ( \xbf\x1d\x83 r \xe7 M \xf0 D \x13 65 \xd8\x88\xd3\xa4\x92\xcb 2 \x06\x04\\\xc1\xb0\xea // \xbe k& \xd8\xe6 +t \xe5\xa1\x13\xad a \x16\xde r5"w] \xa2 i \xb7 [ \x97 R \xe2 IT \xcd ;Z \x04 dk4 \xad\x8a\t\xd3\x81 z \x10\xf1 :^` \xab\x1f\xc5\xdc\x91 N \x14 $+ \x9e\xae\xd3\x80 '
27 if has_cmdline_bunzip2
:
28 def decompress ( self
, data
):
29 pop
= subprocess
. Popen ( "bunzip2" , shell
= True ,
30 stdin
= subprocess
. PIPE
,
31 stdout
= subprocess
. PIPE
,
32 stderr
= subprocess
. STDOUT
)
35 ret
= pop
. stdout
. read ()
38 ret
= bz2
. decompress ( data
)
42 # bunzip2 isn't available to run on Windows.
43 def decompress ( self
, data
):
44 return bz2
. decompress ( data
)
46 class BZ2FileTest ( BaseTest
):
47 "Test BZ2File type miscellaneous methods."
50 self
. filename
= TESTFN
53 if os
. path
. isfile ( self
. filename
):
54 os
. unlink ( self
. filename
)
56 def createTempFile ( self
, crlf
= 0 ):
57 with
open ( self
. filename
, "wb" ) as f
:
65 # "Test BZ2File.read()"
67 with
BZ2File ( self
. filename
) as bz2f
:
68 self
. assertRaises ( TypeError , bz2f
. read
, None )
69 self
. assertEqual ( bz2f
. read (), self
. TEXT
)
72 # Test BBZ2File.read(0)"
74 with
BZ2File ( self
. filename
) as bz2f
:
75 self
. assertRaises ( TypeError , bz2f
. read
, None )
76 self
. assertEqual ( bz2f
. read ( 0 ), "" )
78 def testReadChunk10 ( self
):
79 # "Test BZ2File.read() in chunks of 10 bytes"
81 with
BZ2File ( self
. filename
) as bz2f
:
88 self
. assertEqual ( text
, self
. TEXT
)
90 def testRead100 ( self
):
91 # "Test BZ2File.read(100)"
93 with
BZ2File ( self
. filename
) as bz2f
:
94 self
. assertEqual ( bz2f
. read ( 100 ), self
. TEXT
[: 100 ])
96 def testReadLine ( self
):
97 # "Test BZ2File.readline()"
99 with
BZ2File ( self
. filename
) as bz2f
:
100 self
. assertRaises ( TypeError , bz2f
. readline
, None )
101 sio
= StringIO ( self
. TEXT
)
102 for line
in sio
. readlines ():
103 self
. assertEqual ( bz2f
. readline (), line
)
105 def testReadLines ( self
):
106 # "Test BZ2File.readlines()"
107 self
. createTempFile ()
108 with
BZ2File ( self
. filename
) as bz2f
:
109 self
. assertRaises ( TypeError , bz2f
. readlines
, None )
110 sio
= StringIO ( self
. TEXT
)
111 self
. assertEqual ( bz2f
. readlines (), sio
. readlines ())
113 def testIterator ( self
):
114 # "Test iter(BZ2File)"
115 self
. createTempFile ()
116 with
BZ2File ( self
. filename
) as bz2f
:
117 sio
= StringIO ( self
. TEXT
)
118 self
. assertEqual ( list ( iter ( bz2f
)), sio
. readlines ())
120 def testClosedIteratorDeadlock ( self
):
121 # "Test that iteration on a closed bz2file releases the lock."
122 # http://bugs.python.org/issue3309
123 self
. createTempFile ()
124 bz2f
= BZ2File ( self
. filename
)
126 self
. assertRaises ( ValueError , bz2f
. next
)
127 # This call will deadlock of the above .next call failed to
129 self
. assertRaises ( ValueError , bz2f
. readlines
)
131 def testXReadLines ( self
):
132 # "Test BZ2File.xreadlines()"
133 self
. createTempFile ()
134 bz2f
= BZ2File ( self
. filename
)
135 sio
= StringIO ( self
. TEXT
)
136 self
. assertEqual ( list ( bz2f
. xreadlines ()), sio
. readlines ())
139 def testUniversalNewlinesLF ( self
):
140 # "Test BZ2File.read() with universal newlines (\\n)"
141 self
. createTempFile ()
142 bz2f
= BZ2File ( self
. filename
, "rU" )
143 self
. assertEqual ( bz2f
. read (), self
. TEXT
)
144 self
. assertEqual ( bz2f
. newlines
, " \n " )
147 def testUniversalNewlinesCRLF ( self
):
148 # "Test BZ2File.read() with universal newlines (\\r\\n)"
149 self
. createTempFile ( crlf
= 1 )
150 bz2f
= BZ2File ( self
. filename
, "rU" )
151 self
. assertEqual ( bz2f
. read (), self
. TEXT
)
152 self
. assertEqual ( bz2f
. newlines
, " \r\n " )
156 # "Test BZ2File.write()"
157 with
BZ2File ( self
. filename
, "w" ) as bz2f
:
158 self
. assertRaises ( TypeError , bz2f
. write
)
159 bz2f
. write ( self
. TEXT
)
160 with
open ( self
. filename
, 'rb' ) as f
:
161 self
. assertEqual ( self
. decompress ( f
. read ()), self
. TEXT
)
163 def testWriteChunks10 ( self
):
164 # "Test BZ2File.write() with chunks of 10 bytes"
165 with
BZ2File ( self
. filename
, "w" ) as bz2f
:
168 str = self
. TEXT
[ n
* 10 :( n
+ 1 )* 10 ]
173 with
open ( self
. filename
, 'rb' ) as f
:
174 self
. assertEqual ( self
. decompress ( f
. read ()), self
. TEXT
)
176 def testWriteLines ( self
):
177 # "Test BZ2File.writelines()"
178 with
BZ2File ( self
. filename
, "w" ) as bz2f
:
179 self
. assertRaises ( TypeError , bz2f
. writelines
)
180 sio
= StringIO ( self
. TEXT
)
181 bz2f
. writelines ( sio
. readlines ())
183 self
. assertRaises ( ValueError , bz2f
. writelines
, [ "a" ])
184 with
open ( self
. filename
, 'rb' ) as f
:
185 self
. assertEqual ( self
. decompress ( f
. read ()), self
. TEXT
)
187 def testWriteMethodsOnReadOnlyFile ( self
):
188 with
BZ2File ( self
. filename
, "w" ) as bz2f
:
191 with
BZ2File ( self
. filename
, "r" ) as bz2f
:
192 self
. assertRaises ( IOError , bz2f
. write
, "a" )
193 self
. assertRaises ( IOError , bz2f
. writelines
, [ "a" ])
195 def testSeekForward ( self
):
196 # "Test BZ2File.seek(150, 0)"
197 self
. createTempFile ()
198 with
BZ2File ( self
. filename
) as bz2f
:
199 self
. assertRaises ( TypeError , bz2f
. seek
)
201 self
. assertEqual ( bz2f
. read (), self
. TEXT
[ 150 :])
203 def testSeekBackwards ( self
):
204 # "Test BZ2File.seek(-150, 1)"
205 self
. createTempFile ()
206 with
BZ2File ( self
. filename
) as bz2f
:
209 self
. assertEqual ( bz2f
. read (), self
. TEXT
[ 500 - 150 :])
211 def testSeekBackwardsFromEnd ( self
):
212 # "Test BZ2File.seek(-150, 2)"
213 self
. createTempFile ()
214 with
BZ2File ( self
. filename
) as bz2f
:
216 self
. assertEqual ( bz2f
. read (), self
. TEXT
[ len ( self
. TEXT
)- 150 :])
218 def testSeekPostEnd ( self
):
219 # "Test BZ2File.seek(150000)"
220 self
. createTempFile ()
221 with
BZ2File ( self
. filename
) as bz2f
:
223 self
. assertEqual ( bz2f
. tell (), len ( self
. TEXT
))
224 self
. assertEqual ( bz2f
. read (), "" )
226 def testSeekPostEndTwice ( self
):
227 # "Test BZ2File.seek(150000) twice"
228 self
. createTempFile ()
229 with
BZ2File ( self
. filename
) as bz2f
:
232 self
. assertEqual ( bz2f
. tell (), len ( self
. TEXT
))
233 self
. assertEqual ( bz2f
. read (), "" )
235 def testSeekPreStart ( self
):
236 # "Test BZ2File.seek(-150, 0)"
237 self
. createTempFile ()
238 with
BZ2File ( self
. filename
) as bz2f
:
240 self
. assertEqual ( bz2f
. tell (), 0 )
241 self
. assertEqual ( bz2f
. read (), self
. TEXT
)
243 def testOpenDel ( self
):
244 # "Test opening and deleting a file many times"
245 self
. createTempFile ()
246 for i
in xrange ( 10000 ):
247 o
= BZ2File ( self
. filename
)
250 def testOpenNonexistent ( self
):
251 # "Test opening a nonexistent file"
252 self
. assertRaises ( IOError , BZ2File
, "/non/existent" )
255 # Bug #1194181: bz2.BZ2File opened for write with mode "U"
256 self
. createTempFile ()
257 bz2f
= BZ2File ( self
. filename
, "U" )
259 f
= file ( self
. filename
)
261 self
. assertEqual ( f
. tell (), len ( self
. DATA
))
264 def testBug1191043 ( self
):
265 # readlines() for files containing no newline
266 data
= 'BZh91AY&SY \xd9 b \x89 ] \x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00 ! \x9a h3M \x13 <] \xc9\x14\xe1 BCe \x8a %t'
267 with
open ( self
. filename
, "wb" ) as f
:
269 with
BZ2File ( self
. filename
) as bz2f
:
270 lines
= bz2f
. readlines ()
271 self
. assertEqual ( lines
, [ 'Test' ])
272 with
BZ2File ( self
. filename
) as bz2f
:
273 xlines
= list ( bz2f
. readlines ())
274 self
. assertEqual ( xlines
, [ 'Test' ])
276 def testContextProtocol ( self
):
277 # BZ2File supports the context management protocol
279 with
BZ2File ( self
. filename
, "wb" ) as f
:
281 f
= BZ2File ( self
. filename
, "rb" )
289 self
. fail ( "__enter__ on a closed file didn't raise an exception" )
291 with
BZ2File ( self
. filename
, "wb" ) as f
:
293 except ZeroDivisionError :
296 self
. fail ( "1 // 0 didn't raise an exception" )
298 @unittest . skipUnless ( threading
, 'Threading required for this test.' )
299 def testThreading ( self
):
300 # Using a BZ2File from several threads doesn't deadlock (issue #7205).
303 with bz2
. BZ2File ( self
. filename
, 'wb' ) as f
:
307 threads
= [ threading
. Thread ( target
= comp
) for i
in range ( nthreads
)]
313 def testMixedIterationReads ( self
):
314 # Issue #8397: mixed iteration and reads should be forbidden.
315 with bz2
. BZ2File ( self
. filename
, 'wb' ) as f
:
316 # The internal buffer size is hard-wired to 8192 bytes, we must
317 # write out more than that for the test to stop half through
319 f
. write ( self
. TEXT
* 100 )
320 with bz2
. BZ2File ( self
. filename
, 'rb' ) as f
:
322 self
. assertRaises ( ValueError , f
. read
)
323 self
. assertRaises ( ValueError , f
. readline
)
324 self
. assertRaises ( ValueError , f
. readlines
)
326 class BZ2CompressorTest ( BaseTest
):
327 def testCompress ( self
):
328 # "Test BZ2Compressor.compress()/flush()"
329 bz2c
= BZ2Compressor ()
330 self
. assertRaises ( TypeError , bz2c
. compress
)
331 data
= bz2c
. compress ( self
. TEXT
)
333 self
. assertEqual ( self
. decompress ( data
), self
. TEXT
)
335 def testCompressChunks10 ( self
):
336 # "Test BZ2Compressor.compress()/flush() with chunks of 10 bytes"
337 bz2c
= BZ2Compressor ()
341 str = self
. TEXT
[ n
* 10 :( n
+ 1 )* 10 ]
344 data
+= bz2c
. compress ( str )
347 self
. assertEqual ( self
. decompress ( data
), self
. TEXT
)
349 class BZ2DecompressorTest ( BaseTest
):
350 def test_Constructor ( self
):
351 self
. assertRaises ( TypeError , BZ2Decompressor
, 42 )
353 def testDecompress ( self
):
354 # "Test BZ2Decompressor.decompress()"
355 bz2d
= BZ2Decompressor ()
356 self
. assertRaises ( TypeError , bz2d
. decompress
)
357 text
= bz2d
. decompress ( self
. DATA
)
358 self
. assertEqual ( text
, self
. TEXT
)
360 def testDecompressChunks10 ( self
):
361 # "Test BZ2Decompressor.decompress() with chunks of 10 bytes"
362 bz2d
= BZ2Decompressor ()
366 str = self
. DATA
[ n
* 10 :( n
+ 1 )* 10 ]
369 text
+= bz2d
. decompress ( str )
371 self
. assertEqual ( text
, self
. TEXT
)
373 def testDecompressUnusedData ( self
):
374 # "Test BZ2Decompressor.decompress() with unused data"
375 bz2d
= BZ2Decompressor ()
376 unused_data
= "this is unused data"
377 text
= bz2d
. decompress ( self
. DATA
+ unused_data
)
378 self
. assertEqual ( text
, self
. TEXT
)
379 self
. assertEqual ( bz2d
. unused_data
, unused_data
)
381 def testEOFError ( self
):
382 # "Calling BZ2Decompressor.decompress() after EOS must raise EOFError"
383 bz2d
= BZ2Decompressor ()
384 text
= bz2d
. decompress ( self
. DATA
)
385 self
. assertRaises ( EOFError , bz2d
. decompress
, "anything" )
388 class FuncTest ( BaseTest
):
389 "Test module functions"
391 def testCompress ( self
):
392 # "Test compress() function"
393 data
= bz2
. compress ( self
. TEXT
)
394 self
. assertEqual ( self
. decompress ( data
), self
. TEXT
)
396 def testDecompress ( self
):
397 # "Test decompress() function"
398 text
= bz2
. decompress ( self
. DATA
)
399 self
. assertEqual ( text
, self
. TEXT
)
401 def testDecompressEmpty ( self
):
402 # "Test decompress() function with empty string"
403 text
= bz2
. decompress ( "" )
404 self
. assertEqual ( text
, "" )
406 def testDecompressIncomplete ( self
):
407 # "Test decompress() function with incomplete data"
408 self
. assertRaises ( ValueError , bz2
. decompress
, self
. DATA
[:- 10 ])
411 test_support
. run_unittest (
417 test_support
. reap_children ()
419 if __name__
== '__main__' :