From 983d249680e7bb60695df5066bb34abacd172bf3 Mon Sep 17 00:00:00 2001 From: David Szotten Date: Sun, 26 Jul 2015 01:15:51 +0200 Subject: [PATCH 1/2] reset capture even if readouterr throws otherwise that exception (and all following output) end up in /dev/null --- _pytest/capture.py | 6 ++++-- testing/test_capture.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/_pytest/capture.py b/_pytest/capture.py index 855b99bde..58f9cb525 100644 --- a/_pytest/capture.py +++ b/_pytest/capture.py @@ -86,8 +86,10 @@ class CaptureManager: self.deactivate_funcargs() cap = getattr(self, "_capturing", None) if cap is not None: - outerr = cap.readouterr() - cap.suspend_capturing(in_=in_) + try: + outerr = cap.readouterr() + finally: + cap.suspend_capturing(in_=in_) return outerr def activate_funcargs(self, pyfuncitem): diff --git a/testing/test_capture.py b/testing/test_capture.py index 235bacae0..171a920d3 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -566,6 +566,24 @@ def test_capture_binary_output(testdir): result.assert_outcomes(passed=2) +def test_error_during_readouterr(testdir): + """Make sure we suspend capturing if errors occurr during readouterr""" + testdir.makepyfile(pytest_xyz=""" + from _pytest.capture import FDCapture + def bad_snap(self): + raise Exception('boom') + FDCapture.snap = bad_snap + """) + result = testdir.runpytest_subprocess( + "-p", "pytest_xyz", "--version", syspathinsert=True + ) + result.stderr.fnmatch_lines([ + "*in bad_snap", + " raise Exception('boom')", + "Exception: boom", + ]) + + class TestTextIO: def test_text(self): f = capture.TextIO() From 3e6bee2fc60944f9eba24ff77b41e14e385f4540 Mon Sep 17 00:00:00 2001 From: David Szotten Date: Sun, 26 Jul 2015 01:28:00 +0200 Subject: [PATCH 2/2] fail if snap is removed in the future --- testing/test_capture.py | 1 + 1 file changed, 1 insertion(+) diff --git a/testing/test_capture.py b/testing/test_capture.py index 171a920d3..3c7105170 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -572,6 +572,7 @@ def test_error_during_readouterr(testdir): from _pytest.capture import FDCapture def bad_snap(self): raise Exception('boom') + assert FDCapture.snap FDCapture.snap = bad_snap """) result = testdir.runpytest_subprocess(