11#!/usr/bin/env python3
22from test import support
3- from test .support import TESTFN , bigmemtest , _4G
3+ from test .support import bigmemtest , _4G
44
55import unittest
66from io import BytesIO
1818bz2 = support .import_module ('bz2' )
1919from bz2 import BZ2File , BZ2Compressor , BZ2Decompressor
2020
21- has_cmdline_bunzip2 = (sys .platform != "win32" )
2221
2322class BaseTest (unittest .TestCase ):
2423 "Base for other testcases."
24+
2525 TEXT_LINES = [
2626 b'root:x:0:0:root:/root:/bin/bash\n ' ,
2727 b'bin:x:1:1:bin:/bin:\n ' ,
@@ -49,13 +49,17 @@ class BaseTest(unittest.TestCase):
4949 DATA = b'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`'
5050
5151 def setUp (self ):
52- self .filename = TESTFN
52+ self .filename = support . TESTFN
5353
5454 def tearDown (self ):
5555 if os .path .isfile (self .filename ):
5656 os .unlink (self .filename )
5757
58- if has_cmdline_bunzip2 :
58+ if sys .platform == "win32" :
59+ # bunzip2 isn't available to run on Windows.
60+ def decompress (self , data ):
61+ return bz2 .decompress (data )
62+ else :
5963 def decompress (self , data ):
6064 pop = subprocess .Popen ("bunzip2" , shell = True ,
6165 stdin = subprocess .PIPE ,
@@ -69,31 +73,21 @@ def decompress(self, data):
6973 ret = bz2 .decompress (data )
7074 return ret
7175
72- else :
73- # bunzip2 isn't available to run on Windows.
74- def decompress (self , data ):
75- return bz2 .decompress (data )
7676
7777class BZ2FileTest (BaseTest ):
78- "Test BZ2File type miscellaneous methods ."
78+ "Test the BZ2File class ."
7979
8080 def createTempFile (self , streams = 1 ):
8181 with open (self .filename , "wb" ) as f :
8282 f .write (self .DATA * streams )
8383
8484 def testBadArgs (self ):
85- with self .assertRaises (TypeError ):
86- BZ2File (123.456 )
87- with self .assertRaises (ValueError ):
88- BZ2File ("/dev/null" , "z" )
89- with self .assertRaises (ValueError ):
90- BZ2File ("/dev/null" , "rx" )
91- with self .assertRaises (ValueError ):
92- BZ2File ("/dev/null" , "rbt" )
93- with self .assertRaises (ValueError ):
94- BZ2File ("/dev/null" , compresslevel = 0 )
95- with self .assertRaises (ValueError ):
96- BZ2File ("/dev/null" , compresslevel = 10 )
85+ self .assertRaises (TypeError , BZ2File , 123.456 )
86+ self .assertRaises (ValueError , BZ2File , "/dev/null" , "z" )
87+ self .assertRaises (ValueError , BZ2File , "/dev/null" , "rx" )
88+ self .assertRaises (ValueError , BZ2File , "/dev/null" , "rbt" )
89+ self .assertRaises (ValueError , BZ2File , "/dev/null" , compresslevel = 0 )
90+ self .assertRaises (ValueError , BZ2File , "/dev/null" , compresslevel = 10 )
9791
9892 def testRead (self ):
9993 self .createTempFile ()
@@ -214,9 +208,8 @@ def testClosedIteratorDeadlock(self):
214208 self .createTempFile ()
215209 bz2f = BZ2File (self .filename )
216210 bz2f .close ()
217- self .assertRaises (ValueError , bz2f .__next__ )
218- # This call will deadlock if the above .__next__ call failed to
219- # release the lock.
211+ self .assertRaises (ValueError , next , bz2f )
212+ # This call will deadlock if the above call failed to release the lock.
220213 self .assertRaises (ValueError , bz2f .readlines )
221214
222215 def testWrite (self ):
@@ -379,7 +372,7 @@ def testSeekable(self):
379372 bz2f .close ()
380373 self .assertRaises (ValueError , bz2f .seekable )
381374
382- bz2f = BZ2File (BytesIO (), mode = "w" )
375+ bz2f = BZ2File (BytesIO (), "w" )
383376 try :
384377 self .assertFalse (bz2f .seekable ())
385378 finally :
@@ -405,7 +398,7 @@ def testReadable(self):
405398 bz2f .close ()
406399 self .assertRaises (ValueError , bz2f .readable )
407400
408- bz2f = BZ2File (BytesIO (), mode = "w" )
401+ bz2f = BZ2File (BytesIO (), "w" )
409402 try :
410403 self .assertFalse (bz2f .readable ())
411404 finally :
@@ -422,7 +415,7 @@ def testWritable(self):
422415 bz2f .close ()
423416 self .assertRaises (ValueError , bz2f .writable )
424417
425- bz2f = BZ2File (BytesIO (), mode = "w" )
418+ bz2f = BZ2File (BytesIO (), "w" )
426419 try :
427420 self .assertTrue (bz2f .writable ())
428421 finally :
@@ -476,7 +469,7 @@ def testThreading(self):
476469 # Issue #7205: Using a BZ2File from several threads shouldn't deadlock.
477470 data = b"1" * 2 ** 20
478471 nthreads = 10
479- with bz2 . BZ2File (self .filename , 'wb' ) as f :
472+ with BZ2File (self .filename , 'wb' ) as f :
480473 def comp ():
481474 for i in range (5 ):
482475 f .write (data )
@@ -487,28 +480,27 @@ def comp():
487480 t .join ()
488481
489482 def testWithoutThreading (self ):
490- bz2 = support .import_fresh_module ("bz2" , blocked = ("threading" ,))
491- with bz2 .BZ2File (self .filename , "wb" ) as f :
483+ module = support .import_fresh_module ("bz2" , blocked = ("threading" ,))
484+ with module .BZ2File (self .filename , "wb" ) as f :
492485 f .write (b"abc" )
493- with bz2 .BZ2File (self .filename , "rb" ) as f :
486+ with module .BZ2File (self .filename , "rb" ) as f :
494487 self .assertEqual (f .read (), b"abc" )
495488
496489 def testMixedIterationAndReads (self ):
497490 self .createTempFile ()
498491 linelen = len (self .TEXT_LINES [0 ])
499492 halflen = linelen // 2
500- with bz2 . BZ2File (self .filename ) as bz2f :
493+ with BZ2File (self .filename ) as bz2f :
501494 bz2f .read (halflen )
502495 self .assertEqual (next (bz2f ), self .TEXT_LINES [0 ][halflen :])
503496 self .assertEqual (bz2f .read (), self .TEXT [linelen :])
504- with bz2 . BZ2File (self .filename ) as bz2f :
497+ with BZ2File (self .filename ) as bz2f :
505498 bz2f .readline ()
506499 self .assertEqual (next (bz2f ), self .TEXT_LINES [1 ])
507500 self .assertEqual (bz2f .readline (), self .TEXT_LINES [2 ])
508- with bz2 . BZ2File (self .filename ) as bz2f :
501+ with BZ2File (self .filename ) as bz2f :
509502 bz2f .readlines ()
510- with self .assertRaises (StopIteration ):
511- next (bz2f )
503+ self .assertRaises (StopIteration , next , bz2f )
512504 self .assertEqual (bz2f .readlines (), [])
513505
514506 def testMultiStreamOrdering (self ):
@@ -576,6 +568,7 @@ def testSeekBackwardsBytesIO(self):
576568 bz2f .seek (- 150 , 1 )
577569 self .assertEqual (bz2f .read (), self .TEXT [500 - 150 :])
578570
571+
579572class BZ2CompressorTest (BaseTest ):
580573 def testCompress (self ):
581574 bz2c = BZ2Compressor ()
@@ -687,97 +680,102 @@ def testDecompressMultiStream(self):
687680
688681
689682class OpenTest (BaseTest ):
683+ "Test the open function."
684+
685+ def open (self , * args , ** kwargs ):
686+ return bz2 .open (* args , ** kwargs )
687+
690688 def test_binary_modes (self ):
691- with bz2 .open (self .filename , "wb" ) as f :
689+ with self .open (self .filename , "wb" ) as f :
692690 f .write (self .TEXT )
693691 with open (self .filename , "rb" ) as f :
694- file_data = bz2 .decompress (f .read ())
692+ file_data = self .decompress (f .read ())
695693 self .assertEqual (file_data , self .TEXT )
696- with bz2 .open (self .filename , "rb" ) as f :
694+ with self .open (self .filename , "rb" ) as f :
697695 self .assertEqual (f .read (), self .TEXT )
698- with bz2 .open (self .filename , "ab" ) as f :
696+ with self .open (self .filename , "ab" ) as f :
699697 f .write (self .TEXT )
700698 with open (self .filename , "rb" ) as f :
701- file_data = bz2 .decompress (f .read ())
699+ file_data = self .decompress (f .read ())
702700 self .assertEqual (file_data , self .TEXT * 2 )
703701
704702 def test_implicit_binary_modes (self ):
705703 # Test implicit binary modes (no "b" or "t" in mode string).
706- with bz2 .open (self .filename , "w" ) as f :
704+ with self .open (self .filename , "w" ) as f :
707705 f .write (self .TEXT )
708706 with open (self .filename , "rb" ) as f :
709- file_data = bz2 .decompress (f .read ())
707+ file_data = self .decompress (f .read ())
710708 self .assertEqual (file_data , self .TEXT )
711- with bz2 .open (self .filename , "r" ) as f :
709+ with self .open (self .filename , "r" ) as f :
712710 self .assertEqual (f .read (), self .TEXT )
713- with bz2 .open (self .filename , "a" ) as f :
711+ with self .open (self .filename , "a" ) as f :
714712 f .write (self .TEXT )
715713 with open (self .filename , "rb" ) as f :
716- file_data = bz2 .decompress (f .read ())
714+ file_data = self .decompress (f .read ())
717715 self .assertEqual (file_data , self .TEXT * 2 )
718716
719717 def test_text_modes (self ):
720718 text = self .TEXT .decode ("ascii" )
721719 text_native_eol = text .replace ("\n " , os .linesep )
722- with bz2 .open (self .filename , "wt" ) as f :
720+ with self .open (self .filename , "wt" ) as f :
723721 f .write (text )
724722 with open (self .filename , "rb" ) as f :
725- file_data = bz2 .decompress (f .read ()).decode ("ascii" )
723+ file_data = self .decompress (f .read ()).decode ("ascii" )
726724 self .assertEqual (file_data , text_native_eol )
727- with bz2 .open (self .filename , "rt" ) as f :
725+ with self .open (self .filename , "rt" ) as f :
728726 self .assertEqual (f .read (), text )
729- with bz2 .open (self .filename , "at" ) as f :
727+ with self .open (self .filename , "at" ) as f :
730728 f .write (text )
731729 with open (self .filename , "rb" ) as f :
732- file_data = bz2 .decompress (f .read ()).decode ("ascii" )
730+ file_data = self .decompress (f .read ()).decode ("ascii" )
733731 self .assertEqual (file_data , text_native_eol * 2 )
734732
735733 def test_fileobj (self ):
736- with bz2 .open (BytesIO (self .DATA ), "r" ) as f :
734+ with self .open (BytesIO (self .DATA ), "r" ) as f :
737735 self .assertEqual (f .read (), self .TEXT )
738- with bz2 .open (BytesIO (self .DATA ), "rb" ) as f :
736+ with self .open (BytesIO (self .DATA ), "rb" ) as f :
739737 self .assertEqual (f .read (), self .TEXT )
740738 text = self .TEXT .decode ("ascii" )
741- with bz2 .open (BytesIO (self .DATA ), "rt" ) as f :
739+ with self .open (BytesIO (self .DATA ), "rt" ) as f :
742740 self .assertEqual (f .read (), text )
743741
744742 def test_bad_params (self ):
745743 # Test invalid parameter combinations.
746- with self .assertRaises (ValueError ):
747- bz2 .open ( self .filename , "wbt" )
748- with self .assertRaises (ValueError ):
749- bz2 .open ( self .filename , "rb" , encoding = "utf-8" )
750- with self .assertRaises (ValueError ):
751- bz2 .open ( self .filename , "rb" , errors = "ignore" )
752- with self .assertRaises (ValueError ):
753- bz2 .open ( self .filename , "rb" , newline = "\n " )
744+ self .assertRaises (ValueError ,
745+ self .open , self .filename , "wbt" )
746+ self .assertRaises (ValueError ,
747+ self .open , self .filename , "rb" , encoding = "utf-8" )
748+ self .assertRaises (ValueError ,
749+ self .open , self .filename , "rb" , errors = "ignore" )
750+ self .assertRaises (ValueError ,
751+ self .open , self .filename , "rb" , newline = "\n " )
754752
755753 def test_encoding (self ):
756754 # Test non-default encoding.
757755 text = self .TEXT .decode ("ascii" )
758756 text_native_eol = text .replace ("\n " , os .linesep )
759- with bz2 .open (self .filename , "wt" , encoding = "utf-16-le" ) as f :
757+ with self .open (self .filename , "wt" , encoding = "utf-16-le" ) as f :
760758 f .write (text )
761759 with open (self .filename , "rb" ) as f :
762- file_data = bz2 .decompress (f .read ()).decode ("utf-16-le" )
760+ file_data = self .decompress (f .read ()).decode ("utf-16-le" )
763761 self .assertEqual (file_data , text_native_eol )
764- with bz2 .open (self .filename , "rt" , encoding = "utf-16-le" ) as f :
762+ with self .open (self .filename , "rt" , encoding = "utf-16-le" ) as f :
765763 self .assertEqual (f .read (), text )
766764
767765 def test_encoding_error_handler (self ):
768766 # Test with non-default encoding error handler.
769- with bz2 .open (self .filename , "wb" ) as f :
767+ with self .open (self .filename , "wb" ) as f :
770768 f .write (b"foo\xff bar" )
771- with bz2 .open (self .filename , "rt" , encoding = "ascii" , errors = "ignore" ) \
769+ with self .open (self .filename , "rt" , encoding = "ascii" , errors = "ignore" ) \
772770 as f :
773771 self .assertEqual (f .read (), "foobar" )
774772
775773 def test_newline (self ):
776774 # Test with explicit newline (universal newline mode disabled).
777775 text = self .TEXT .decode ("ascii" )
778- with bz2 .open (self .filename , "wt" , newline = "\n " ) as f :
776+ with self .open (self .filename , "wt" , newline = "\n " ) as f :
779777 f .write (text )
780- with bz2 .open (self .filename , "rt" , newline = "\r " ) as f :
778+ with self .open (self .filename , "rt" , newline = "\r " ) as f :
781779 self .assertEqual (f .readlines (), [text ])
782780
783781
0 commit comments