Skip to content

Refactor Capture classes: move/rename CaptureIO classes #6765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2020
Merged
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
24 changes: 21 additions & 3 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
from tempfile import TemporaryFile
from typing import Generator
from typing import Optional
from typing import TextIO

import pytest
from _pytest.compat import CaptureAndPassthroughIO
from _pytest.compat import CaptureIO
from _pytest.compat import TYPE_CHECKING
from _pytest.config import Config
from _pytest.fixtures import FixtureRequest
Expand Down Expand Up @@ -320,6 +319,25 @@ def capfdbinary(request):
yield fixture


class CaptureIO(io.TextIOWrapper):
def __init__(self) -> None:
super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True)

def getvalue(self) -> str:
assert isinstance(self.buffer, io.BytesIO)
return self.buffer.getvalue().decode("UTF-8")


class TeeCaptureIO(CaptureIO):
def __init__(self, other: TextIO) -> None:
self._other = other
super().__init__()

def write(self, s: str) -> int:
super().write(s)
return self._other.write(s)


class CaptureFixture:
"""
Object returned by :py:func:`capsys`, :py:func:`capsysbinary`, :py:func:`capfd` and :py:func:`capfdbinary`
Expand Down Expand Up @@ -673,7 +691,7 @@ def __init__(self, fd, tmpfile=None):
if name == "stdin":
tmpfile = DontReadFromInput()
else:
tmpfile = CaptureAndPassthroughIO(self._old)
tmpfile = TeeCaptureIO(self._old)
self.tmpfile = tmpfile


Expand Down
21 changes: 0 additions & 21 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import functools
import inspect
import io
import os
import re
import sys
Expand All @@ -13,7 +12,6 @@
from typing import Any
from typing import Callable
from typing import Generic
from typing import IO
from typing import Optional
from typing import overload
from typing import Tuple
Expand Down Expand Up @@ -343,25 +341,6 @@ def safe_isclass(obj: object) -> bool:
return False


class CaptureIO(io.TextIOWrapper):
def __init__(self) -> None:
super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True)

def getvalue(self) -> str:
assert isinstance(self.buffer, io.BytesIO)
return self.buffer.getvalue().decode("UTF-8")


class CaptureAndPassthroughIO(CaptureIO):
def __init__(self, other: IO) -> None:
self._other = other
super().__init__()

def write(self, s) -> int:
super().write(s)
return self._other.write(s)


if sys.version_info < (3, 5, 2):

def overload(f): # noqa: F811
Expand Down
6 changes: 3 additions & 3 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,10 @@ def test_write_bytes_to_buffer(self):
assert f.getvalue() == "foo\r\n"


class TestCaptureAndPassthroughIO(TestCaptureIO):
class TestTeeCaptureIO(TestCaptureIO):
def test_text(self):
sio = io.StringIO()
f = capture.CaptureAndPassthroughIO(sio)
f = capture.TeeCaptureIO(sio)
f.write("hello")
s1 = f.getvalue()
assert s1 == "hello"
Expand All @@ -818,7 +818,7 @@ def test_text(self):

def test_unicode_and_str_mixture(self):
sio = io.StringIO()
f = capture.CaptureAndPassthroughIO(sio)
f = capture.TeeCaptureIO(sio)
f.write("\u00f6")
pytest.raises(TypeError, f.write, b"hello")

Expand Down