Skip to content

Commit 5646daa

Browse files
committed
SysCaptureBinary: decode in writeorg
Upstream: pytest-dev#6880 Ref (fixes): pytest-dev#6871 (cherry picked from commit d90ae38)
1 parent 7f601a9 commit 5646daa

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

changelog/6871.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash with captured output when using the :fixture:`capsysbinary fixture <capsysbinary>`.

src/_pytest/capture.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,8 @@ def resume(self):
682682
setattr(sys, self.name, self.tmpfile)
683683
self._state = "resumed"
684684

685-
def writeorg(self, data):
685+
def writeorg(self, data: str) -> None:
686+
data = data.decode(self._old.encoding)
686687
self._old.write(data)
687688
self._old.flush()
688689

@@ -696,6 +697,10 @@ def snap(self):
696697
self.tmpfile.truncate()
697698
return res
698699

700+
def writeorg(self, data: str) -> None:
701+
self._old.write(data)
702+
self._old.flush()
703+
699704

700705
class TeeSysCapture(SysCapture):
701706
def __init__(self, fd, tmpfile=None):

testing/test_capture.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -542,18 +542,33 @@ def test_hello(capfdbinary):
542542
reprec.assertoutcome(passed=1)
543543

544544
def test_capsysbinary(self, testdir):
545-
reprec = testdir.inline_runsource(
545+
p1 = testdir.makepyfile(
546546
"""\
547547
def test_hello(capsysbinary):
548548
import sys
549+
549550
# some likely un-decodable bytes
550551
sys.stdout.buffer.write(b'\\xfe\\x98\\x20')
552+
551553
out, err = capsysbinary.readouterr()
552554
assert out == b'\\xfe\\x98\\x20'
553555
assert err == b''
556+
557+
# handles writing strings
558+
print("hello")
559+
print("hello stderr", file=sys.stderr)
554560
"""
555561
)
556-
reprec.assertoutcome(passed=1)
562+
result = testdir.runpytest(str(p1), "-rA")
563+
result.stdout.fnmatch_lines(
564+
[
565+
"*- Captured stdout call -*",
566+
"hello",
567+
"*- Captured stderr call -*",
568+
"hello stderr",
569+
"*= 1 passed in *",
570+
]
571+
)
557572

558573
def test_partial_setup_failure(self, testdir):
559574
p = testdir.makepyfile(

0 commit comments

Comments
 (0)