@@ -48,6 +48,13 @@ class BaseTest(unittest.TestCase):
4848 TEXT = b'' .join (TEXT_LINES )
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
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+
5158 if has_cmdline_bunzip2 :
5259 def decompress (self , data ):
5360 pop = subprocess .Popen ("bunzip2" , shell = True ,
@@ -70,13 +77,6 @@ def decompress(self, data):
7077class BZ2FileTest (BaseTest ):
7178 "Test BZ2File type miscellaneous methods."
7279
73- def setUp (self ):
74- self .filename = TESTFN
75-
76- def tearDown (self ):
77- if os .path .isfile (self .filename ):
78- os .unlink (self .filename )
79-
8080 def createTempFile (self , streams = 1 ):
8181 with open (self .filename , "wb" ) as f :
8282 f .write (self .DATA * streams )
@@ -650,9 +650,7 @@ def testDecompress4G(self, size):
650650 decompressed = None
651651
652652
653- class FuncTest (BaseTest ):
654- "Test module functions"
655-
653+ class CompressDecompressTest (BaseTest ):
656654 def testCompress (self ):
657655 data = bz2 .compress (self .TEXT )
658656 self .assertEqual (self .decompress (data ), self .TEXT )
@@ -672,12 +670,109 @@ def testDecompressMultiStream(self):
672670 text = bz2 .decompress (self .DATA * 5 )
673671 self .assertEqual (text , self .TEXT * 5 )
674672
673+
674+ class OpenTest (BaseTest ):
675+ def test_binary_modes (self ):
676+ with bz2 .open (self .filename , "wb" ) as f :
677+ f .write (self .TEXT )
678+ with open (self .filename , "rb" ) as f :
679+ file_data = bz2 .decompress (f .read ())
680+ self .assertEqual (file_data , self .TEXT )
681+ with bz2 .open (self .filename , "rb" ) as f :
682+ self .assertEqual (f .read (), self .TEXT )
683+ with bz2 .open (self .filename , "ab" ) as f :
684+ f .write (self .TEXT )
685+ with open (self .filename , "rb" ) as f :
686+ file_data = bz2 .decompress (f .read ())
687+ self .assertEqual (file_data , self .TEXT * 2 )
688+
689+ def test_implicit_binary_modes (self ):
690+ # Test implicit binary modes (no "b" or "t" in mode string).
691+ with bz2 .open (self .filename , "w" ) as f :
692+ f .write (self .TEXT )
693+ with open (self .filename , "rb" ) as f :
694+ file_data = bz2 .decompress (f .read ())
695+ self .assertEqual (file_data , self .TEXT )
696+ with bz2 .open (self .filename , "r" ) as f :
697+ self .assertEqual (f .read (), self .TEXT )
698+ with bz2 .open (self .filename , "a" ) as f :
699+ f .write (self .TEXT )
700+ with open (self .filename , "rb" ) as f :
701+ file_data = bz2 .decompress (f .read ())
702+ self .assertEqual (file_data , self .TEXT * 2 )
703+
704+ def test_text_modes (self ):
705+ text = self .TEXT .decode ("ascii" )
706+ text_native_eol = text .replace ("\n " , os .linesep )
707+ with bz2 .open (self .filename , "wt" ) as f :
708+ f .write (text )
709+ with open (self .filename , "rb" ) as f :
710+ file_data = bz2 .decompress (f .read ()).decode ("ascii" )
711+ self .assertEqual (file_data , text_native_eol )
712+ with bz2 .open (self .filename , "rt" ) as f :
713+ self .assertEqual (f .read (), text )
714+ with bz2 .open (self .filename , "at" ) as f :
715+ f .write (text )
716+ with open (self .filename , "rb" ) as f :
717+ file_data = bz2 .decompress (f .read ()).decode ("ascii" )
718+ self .assertEqual (file_data , text_native_eol * 2 )
719+
720+ def test_fileobj (self ):
721+ with bz2 .open (BytesIO (self .DATA ), "r" ) as f :
722+ self .assertEqual (f .read (), self .TEXT )
723+ with bz2 .open (BytesIO (self .DATA ), "rb" ) as f :
724+ self .assertEqual (f .read (), self .TEXT )
725+ text = self .TEXT .decode ("ascii" )
726+ with bz2 .open (BytesIO (self .DATA ), "rt" ) as f :
727+ self .assertEqual (f .read (), text )
728+
729+ def test_bad_params (self ):
730+ # Test invalid parameter combinations.
731+ with self .assertRaises (ValueError ):
732+ bz2 .open (self .filename , "wbt" )
733+ with self .assertRaises (ValueError ):
734+ bz2 .open (self .filename , "rb" , encoding = "utf-8" )
735+ with self .assertRaises (ValueError ):
736+ bz2 .open (self .filename , "rb" , errors = "ignore" )
737+ with self .assertRaises (ValueError ):
738+ bz2 .open (self .filename , "rb" , newline = "\n " )
739+
740+ def test_encoding (self ):
741+ # Test non-default encoding.
742+ text = self .TEXT .decode ("ascii" )
743+ text_native_eol = text .replace ("\n " , os .linesep )
744+ with bz2 .open (self .filename , "wt" , encoding = "utf-16-le" ) as f :
745+ f .write (text )
746+ with open (self .filename , "rb" ) as f :
747+ file_data = bz2 .decompress (f .read ()).decode ("utf-16-le" )
748+ self .assertEqual (file_data , text_native_eol )
749+ with bz2 .open (self .filename , "rt" , encoding = "utf-16-le" ) as f :
750+ self .assertEqual (f .read (), text )
751+
752+ def test_encoding_error_handler (self ):
753+ # Test with non-default encoding error handler.
754+ with bz2 .open (self .filename , "wb" ) as f :
755+ f .write (b"foo\xff bar" )
756+ with bz2 .open (self .filename , "rt" , encoding = "ascii" , errors = "ignore" ) \
757+ as f :
758+ self .assertEqual (f .read (), "foobar" )
759+
760+ def test_newline (self ):
761+ # Test with explicit newline (universal newline mode disabled).
762+ text = self .TEXT .decode ("ascii" )
763+ with bz2 .open (self .filename , "wt" , newline = "\n " ) as f :
764+ f .write (text )
765+ with bz2 .open (self .filename , "rt" , newline = "\r " ) as f :
766+ self .assertEqual (f .readlines (), [text ])
767+
768+
675769def test_main ():
676770 support .run_unittest (
677771 BZ2FileTest ,
678772 BZ2CompressorTest ,
679773 BZ2DecompressorTest ,
680- FuncTest
774+ CompressDecompressTest ,
775+ OpenTest ,
681776 )
682777 support .reap_children ()
683778
0 commit comments