Skip to content

Commit

Permalink
capture: do not close tmpfile buffer, but redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Oct 22, 2019
1 parent 978c7ae commit df4e3a6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,10 @@ def _done(self):
os.dup2(targetfd_save, self.targetfd)
os.close(targetfd_save)
self.syscapture.done()
self.tmpfile.close()
# Redirect any remaining output.
os.dup2(self.targetfd, self.tmpfile.buffer.fileno())
# TODO: atexit?
# self.tmpfile.close()
self._state = "done"

def suspend(self):
Expand Down Expand Up @@ -665,7 +668,8 @@ def snap(self):
def done(self):
setattr(sys, self.name, self._old)
del self._old
self.tmpfile.close()
# TODO: atexit?
# self.tmpfile.close()
self._state = "done"

def suspend(self):
Expand Down
44 changes: 44 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1510,3 +1510,47 @@ def test_fails():
result_with_capture.stdout.fnmatch_lines(
["E TypeError: write() argument must be str, not bytes"]
)


def test_logging_in_atexit(testdir):
p = testdir.makepyfile(
"""
import atexit
import logging
import sys
cur_stdout = sys.stdout
LOGGER = logging.getLogger(__name__)
def test_fail():
assert 0
def _atexit():
print("test-print in atexit", cur_stdout)
LOGGER.error("test-log in atexit")
print()
print("test-register")
print()
atexit.register(_atexit)
logging.basicConfig()
LOGGER.error("log_setup_not_shown_from_collection")
print(sys.stderr, id(sys.stderr))
"""
)
result = testdir.runpytest_subprocess(str(p))
result.stdout.fnmatch_lines(
[
"*= 1 failed in *",
"test-print in atexit <_pytest.capture.EncodedFile object *",
]
)
assert result.stderr.lines == ["ERROR:test_logging_in_atexit:test-log in atexit"]
assert result.ret == 1

output = str(result.stdout) + str(result.stderr)
assert "test-register" not in output
assert "*- Captured stderr call -*" not in output
assert "log_setup_not_shown_from_collection" not in output

0 comments on commit df4e3a6

Please sign in to comment.