Skip to content

Commit 4bf6a07

Browse files
authored
Merge pull request #4305 from RonnyPfannschmidt/cleanup-tobytes
replace byte/unicode helpers in test_capture with python level syntax
2 parents 4aa3c4f + 7cb271b commit 4bf6a07

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

changelog/4305.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace byte/unicode helpers in test_capture with python level syntax.

testing/test_capture.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,6 @@
2727
)
2828

2929

30-
def tobytes(obj):
31-
if isinstance(obj, text_type):
32-
obj = obj.encode("UTF-8")
33-
assert isinstance(obj, bytes)
34-
return obj
35-
36-
37-
def totext(obj):
38-
if isinstance(obj, bytes):
39-
obj = text_type(obj, "UTF-8")
40-
assert isinstance(obj, text_type)
41-
return obj
42-
43-
44-
def oswritebytes(fd, obj):
45-
os.write(fd, tobytes(obj))
46-
47-
4830
def StdCaptureFD(out=True, err=True, in_=True):
4931
return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture)
5032

@@ -836,10 +818,11 @@ def test_write_bytes_to_buffer(self):
836818

837819
def test_bytes_io():
838820
f = py.io.BytesIO()
839-
f.write(tobytes("hello"))
840-
pytest.raises(TypeError, "f.write(totext('hello'))")
821+
f.write(b"hello")
822+
with pytest.raises(TypeError):
823+
f.write(u"hello")
841824
s = f.getvalue()
842-
assert s == tobytes("hello")
825+
assert s == b"hello"
843826

844827

845828
def test_dontreadfrominput():
@@ -952,7 +935,7 @@ class TestFDCapture(object):
952935
def test_simple(self, tmpfile):
953936
fd = tmpfile.fileno()
954937
cap = capture.FDCapture(fd)
955-
data = tobytes("hello")
938+
data = b"hello"
956939
os.write(fd, data)
957940
s = cap.snap()
958941
cap.done()
@@ -992,18 +975,18 @@ def test_stdin(self, tmpfile):
992975
cap.start()
993976
x = os.read(0, 100).strip()
994977
cap.done()
995-
assert x == tobytes("")
978+
assert x == b""
996979

997980
def test_writeorg(self, tmpfile):
998-
data1, data2 = tobytes("foo"), tobytes("bar")
981+
data1, data2 = b"foo", b"bar"
999982
cap = capture.FDCapture(tmpfile.fileno())
1000983
cap.start()
1001984
tmpfile.write(data1)
1002985
tmpfile.flush()
1003986
cap.writeorg(data2)
1004987
scap = cap.snap()
1005988
cap.done()
1006-
assert scap == totext(data1)
989+
assert scap == data1.decode("ascii")
1007990
with open(tmpfile.name, "rb") as stmp_file:
1008991
stmp = stmp_file.read()
1009992
assert stmp == data2
@@ -1012,17 +995,17 @@ def test_simple_resume_suspend(self, tmpfile):
1012995
with saved_fd(1):
1013996
cap = capture.FDCapture(1)
1014997
cap.start()
1015-
data = tobytes("hello")
998+
data = b"hello"
1016999
os.write(1, data)
10171000
sys.stdout.write("whatever")
10181001
s = cap.snap()
10191002
assert s == "hellowhatever"
10201003
cap.suspend()
1021-
os.write(1, tobytes("world"))
1004+
os.write(1, b"world")
10221005
sys.stdout.write("qlwkej")
10231006
assert not cap.snap()
10241007
cap.resume()
1025-
os.write(1, tobytes("but now"))
1008+
os.write(1, b"but now")
10261009
sys.stdout.write(" yes\n")
10271010
s = cap.snap()
10281011
assert s == "but now yes\n"
@@ -1193,14 +1176,14 @@ def test_x():
11931176

11941177
def test_intermingling(self):
11951178
with self.getcapture() as cap:
1196-
oswritebytes(1, "1")
1179+
os.write(1, b"1")
11971180
sys.stdout.write(str(2))
11981181
sys.stdout.flush()
1199-
oswritebytes(1, "3")
1200-
oswritebytes(2, "a")
1182+
os.write(1, b"3")
1183+
os.write(2, b"a")
12011184
sys.stderr.write("b")
12021185
sys.stderr.flush()
1203-
oswritebytes(2, "c")
1186+
os.write(2, b"c")
12041187
out, err = cap.readouterr()
12051188
assert out == "123"
12061189
assert err == "abc"

0 commit comments

Comments
 (0)