Skip to content

Commit b696666

Browse files
authored
Merge pull request #4945 from blueyed/FDCapture-repr-None-targetfd_save
capture: fix FDCapture.__repr__ without targetfd_save
2 parents f4f6cb7 + 9898127 commit b696666

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/_pytest/capture.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,10 @@ def __init__(self, targetfd, tmpfile=None):
539539
self.tmpfile_fd = tmpfile.fileno()
540540

541541
def __repr__(self):
542-
return "<FDCapture %s oldfd=%s>" % (self.targetfd, self.targetfd_save)
542+
return "<FDCapture %s oldfd=%s>" % (
543+
self.targetfd,
544+
getattr(self, "targetfd_save", None),
545+
)
543546

544547
def start(self):
545548
""" Start capturing on targetfd using memorized tmpfile. """

testing/test_capture.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,20 +1231,27 @@ def test_stdcapture_fd_invalid_fd(self, testdir):
12311231
"""
12321232
import os
12331233
from _pytest import capture
1234+
12341235
def StdCaptureFD(out=True, err=True, in_=True):
12351236
return capture.MultiCapture(out, err, in_,
1236-
Capture=capture.FDCapture)
1237+
Capture=capture.FDCapture)
1238+
12371239
def test_stdout():
12381240
os.close(1)
12391241
cap = StdCaptureFD(out=True, err=False, in_=False)
1242+
assert repr(cap.out) == "<FDCapture 1 oldfd=None>"
12401243
cap.stop_capturing()
1244+
12411245
def test_stderr():
12421246
os.close(2)
12431247
cap = StdCaptureFD(out=False, err=True, in_=False)
1248+
assert repr(cap.err) == "<FDCapture 2 oldfd=None>"
12441249
cap.stop_capturing()
1250+
12451251
def test_stdin():
12461252
os.close(0)
12471253
cap = StdCaptureFD(out=False, err=False, in_=True)
1254+
assert repr(cap.in_) == "<FDCapture 0 oldfd=None>"
12481255
cap.stop_capturing()
12491256
"""
12501257
)

0 commit comments

Comments
 (0)