-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Preserve chronological order of stdout and stderr with capsys
#6690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow ``capsys`` to retrieve combined stdout + stderr streams with original order of messages. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,7 +366,7 @@ def close(self): | |
self._capture.stop_capturing() | ||
self._capture = None | ||
|
||
def readouterr(self): | ||
def readouterr(self) -> "CaptureResult": | ||
"""Read and return the captured output so far, resetting the internal buffer. | ||
|
||
:return: captured content as a namedtuple with ``out`` and ``err`` string attributes | ||
|
@@ -380,6 +380,17 @@ def readouterr(self): | |
self._captured_err = self.captureclass.EMPTY_BUFFER | ||
return CaptureResult(captured_out, captured_err) | ||
|
||
def read_combined(self) -> str: | ||
""" | ||
Read captured output with both stdout and stderr streams combined, with preserving | ||
the correct order of messages. | ||
""" | ||
if self.captureclass is not OrderedCapture: | ||
raise AttributeError("Only capsys is able to combine streams.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is usually a design smell for a superclass to know about a particular subclass, perhaps this method should defer to the captured data's class and the particular subclasses can decide whether they implement this api or not |
||
result = "".join(line[0] for line in OrderedCapture.streams) | ||
OrderedCapture.flush() | ||
return result | ||
|
||
def _suspend(self): | ||
"""Suspends this fixture's own capturing temporarily.""" | ||
if self._capture is not None: | ||
|
@@ -622,13 +633,17 @@ def __init__(self, fd, tmpfile=None): | |
name = patchsysdict[fd] | ||
self._old = getattr(sys, name) | ||
self.name = name | ||
self.fd = fd | ||
if tmpfile is None: | ||
if name == "stdin": | ||
tmpfile = DontReadFromInput() | ||
else: | ||
tmpfile = CaptureIO() | ||
tmpfile = self._get_writer() | ||
self.tmpfile = tmpfile | ||
|
||
def _get_writer(self): | ||
return CaptureIO() | ||
aklajnert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __repr__(self): | ||
return "<{} {} _old={} _state={!r} tmpfile={!r}>".format( | ||
self.__class__.__name__, | ||
|
@@ -695,14 +710,65 @@ def __init__(self, fd, tmpfile=None): | |
self.tmpfile = tmpfile | ||
|
||
|
||
class OrderedCapture(SysCapture): | ||
"""Capture class that keeps streams in order.""" | ||
|
||
streams = collections.deque() # type: collections.deque | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems a bit odd for this to be a class member and not an instance member (as written this makes it effectively a global variable). I'd rather see it as an instance variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, but see my comment in in #6690 (comment):
IOW I don't know how to easily fix this, as @aklajnert also commented. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think all that's needed is to grow another api on the capture classes which represents combined? if that's possible I think that would also fix the problem below of the backward-class-coupling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm but currently who deals with each capture class is Or do you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh I see :/ maybe nevermind then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it is a bit frustrating, that's why I think we have to rethink the whole design (in a future PR that is). |
||
|
||
def _get_writer(self): | ||
return OrderedWriter(self.fd) | ||
aklajnert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def snap(self): | ||
res = self.tmpfile.getvalue() | ||
if self.name == "stderr": | ||
# both streams are being read one after another, while stderr is last - it will clear the queue | ||
self.flush() | ||
return res | ||
|
||
@classmethod | ||
def flush(cls) -> None: | ||
"""Clear streams.""" | ||
cls.streams.clear() | ||
|
||
@classmethod | ||
def close(cls) -> None: | ||
cls.flush() | ||
|
||
|
||
map_fixname_class = { | ||
"capfd": FDCapture, | ||
"capfdbinary": FDCaptureBinary, | ||
"capsys": SysCapture, | ||
"capsys": OrderedCapture, | ||
"capsysbinary": SysCaptureBinary, | ||
} | ||
|
||
|
||
class OrderedWriter: | ||
encoding = sys.getdefaultencoding() | ||
|
||
def __init__(self, fd: int) -> None: | ||
super().__init__() | ||
self._fd = fd # type: int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think mypy can infer this value |
||
|
||
def write(self, text: str, **kwargs) -> int: | ||
OrderedCapture.streams.append((text, self._fd)) | ||
aklajnert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return len(text) | ||
|
||
def getvalue(self) -> str: | ||
return "".join( | ||
line[0] for line in OrderedCapture.streams if line[1] == self._fd | ||
) | ||
|
||
def flush(self) -> None: | ||
pass | ||
|
||
def isatty(self) -> bool: | ||
return False | ||
|
||
def close(self) -> None: | ||
OrderedCapture.close() | ||
|
||
|
||
class DontReadFromInput: | ||
encoding = None | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm this just occurred to me, to follow with the naming of the other methods (
readouterr
), we should stick toreadcombined
.