Skip to content

Commit b4be6cd

Browse files
authored
Merge pull request #6765 from blueyed/capture-refactor-1
Refactor Capture classes: move/rename CaptureIO classes
2 parents d7d627b + 7647d1c commit b4be6cd

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

src/_pytest/capture.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
from tempfile import TemporaryFile
1212
from typing import Generator
1313
from typing import Optional
14+
from typing import TextIO
1415

1516
import pytest
16-
from _pytest.compat import CaptureAndPassthroughIO
17-
from _pytest.compat import CaptureIO
1817
from _pytest.compat import TYPE_CHECKING
1918
from _pytest.config import Config
2019
from _pytest.fixtures import FixtureRequest
@@ -320,6 +319,25 @@ def capfdbinary(request):
320319
yield fixture
321320

322321

322+
class CaptureIO(io.TextIOWrapper):
323+
def __init__(self) -> None:
324+
super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True)
325+
326+
def getvalue(self) -> str:
327+
assert isinstance(self.buffer, io.BytesIO)
328+
return self.buffer.getvalue().decode("UTF-8")
329+
330+
331+
class TeeCaptureIO(CaptureIO):
332+
def __init__(self, other: TextIO) -> None:
333+
self._other = other
334+
super().__init__()
335+
336+
def write(self, s: str) -> int:
337+
super().write(s)
338+
return self._other.write(s)
339+
340+
323341
class CaptureFixture:
324342
"""
325343
Object returned by :py:func:`capsys`, :py:func:`capsysbinary`, :py:func:`capfd` and :py:func:`capfdbinary`
@@ -673,7 +691,7 @@ def __init__(self, fd, tmpfile=None):
673691
if name == "stdin":
674692
tmpfile = DontReadFromInput()
675693
else:
676-
tmpfile = CaptureAndPassthroughIO(self._old)
694+
tmpfile = TeeCaptureIO(self._old)
677695
self.tmpfile = tmpfile
678696

679697

src/_pytest/compat.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44
import functools
55
import inspect
6-
import io
76
import os
87
import re
98
import sys
@@ -13,7 +12,6 @@
1312
from typing import Any
1413
from typing import Callable
1514
from typing import Generic
16-
from typing import IO
1715
from typing import Optional
1816
from typing import overload
1917
from typing import Tuple
@@ -343,25 +341,6 @@ def safe_isclass(obj: object) -> bool:
343341
return False
344342

345343

346-
class CaptureIO(io.TextIOWrapper):
347-
def __init__(self) -> None:
348-
super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True)
349-
350-
def getvalue(self) -> str:
351-
assert isinstance(self.buffer, io.BytesIO)
352-
return self.buffer.getvalue().decode("UTF-8")
353-
354-
355-
class CaptureAndPassthroughIO(CaptureIO):
356-
def __init__(self, other: IO) -> None:
357-
self._other = other
358-
super().__init__()
359-
360-
def write(self, s) -> int:
361-
super().write(s)
362-
return self._other.write(s)
363-
364-
365344
if sys.version_info < (3, 5, 2):
366345

367346
def overload(f): # noqa: F811

testing/test_capture.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,10 @@ def test_write_bytes_to_buffer(self):
804804
assert f.getvalue() == "foo\r\n"
805805

806806

807-
class TestCaptureAndPassthroughIO(TestCaptureIO):
807+
class TestTeeCaptureIO(TestCaptureIO):
808808
def test_text(self):
809809
sio = io.StringIO()
810-
f = capture.CaptureAndPassthroughIO(sio)
810+
f = capture.TeeCaptureIO(sio)
811811
f.write("hello")
812812
s1 = f.getvalue()
813813
assert s1 == "hello"
@@ -818,7 +818,7 @@ def test_text(self):
818818

819819
def test_unicode_and_str_mixture(self):
820820
sio = io.StringIO()
821-
f = capture.CaptureAndPassthroughIO(sio)
821+
f = capture.TeeCaptureIO(sio)
822822
f.write("\u00f6")
823823
pytest.raises(TypeError, f.write, b"hello")
824824

0 commit comments

Comments
 (0)