Skip to content

Commit

Permalink
capture: revisit/fix __repr__, define _in_suspended (#6749)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Feb 19, 2020
1 parent 2b13a9b commit fb16d3e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
27 changes: 16 additions & 11 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def __getattr__(self, name):
class MultiCapture:
out = err = in_ = None
_state = None
_in_suspended = False

def __init__(self, out=True, err=True, in_=True, Capture=None):
if in_:
Expand All @@ -449,11 +450,7 @@ def __init__(self, out=True, err=True, in_=True, Capture=None):

def __repr__(self):
return "<MultiCapture out={!r} err={!r} in_={!r} _state={!r} _in_suspended={!r}>".format(
self.out,
self.err,
self.in_,
self._state,
getattr(self, "_in_suspended", "<UNSET>"),
self.out, self.err, self.in_, self._state, self._in_suspended,
)

def start_capturing(self):
Expand Down Expand Up @@ -490,9 +487,9 @@ def resume_capturing(self):
self.out.resume()
if self.err:
self.err.resume()
if hasattr(self, "_in_suspended"):
if self._in_suspended:
self.in_.resume()
del self._in_suspended
self._in_suspended = False

def stop_capturing(self):
""" stop capturing and reset capturing streams """
Expand Down Expand Up @@ -555,8 +552,12 @@ def __init__(self, targetfd, tmpfile=None):
self.tmpfile_fd = tmpfile.fileno()

def __repr__(self):
return "<FDCapture {} oldfd={} _state={!r}>".format(
self.targetfd, getattr(self, "targetfd_save", None), self._state
return "<{} {} oldfd={} _state={!r} tmpfile={}>".format(
self.__class__.__name__,
self.targetfd,
getattr(self, "targetfd_save", "<UNSET>"),
self._state,
hasattr(self, "tmpfile") and repr(self.tmpfile) or "<UNSET>",
)

def _start(self):
Expand Down Expand Up @@ -637,8 +638,12 @@ def __init__(self, fd, tmpfile=None):
self.tmpfile = tmpfile

def __repr__(self):
return "<SysCapture {} _old={!r}, tmpfile={!r} _state={!r}>".format(
self.name, self._old, self.tmpfile, self._state
return "<{} {} _old={} _state={!r} tmpfile={!r}>".format(
self.__class__.__name__,
self.name,
hasattr(self, "_old") and repr(self._old) or "<UNSET>",
self._state,
self.tmpfile,
)

def start(self):
Expand Down
18 changes: 15 additions & 3 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,18 @@ def test_simple_resume_suspend(self):
cap.done()
pytest.raises(AttributeError, cap.suspend)

assert repr(cap) == (
"<FDCapture 1 oldfd=<UNSET> _state='done' tmpfile={!r}>".format(
cap.tmpfile
)
)
# Should not crash with missing "_old".
assert repr(cap.syscapture) == (
"<SysCapture stdout _old=<UNSET> _state='done' tmpfile={!r}>".format(
cap.syscapture.tmpfile
)
)

def test_capfd_sys_stdout_mode(self, capfd):
assert "b" not in sys.stdout.mode

Expand Down Expand Up @@ -1212,19 +1224,19 @@ def StdCaptureFD(out=True, err=True, in_=True):
def test_stdout():
os.close(1)
cap = StdCaptureFD(out=True, err=False, in_=False)
assert repr(cap.out) == "<FDCapture 1 oldfd=None _state=None>"
assert repr(cap.out) == "<FDCapture 1 oldfd=<UNSET> _state=None tmpfile=<UNSET>>"
cap.stop_capturing()
def test_stderr():
os.close(2)
cap = StdCaptureFD(out=False, err=True, in_=False)
assert repr(cap.err) == "<FDCapture 2 oldfd=None _state=None>"
assert repr(cap.err) == "<FDCapture 2 oldfd=<UNSET> _state=None tmpfile=<UNSET>>"
cap.stop_capturing()
def test_stdin():
os.close(0)
cap = StdCaptureFD(out=False, err=False, in_=True)
assert repr(cap.in_) == "<FDCapture 0 oldfd=None _state=None>"
assert repr(cap.in_) == "<FDCapture 0 oldfd=<UNSET> _state=None tmpfile=<UNSET>>"
cap.stop_capturing()
"""
)
Expand Down

0 comments on commit fb16d3e

Please sign in to comment.