|
11 | 11 | from tempfile import TemporaryFile |
12 | 12 | from typing import BinaryIO |
13 | 13 | from typing import Generator |
| 14 | +from typing import IO |
14 | 15 | from typing import Iterable |
15 | 16 | from typing import Optional |
16 | 17 |
|
17 | 18 | import pytest |
18 | | -from _pytest.compat import CaptureAndPassthroughIO |
19 | | -from _pytest.compat import CaptureIO |
20 | 19 | from _pytest.config import Config |
21 | 20 | from _pytest.fixtures import FixtureRequest |
22 | 21 |
|
@@ -323,6 +322,25 @@ def capfdbinary(request): |
323 | 322 | yield fixture |
324 | 323 |
|
325 | 324 |
|
| 325 | +class CaptureIO(io.TextIOWrapper): |
| 326 | + def __init__(self) -> None: |
| 327 | + super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True) |
| 328 | + |
| 329 | + def getvalue(self) -> str: |
| 330 | + assert isinstance(self.buffer, io.BytesIO) |
| 331 | + return self.buffer.getvalue().decode("UTF-8") |
| 332 | + |
| 333 | + |
| 334 | +class PassthroughCaptureIO(CaptureIO): |
| 335 | + def __init__(self, other: IO) -> None: |
| 336 | + self._other = other |
| 337 | + super().__init__() |
| 338 | + |
| 339 | + def write(self, s) -> int: |
| 340 | + super().write(s) |
| 341 | + return self._other.write(s) |
| 342 | + |
| 343 | + |
326 | 344 | class CaptureFixture: |
327 | 345 | """ |
328 | 346 | Object returned by :py:func:`capsys`, :py:func:`capsysbinary`, :py:func:`capfd` and :py:func:`capfdbinary` |
@@ -686,16 +704,13 @@ def snap(self): |
686 | 704 |
|
687 | 705 |
|
688 | 706 | class TeeSysCapture(SysCapture): |
689 | | - def __init__(self, fd, tmpfile=None): |
690 | | - name = patchsysdict[fd] |
691 | | - self._old = getattr(sys, name) |
692 | | - self.name = name |
693 | | - if tmpfile is None: |
694 | | - if name == "stdin": |
695 | | - tmpfile = DontReadFromInput() |
696 | | - else: |
697 | | - tmpfile = CaptureAndPassthroughIO(self._old) |
698 | | - self.tmpfile = tmpfile |
| 707 | + def __init__(self, fd: int) -> None: |
| 708 | + old = getattr(sys, patchsysdict[fd]) |
| 709 | + if fd == 0: |
| 710 | + super().__init__(fd) |
| 711 | + else: |
| 712 | + super().__init__(fd, PassthroughCaptureIO(old)) |
| 713 | + assert self._old == old, (self._old, old) |
699 | 714 |
|
700 | 715 |
|
701 | 716 | map_fixname_class = { |
|
0 commit comments