23
23
)
24
24
25
25
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
-
44
26
def StdCaptureFD (out = True , err = True , in_ = True ):
45
27
return capture .MultiCapture (out , err , in_ , Capture = capture .FDCapture )
46
28
@@ -832,10 +814,11 @@ def test_write_bytes_to_buffer(self):
832
814
833
815
def test_bytes_io ():
834
816
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" )
837
820
s = f .getvalue ()
838
- assert s == tobytes ( "hello" )
821
+ assert s == b "hello"
839
822
840
823
841
824
def test_dontreadfrominput ():
@@ -948,7 +931,7 @@ class TestFDCapture(object):
948
931
def test_simple (self , tmpfile ):
949
932
fd = tmpfile .fileno ()
950
933
cap = capture .FDCapture (fd )
951
- data = tobytes ( "hello" )
934
+ data = b "hello"
952
935
os .write (fd , data )
953
936
s = cap .snap ()
954
937
cap .done ()
@@ -988,18 +971,18 @@ def test_stdin(self, tmpfile):
988
971
cap .start ()
989
972
x = os .read (0 , 100 ).strip ()
990
973
cap .done ()
991
- assert x == tobytes ( "" )
974
+ assert x == b""
992
975
993
976
def test_writeorg (self , tmpfile ):
994
- data1 , data2 = tobytes ( "foo" ), tobytes ( "bar" )
977
+ data1 , data2 = b "foo", b "bar"
995
978
cap = capture .FDCapture (tmpfile .fileno ())
996
979
cap .start ()
997
980
tmpfile .write (data1 )
998
981
tmpfile .flush ()
999
982
cap .writeorg (data2 )
1000
983
scap = cap .snap ()
1001
984
cap .done ()
1002
- assert scap == totext ( data1 )
985
+ assert scap == data1 . decode ( "ascii" )
1003
986
with open (tmpfile .name , "rb" ) as stmp_file :
1004
987
stmp = stmp_file .read ()
1005
988
assert stmp == data2
@@ -1008,17 +991,17 @@ def test_simple_resume_suspend(self, tmpfile):
1008
991
with saved_fd (1 ):
1009
992
cap = capture .FDCapture (1 )
1010
993
cap .start ()
1011
- data = tobytes ( "hello" )
994
+ data = b "hello"
1012
995
os .write (1 , data )
1013
996
sys .stdout .write ("whatever" )
1014
997
s = cap .snap ()
1015
998
assert s == "hellowhatever"
1016
999
cap .suspend ()
1017
- os .write (1 , tobytes ( "world" ) )
1000
+ os .write (1 , b "world" )
1018
1001
sys .stdout .write ("qlwkej" )
1019
1002
assert not cap .snap ()
1020
1003
cap .resume ()
1021
- os .write (1 , tobytes ( "but now" ) )
1004
+ os .write (1 , b "but now" )
1022
1005
sys .stdout .write (" yes\n " )
1023
1006
s = cap .snap ()
1024
1007
assert s == "but now yes\n "
@@ -1189,14 +1172,14 @@ def test_x():
1189
1172
1190
1173
def test_intermingling (self ):
1191
1174
with self .getcapture () as cap :
1192
- oswritebytes (1 , "1" )
1175
+ os . write (1 , b "1" )
1193
1176
sys .stdout .write (str (2 ))
1194
1177
sys .stdout .flush ()
1195
- oswritebytes (1 , "3" )
1196
- oswritebytes (2 , "a" )
1178
+ os . write (1 , b "3" )
1179
+ os . write (2 , b "a" )
1197
1180
sys .stderr .write ("b" )
1198
1181
sys .stderr .flush ()
1199
- oswritebytes (2 , "c" )
1182
+ os . write (2 , b "c" )
1200
1183
out , err = cap .readouterr ()
1201
1184
assert out == "123"
1202
1185
assert err == "abc"
0 commit comments