Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ def name(self):
"""Ensure that file.name is a string."""
return repr(self.buffer)

@property
def mode(self):
"""
Present a text-like mode to callers.

The underlying buffer is opened in binary mode (e.g., 'rb+', 'wb+')
during fd-level capturing. Some libraries (e.g., youtube-dl) inspect
file.mode to decide whether to write bytes or text. Since EncodedFile
expects text on Python 3, report a mode without the binary flag.
"""
m = getattr(self.buffer, "mode", "")
return m.replace("b", "")


def __getattr__(self, name):
return getattr(object.__getattribute__(self, "buffer"), name)

Expand Down
8 changes: 8 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,14 @@ class TestStdCaptureFD(TestStdCapture):
pytestmark = needsosdup
captureclass = staticmethod(StdCaptureFD)

def test_stdout_mode(self):
with self.getcapture():
assert hasattr(sys.stdout, "buffer")
# underlying buffer is binary
assert "b" in sys.stdout.buffer.mode
# EncodedFile.mode should not include "b"
assert "b" not in sys.stdout.mode

def test_simple_only_fd(self, testdir):
testdir.makepyfile(
"""
Expand Down