|
11 | 11 | from tempfile import TemporaryFile |
12 | 12 | from typing import Generator |
13 | 13 | from typing import Optional |
| 14 | +from typing import TextIO |
14 | 15 |
|
15 | 16 | import pytest |
16 | | -from _pytest.compat import CaptureAndPassthroughIO |
17 | | -from _pytest.compat import CaptureIO |
18 | 17 | from _pytest.compat import TYPE_CHECKING |
19 | 18 | from _pytest.config import Config |
20 | 19 | from _pytest.fixtures import FixtureRequest |
@@ -320,6 +319,25 @@ def capfdbinary(request): |
320 | 319 | yield fixture |
321 | 320 |
|
322 | 321 |
|
| 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 | + |
323 | 341 | class CaptureFixture: |
324 | 342 | """ |
325 | 343 | 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): |
673 | 691 | if name == "stdin": |
674 | 692 | tmpfile = DontReadFromInput() |
675 | 693 | else: |
676 | | - tmpfile = CaptureAndPassthroughIO(self._old) |
| 694 | + tmpfile = TeeCaptureIO(self._old) |
677 | 695 | self.tmpfile = tmpfile |
678 | 696 |
|
679 | 697 |
|
|
0 commit comments