Object returned by the :fixture:`capsys`, :fixture:`capsysbinary`, :fixture:`capfd` and :fixture:`capfdbinary` fixtures.
| 914 | |
| 915 | |
| 916 | class CaptureFixture(Generic[AnyStr]): |
| 917 | """Object returned by the :fixture:`capsys`, :fixture:`capsysbinary`, |
| 918 | :fixture:`capfd` and :fixture:`capfdbinary` fixtures.""" |
| 919 | |
| 920 | def __init__( |
| 921 | self, |
| 922 | captureclass: type[CaptureBase[AnyStr]], |
| 923 | request: SubRequest, |
| 924 | *, |
| 925 | config: dict[str, Any] | None = None, |
| 926 | _ispytest: bool = False, |
| 927 | ) -> None: |
| 928 | check_ispytest(_ispytest) |
| 929 | self.captureclass: type[CaptureBase[AnyStr]] = captureclass |
| 930 | self.request = request |
| 931 | self._config = config if config else {} |
| 932 | self._capture: MultiCapture[AnyStr] | None = None |
| 933 | self._captured_out: AnyStr = self.captureclass.EMPTY_BUFFER |
| 934 | self._captured_err: AnyStr = self.captureclass.EMPTY_BUFFER |
| 935 | |
| 936 | def _start(self) -> None: |
| 937 | if self._capture is None: |
| 938 | self._capture = MultiCapture( |
| 939 | in_=None, |
| 940 | out=self.captureclass(1, **self._config), |
| 941 | err=self.captureclass(2, **self._config), |
| 942 | ) |
| 943 | self._capture.start_capturing() |
| 944 | |
| 945 | def close(self) -> None: |
| 946 | if self._capture is not None: |
| 947 | if self._config.get("tee"): |
| 948 | # When tee is enabled, output was already written to the |
| 949 | # original stream in real-time by TeeCaptureIO. Using |
| 950 | # pop_outerr_to_orig() would write it a second time via |
| 951 | # writeorg(), causing doubled output (see #13784). |
| 952 | out, err = self._capture.readouterr() |
| 953 | else: |
| 954 | out, err = self._capture.pop_outerr_to_orig() |
| 955 | self._captured_out += out |
| 956 | self._captured_err += err |
| 957 | self._capture.stop_capturing() |
| 958 | self._capture = None |
| 959 | |
| 960 | def readouterr(self) -> CaptureResult[AnyStr]: |
| 961 | """Read and return the captured output so far, resetting the internal |
| 962 | buffer. |
| 963 | |
| 964 | :returns: |
| 965 | The captured content as a namedtuple with ``out`` and ``err`` |
| 966 | string attributes. |
| 967 | """ |
| 968 | captured_out, captured_err = self._captured_out, self._captured_err |
| 969 | if self._capture is not None: |
| 970 | out, err = self._capture.readouterr() |
| 971 | captured_out += out |
| 972 | captured_err += err |
| 973 | self._captured_out = self.captureclass.EMPTY_BUFFER |
no outgoing calls