diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 7bd319b1a..ece690006 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -539,7 +539,10 @@ class FDCaptureBinary(object): self.tmpfile_fd = tmpfile.fileno() def __repr__(self): - return "" % (self.targetfd, self.targetfd_save) + return "" % ( + self.targetfd, + getattr(self, "targetfd_save", None), + ) def start(self): """ Start capturing on targetfd using memorized tmpfile. """ diff --git a/testing/test_capture.py b/testing/test_capture.py index 91cf8d8cf..5ed806fa4 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1231,20 +1231,27 @@ class TestStdCaptureFDinvalidFD(object): """ import os from _pytest import capture + def StdCaptureFD(out=True, err=True, in_=True): return capture.MultiCapture(out, err, in_, - Capture=capture.FDCapture) + Capture=capture.FDCapture) + def test_stdout(): os.close(1) cap = StdCaptureFD(out=True, err=False, in_=False) + assert repr(cap.out) == "" cap.stop_capturing() + def test_stderr(): os.close(2) cap = StdCaptureFD(out=False, err=True, in_=False) + assert repr(cap.err) == "" cap.stop_capturing() + def test_stdin(): os.close(0) cap = StdCaptureFD(out=False, err=False, in_=True) + assert repr(cap.in_) == "" cap.stop_capturing() """ )