2323)
2424
2525
26- def tobytes (obj ):
27- if isinstance (obj , text_type ):
28- obj = obj .encode ("UTF-8" )
29- assert isinstance (obj , bytes )
30- return obj
31-
32-
33- def totext (obj ):
34- if isinstance (obj , bytes ):
35- obj = text_type (obj , "UTF-8" )
36- assert isinstance (obj , text_type )
37- return obj
38-
39-
40- def oswritebytes (fd , obj ):
41- os .write (fd , tobytes (obj ))
42-
43-
4426def StdCaptureFD (out = True , err = True , in_ = True ):
4527 return capture .MultiCapture (out , err , in_ , Capture = capture .FDCapture )
4628
@@ -832,10 +814,11 @@ def test_write_bytes_to_buffer(self):
832814
833815def test_bytes_io ():
834816 f = py .io .BytesIO ()
835- f .write (tobytes ("hello" ))
836- pytest .raises (TypeError , "f.write(totext('hello'))" )
817+ f .write (b"hello" )
818+ with pytest .raises (TypeError ):
819+ f .write (u"hello" )
837820 s = f .getvalue ()
838- assert s == tobytes ( "hello" )
821+ assert s == b "hello"
839822
840823
841824def test_dontreadfrominput ():
@@ -948,7 +931,7 @@ class TestFDCapture(object):
948931 def test_simple (self , tmpfile ):
949932 fd = tmpfile .fileno ()
950933 cap = capture .FDCapture (fd )
951- data = tobytes ( "hello" )
934+ data = b "hello"
952935 os .write (fd , data )
953936 s = cap .snap ()
954937 cap .done ()
@@ -988,18 +971,18 @@ def test_stdin(self, tmpfile):
988971 cap .start ()
989972 x = os .read (0 , 100 ).strip ()
990973 cap .done ()
991- assert x == tobytes ( "" )
974+ assert x == b""
992975
993976 def test_writeorg (self , tmpfile ):
994- data1 , data2 = tobytes ( "foo" ), tobytes ( "bar" )
977+ data1 , data2 = b "foo", b "bar"
995978 cap = capture .FDCapture (tmpfile .fileno ())
996979 cap .start ()
997980 tmpfile .write (data1 )
998981 tmpfile .flush ()
999982 cap .writeorg (data2 )
1000983 scap = cap .snap ()
1001984 cap .done ()
1002- assert scap == totext ( data1 )
985+ assert scap == data1 . decode ( "ascii" )
1003986 with open (tmpfile .name , "rb" ) as stmp_file :
1004987 stmp = stmp_file .read ()
1005988 assert stmp == data2
@@ -1008,17 +991,17 @@ def test_simple_resume_suspend(self, tmpfile):
1008991 with saved_fd (1 ):
1009992 cap = capture .FDCapture (1 )
1010993 cap .start ()
1011- data = tobytes ( "hello" )
994+ data = b "hello"
1012995 os .write (1 , data )
1013996 sys .stdout .write ("whatever" )
1014997 s = cap .snap ()
1015998 assert s == "hellowhatever"
1016999 cap .suspend ()
1017- os .write (1 , tobytes ( "world" ) )
1000+ os .write (1 , b "world" )
10181001 sys .stdout .write ("qlwkej" )
10191002 assert not cap .snap ()
10201003 cap .resume ()
1021- os .write (1 , tobytes ( "but now" ) )
1004+ os .write (1 , b "but now" )
10221005 sys .stdout .write (" yes\n " )
10231006 s = cap .snap ()
10241007 assert s == "but now yes\n "
@@ -1189,14 +1172,14 @@ def test_x():
11891172
11901173 def test_intermingling (self ):
11911174 with self .getcapture () as cap :
1192- oswritebytes (1 , "1" )
1175+ os . write (1 , b "1" )
11931176 sys .stdout .write (str (2 ))
11941177 sys .stdout .flush ()
1195- oswritebytes (1 , "3" )
1196- oswritebytes (2 , "a" )
1178+ os . write (1 , b "3" )
1179+ os . write (2 , b "a" )
11971180 sys .stderr .write ("b" )
11981181 sys .stderr .flush ()
1199- oswritebytes (2 , "c" )
1182+ os . write (2 , b "c" )
12001183 out , err = cap .readouterr ()
12011184 assert out == "123"
12021185 assert err == "abc"
0 commit comments