Skip to content

[WIP] capture: do not close tmpfile buffer, but redirect #6034

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 65 additions & 16 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
per-test stdout/stderr capturing mechanism.

"""
import atexit
import collections
import contextlib
import io
import os
import sys
from io import UnsupportedOperation
from tempfile import TemporaryFile
from typing import Callable
from typing import List
from typing import Optional

import pytest
from _pytest.compat import CaptureIO
Expand Down Expand Up @@ -77,19 +81,29 @@ def __init__(self, method):
self._method = method
self._global_capturing = None
self._current_item = None
self._atexit_funcs = [] # type: List[Callable]
atexit.register(self._atexit_run)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tmpfile management can be isolated in a separate class, which would greatly simplify the code:

class Streams(IntEnum):

    STDIN = 0
    STDOUT = 1
    STDERR = 2


@attr.s
class TemporaryFilePool:

    _tmpfiles = attr.ib(default=attr.factory(dict))

    def obtain_tmpfile(self, stream_id):
        try:
            tmpfile = self._tmpfiles[stream_id]            
        except KeyError:
            if stream_id == Streams.STDIN:
                tmpfile = open(os.devnull, "r")
            else:                
                f = TemporaryFile()
                with f:
                    tmpfile = safe_text_dupfile(f, mode="wb+")
            self._tmpfiles[stream_id] = tmpfile
        
        assert not tmpfile.closed
        return tmpfile

    def close(self):
        for f in self._tmpfiles.values():
            f.close()
        self._tmpfiles.clear()

We should then make CaptureManager create and maintain the tmpfile_pool, and pass it around to FDCaptureBinary. CaptureManager should then register the atexit handle itself, so these 3 lines would become:

self._tmpfile_pool = TemporaryFilePool()
atexit.register(self._tmpfile_pool)

This design also allows us to change the pool implementation, so we could even possibly control it via a config variable and get back the old behavior (I'm not actually suggesting it, just mentioning that this is then possible).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I also realize now that we don't respect basetemp when creating TemporaryFiles...

self._tmpfiles = {}

def __repr__(self):
return "<CaptureManager _method={!r} _global_capturing={!r} _current_item={!r}>".format(
self._method, self._global_capturing, self._current_item
)

def _atexit_register(self, func):
self._atexit_funcs.append(func)

def _atexit_run(self):
for func in self._atexit_funcs:
func()

def _getcapture(self, method):
if method == "fd":
return MultiCapture(out=True, err=True, Capture=FDCapture)
return MultiCapture(out=True, err=True, Capture=FDCapture, capman=self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we would pass self._tmpfile_pool to the MultiCapture objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think passing capman is more universal.
(IIRC I am using this when auto-suspending on readin from stdin)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more universal means more coupling here... I prefer to pass only the necessary. I also suggest making it mandatory, so we don't have to check for its existense first (might require a few test changes I suppose).

elif method == "sys":
return MultiCapture(out=True, err=True, Capture=SysCapture)
return MultiCapture(out=True, err=True, Capture=SysCapture, capman=self)
elif method == "no":
return MultiCapture(out=False, err=False, in_=False)
return MultiCapture(out=False, err=False, in_=False, capman=self)
raise ValueError("unknown capturing method: %r" % method) # pragma: no cover

def is_capturing(self):
Expand Down Expand Up @@ -450,13 +464,20 @@ class MultiCapture:
out = err = in_ = None
_state = None

def __init__(self, out=True, err=True, in_=True, Capture=None):
def __init__(
self,
out=True,
err=True,
in_=True,
Capture=None,
capman: Optional[CaptureManager] = None,
):
if in_:
self.in_ = Capture(0)
self.in_ = Capture(0, capman=capman)
if out:
self.out = Capture(1)
self.out = Capture(1, capman=capman)
if err:
self.err = Capture(2)
self.err = Capture(2, capman=capman)

def __repr__(self):
return "<MultiCapture out={!r} err={!r} in_={!r} _state={!r} _in_suspended={!r}>".format(
Expand Down Expand Up @@ -539,8 +560,9 @@ class FDCaptureBinary:
EMPTY_BUFFER = b""
_state = None

def __init__(self, targetfd, tmpfile=None):
def __init__(self, targetfd, tmpfile=None, capman: Optional[CaptureManager] = None):
self.targetfd = targetfd
self._capman = capman
try:
self.targetfd_save = os.dup(self.targetfd)
except OSError:
Expand All @@ -551,15 +573,33 @@ def __init__(self, targetfd, tmpfile=None):
self.done = self._done
if targetfd == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this entire if/else can now be just:

tmpfile = self._tmpfile_pool.obtain_tmpfile(self.targetfd)

assert not tmpfile, "cannot set tmpfile with stdin"
tmpfile = open(os.devnull, "r")
if capman:
try:
tmpfile = capman._tmpfiles[0]
assert not tmpfile.closed
except KeyError:
tmpfile = open(os.devnull, "r")
capman._tmpfiles[targetfd] = tmpfile
else:
tmpfile = open(os.devnull, "r")
self.syscapture = SysCapture(targetfd)
else:
if tmpfile is None:
f = TemporaryFile()
with f:
tmpfile = safe_text_dupfile(f, mode="wb+")
if capman:
try:
tmpfile = capman._tmpfiles[targetfd]
assert not tmpfile.closed
except KeyError:
f = TemporaryFile()
with f:
tmpfile = safe_text_dupfile(f, mode="wb+")
capman._tmpfiles[targetfd] = tmpfile
else:
f = TemporaryFile()
with f:
tmpfile = safe_text_dupfile(f, mode="wb+")
if targetfd in patchsysdict:
self.syscapture = SysCapture(targetfd, tmpfile)
self.syscapture = SysCapture(targetfd, tmpfile, capman)
else:
self.syscapture = NoCapture()
self.tmpfile = tmpfile
Expand Down Expand Up @@ -594,7 +634,12 @@ def _done(self):
os.dup2(targetfd_save, self.targetfd)
os.close(targetfd_save)
self.syscapture.done()
self.tmpfile.close()
if self._capman:
# Redirect any remaining output.
os.dup2(self.targetfd, self.tmpfile_fd)
self._capman._atexit_register(self.tmpfile.close)
else:
self.tmpfile.close()
self._state = "done"

def suspend(self):
Expand Down Expand Up @@ -636,8 +681,9 @@ class SysCapture:
EMPTY_BUFFER = str()
_state = None

def __init__(self, fd, tmpfile=None):
def __init__(self, fd, tmpfile=None, capman: Optional[CaptureManager] = None):
name = patchsysdict[fd]
self._capman = capman
self._old = getattr(sys, name)
self.name = name
if tmpfile is None:
Expand Down Expand Up @@ -665,7 +711,10 @@ def snap(self):
def done(self):
setattr(sys, self.name, self._old)
del self._old
self.tmpfile.close()
if self._capman:
self._capman._atexit_register(self.tmpfile.close)
else:
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 @@ -1490,3 +1490,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