Skip to content

Commit

Permalink
SysCaptureBinary: decode in writeorg
Browse files Browse the repository at this point in the history
Upstream: pytest-dev#6880
Ref (fixes): pytest-dev#6871
  • Loading branch information
blueyed committed Mar 12, 2020
1 parent 7f601a9 commit f6bb7c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/6871.bugfix.rst
@@ -0,0 +1 @@
Fix crash with captured output when using the :fixture:`capsysbinary fixture <capsysbinary>`.
8 changes: 7 additions & 1 deletion src/_pytest/capture.py
Expand Up @@ -682,7 +682,9 @@ def resume(self):
setattr(sys, self.name, self.tmpfile)
self._state = "resumed"

def writeorg(self, data):
def writeorg(self, data: bytes) -> None:
assert isinstance(data, bytes)
data = data.decode(self._old.encoding)
self._old.write(data)
self._old.flush()

Expand All @@ -696,6 +698,10 @@ def snap(self):
self.tmpfile.truncate()
return res

def writeorg(self, data: str) -> None:
self._old.write(data)
self._old.flush()


class TeeSysCapture(SysCapture):
def __init__(self, fd, tmpfile=None):
Expand Down
19 changes: 17 additions & 2 deletions testing/test_capture.py
Expand Up @@ -542,18 +542,33 @@ def test_hello(capfdbinary):
reprec.assertoutcome(passed=1)

def test_capsysbinary(self, testdir):
reprec = testdir.inline_runsource(
p1 = testdir.makepyfile(
"""\
def test_hello(capsysbinary):
import sys
# some likely un-decodable bytes
sys.stdout.buffer.write(b'\\xfe\\x98\\x20')
out, err = capsysbinary.readouterr()
assert out == b'\\xfe\\x98\\x20'
assert err == b''
# handles writing strings
print("hello")
print("hello stderr", file=sys.stderr)
"""
)
reprec.assertoutcome(passed=1)
result = testdir.runpytest(str(p1), "-rA")
result.stdout.fnmatch_lines(
[
"*- Captured stdout call -*",
"hello",
"*- Captured stderr call -*",
"hello stderr",
"*= 1 passed in *",
]
)

def test_partial_setup_failure(self, testdir):
p = testdir.makepyfile(
Expand Down

0 comments on commit f6bb7c4

Please sign in to comment.