2014-01-25 04:22:19 +08:00
|
|
|
"""
|
2014-03-14 19:49:36 +08:00
|
|
|
per-test stdout/stderr capturing mechanism.
|
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
"""
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2014-03-14 22:58:16 +08:00
|
|
|
|
2016-06-09 07:21:17 +08:00
|
|
|
import contextlib
|
2013-09-30 19:14:16 +08:00
|
|
|
import sys
|
2009-09-05 22:54:52 +08:00
|
|
|
import os
|
2016-09-26 18:15:35 +08:00
|
|
|
from io import UnsupportedOperation
|
2014-03-28 14:11:25 +08:00
|
|
|
from tempfile import TemporaryFile
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
import py
|
|
|
|
import pytest
|
2017-02-20 01:16:32 +08:00
|
|
|
from _pytest.compat import CaptureIO
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-03-28 18:27:02 +08:00
|
|
|
unicode = py.builtin.text
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
patchsysdict = {0: 'stdin', 1: 'stdout', 2: 'stderr'}
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("general")
|
2014-01-25 04:22:19 +08:00
|
|
|
group._addoption(
|
2014-07-28 19:17:37 +08:00
|
|
|
'--capture', action="store",
|
2014-04-01 20:32:12 +08:00
|
|
|
default="fd" if hasattr(os, "dup") else "sys",
|
2013-07-25 21:33:43 +08:00
|
|
|
metavar="method", choices=['fd', 'sys', 'no'],
|
2014-04-01 20:32:12 +08:00
|
|
|
help="per-test capturing method: one of fd|sys|no.")
|
2014-01-25 04:22:19 +08:00
|
|
|
group._addoption(
|
|
|
|
'-s', action="store_const", const="no", dest="capture",
|
2009-10-17 23:43:59 +08:00
|
|
|
help="shortcut for --capture=no.")
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2014-10-09 02:23:40 +08:00
|
|
|
def pytest_load_initial_conftests(early_config, parser, args):
|
2015-12-26 03:46:19 +08:00
|
|
|
_readline_workaround()
|
2014-04-02 17:29:23 +08:00
|
|
|
ns = early_config.known_args_namespace
|
2014-03-14 19:49:36 +08:00
|
|
|
pluginmanager = early_config.pluginmanager
|
2014-04-01 21:03:17 +08:00
|
|
|
capman = CaptureManager(ns.capture)
|
2014-03-14 19:49:36 +08:00
|
|
|
pluginmanager.register(capman, "capturemanager")
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2013-09-29 04:23:00 +08:00
|
|
|
# make sure that capturemanager is properly reset at final shutdown
|
2015-04-22 22:33:20 +08:00
|
|
|
early_config.add_cleanup(capman.reset_capturings)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2013-10-02 18:39:01 +08:00
|
|
|
# make sure logging does not raise exceptions at the end
|
2013-09-30 22:09:26 +08:00
|
|
|
def silence_logging_at_shutdown():
|
|
|
|
if "logging" in sys.modules:
|
|
|
|
sys.modules["logging"].raiseExceptions = False
|
2015-04-22 22:33:20 +08:00
|
|
|
early_config.add_cleanup(silence_logging_at_shutdown)
|
2013-09-30 19:14:16 +08:00
|
|
|
|
|
|
|
# finally trigger conftest loading but while capturing (issue93)
|
2014-04-01 20:32:12 +08:00
|
|
|
capman.init_capturings()
|
2014-10-09 02:23:40 +08:00
|
|
|
outcome = yield
|
|
|
|
out, err = capman.suspendcapture()
|
|
|
|
if outcome.excinfo is not None:
|
2013-09-30 19:14:16 +08:00
|
|
|
sys.stdout.write(out)
|
|
|
|
sys.stderr.write(err)
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2017-01-09 12:52:42 +08:00
|
|
|
class CaptureManager(object):
|
2014-04-01 20:32:12 +08:00
|
|
|
def __init__(self, method):
|
|
|
|
self._method = method
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2010-05-18 01:00:39 +08:00
|
|
|
def _getcapture(self, method):
|
2010-07-27 03:15:15 +08:00
|
|
|
if method == "fd":
|
2014-04-01 20:19:52 +08:00
|
|
|
return MultiCapture(out=True, err=True, Capture=FDCapture)
|
2009-07-31 20:21:02 +08:00
|
|
|
elif method == "sys":
|
2014-04-01 20:19:52 +08:00
|
|
|
return MultiCapture(out=True, err=True, Capture=SysCapture)
|
2010-05-18 01:00:39 +08:00
|
|
|
elif method == "no":
|
2014-04-01 20:19:52 +08:00
|
|
|
return MultiCapture(out=False, err=False, in_=False)
|
2009-07-31 20:21:02 +08:00
|
|
|
else:
|
|
|
|
raise ValueError("unknown capturing method: %r" % method)
|
|
|
|
|
2014-04-01 20:32:12 +08:00
|
|
|
def init_capturings(self):
|
|
|
|
assert not hasattr(self, "_capturing")
|
|
|
|
self._capturing = self._getcapture(self._method)
|
|
|
|
self._capturing.start_capturing()
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def reset_capturings(self):
|
2014-04-01 20:32:12 +08:00
|
|
|
cap = self.__dict__.pop("_capturing", None)
|
|
|
|
if cap is not None:
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.pop_outerr_to_orig()
|
2014-03-28 14:11:25 +08:00
|
|
|
cap.stop_capturing()
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2014-04-01 20:32:12 +08:00
|
|
|
def resumecapture(self):
|
|
|
|
self._capturing.resume_capturing()
|
|
|
|
|
|
|
|
def suspendcapture(self, in_=False):
|
2009-07-31 20:21:02 +08:00
|
|
|
self.deactivate_funcargs()
|
2014-04-01 20:32:12 +08:00
|
|
|
cap = getattr(self, "_capturing", None)
|
|
|
|
if cap is not None:
|
2015-07-26 07:15:51 +08:00
|
|
|
try:
|
|
|
|
outerr = cap.readouterr()
|
|
|
|
finally:
|
|
|
|
cap.suspend_capturing(in_=in_)
|
2014-04-01 20:32:12 +08:00
|
|
|
return outerr
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def activate_funcargs(self, pyfuncitem):
|
2014-03-28 14:13:08 +08:00
|
|
|
capfuncarg = pyfuncitem.__dict__.pop("_capfuncarg", None)
|
|
|
|
if capfuncarg is not None:
|
|
|
|
capfuncarg._start()
|
|
|
|
self._capfuncarg = capfuncarg
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def deactivate_funcargs(self):
|
2014-03-28 14:13:08 +08:00
|
|
|
capfuncarg = self.__dict__.pop("_capfuncarg", None)
|
|
|
|
if capfuncarg is not None:
|
|
|
|
capfuncarg.close()
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2014-10-09 02:23:40 +08:00
|
|
|
def pytest_make_collect_report(self, collector):
|
|
|
|
if isinstance(collector, pytest.File):
|
|
|
|
self.resumecapture()
|
|
|
|
outcome = yield
|
2014-04-01 20:32:12 +08:00
|
|
|
out, err = self.suspendcapture()
|
2014-10-09 02:23:40 +08:00
|
|
|
rep = outcome.get_result()
|
|
|
|
if out:
|
|
|
|
rep.sections.append(("Captured stdout", out))
|
|
|
|
if err:
|
|
|
|
rep.sections.append(("Captured stderr", err))
|
|
|
|
else:
|
|
|
|
yield
|
2014-03-14 19:49:36 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_runtest_setup(self, item):
|
2014-04-01 21:03:17 +08:00
|
|
|
self.resumecapture()
|
|
|
|
yield
|
|
|
|
self.suspendcapture_item(item, "setup")
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_runtest_call(self, item):
|
2014-04-01 21:03:17 +08:00
|
|
|
self.resumecapture()
|
|
|
|
self.activate_funcargs(item)
|
|
|
|
yield
|
|
|
|
#self.deactivate_funcargs() called from suspendcapture()
|
|
|
|
self.suspendcapture_item(item, "call")
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_runtest_teardown(self, item):
|
2014-04-01 21:03:17 +08:00
|
|
|
self.resumecapture()
|
|
|
|
yield
|
|
|
|
self.suspendcapture_item(item, "teardown")
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(tryfirst=True)
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_keyboard_interrupt(self, excinfo):
|
2014-03-14 19:49:36 +08:00
|
|
|
self.reset_capturings()
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(tryfirst=True)
|
2014-03-14 19:49:36 +08:00
|
|
|
def pytest_internalerror(self, excinfo):
|
|
|
|
self.reset_capturings()
|
|
|
|
|
2016-06-09 07:21:17 +08:00
|
|
|
def suspendcapture_item(self, item, when, in_=False):
|
|
|
|
out, err = self.suspendcapture(in_=in_)
|
2015-09-23 02:29:16 +08:00
|
|
|
item.add_report_section(when, "stdout", out)
|
|
|
|
item.add_report_section(when, "stderr", err)
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-06-04 03:01:27 +08:00
|
|
|
error_capsysfderror = "cannot use capsys and capfd at the same time"
|
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-03-28 16:46:38 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def capsys(request):
|
2016-07-22 18:39:06 +08:00
|
|
|
"""Enable capturing of writes to sys.stdout/sys.stderr and make
|
2011-03-04 06:40:38 +08:00
|
|
|
captured output available via ``capsys.readouterr()`` method calls
|
|
|
|
which return a ``(out, err)`` tuple.
|
2010-07-27 03:15:15 +08:00
|
|
|
"""
|
2016-07-07 08:35:32 +08:00
|
|
|
if "capfd" in request.fixturenames:
|
2012-07-19 15:20:14 +08:00
|
|
|
raise request.raiseerror(error_capsysfderror)
|
2016-06-09 07:21:17 +08:00
|
|
|
request.node._capfuncarg = c = CaptureFixture(SysCapture, request)
|
2014-03-28 14:13:08 +08:00
|
|
|
return c
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2014-03-28 16:46:38 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def capfd(request):
|
2016-07-22 18:39:06 +08:00
|
|
|
"""Enable capturing of writes to file descriptors 1 and 2 and make
|
2014-04-03 15:59:04 +08:00
|
|
|
captured output available via ``capfd.readouterr()`` method calls
|
2011-03-04 06:40:38 +08:00
|
|
|
which return a ``(out, err)`` tuple.
|
2010-07-27 03:15:15 +08:00
|
|
|
"""
|
2016-07-07 08:35:32 +08:00
|
|
|
if "capsys" in request.fixturenames:
|
2012-07-19 15:20:14 +08:00
|
|
|
request.raiseerror(error_capsysfderror)
|
2010-05-18 01:00:39 +08:00
|
|
|
if not hasattr(os, 'dup'):
|
2012-06-04 03:01:27 +08:00
|
|
|
pytest.skip("capfd funcarg needs os.dup")
|
2016-06-09 07:21:17 +08:00
|
|
|
request.node._capfuncarg = c = CaptureFixture(FDCapture, request)
|
2014-03-28 14:13:08 +08:00
|
|
|
return c
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
|
2017-01-09 12:52:42 +08:00
|
|
|
class CaptureFixture(object):
|
2016-06-09 07:21:17 +08:00
|
|
|
def __init__(self, captureclass, request):
|
2014-03-28 14:11:25 +08:00
|
|
|
self.captureclass = captureclass
|
2016-06-09 07:21:17 +08:00
|
|
|
self.request = request
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def _start(self):
|
2014-04-01 20:19:52 +08:00
|
|
|
self._capture = MultiCapture(out=True, err=True, in_=False,
|
2016-06-09 07:21:17 +08:00
|
|
|
Capture=self.captureclass)
|
2014-03-14 19:49:36 +08:00
|
|
|
self._capture.start_capturing()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2014-03-28 14:13:08 +08:00
|
|
|
def close(self):
|
|
|
|
cap = self.__dict__.pop("_capture", None)
|
|
|
|
if cap is not None:
|
2014-04-01 20:32:12 +08:00
|
|
|
self._outerr = cap.pop_outerr_to_orig()
|
2014-03-28 14:13:08 +08:00
|
|
|
cap.stop_capturing()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def readouterr(self):
|
2012-11-22 03:43:31 +08:00
|
|
|
try:
|
2014-01-25 04:22:19 +08:00
|
|
|
return self._capture.readouterr()
|
2012-11-22 03:43:31 +08:00
|
|
|
except AttributeError:
|
2014-04-01 20:32:12 +08:00
|
|
|
return self._outerr
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2016-06-09 07:21:17 +08:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def disabled(self):
|
|
|
|
capmanager = self.request.config.pluginmanager.getplugin('capturemanager')
|
|
|
|
capmanager.suspendcapture_item(self.request.node, "call", in_=True)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
capmanager.resumecapture()
|
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-03-28 18:27:02 +08:00
|
|
|
def safe_text_dupfile(f, mode, default_encoding="UTF8"):
|
|
|
|
""" return a open text file object that's a duplicate of f on the
|
|
|
|
FD-level if possible.
|
2014-01-25 04:22:19 +08:00
|
|
|
"""
|
2014-03-28 18:27:02 +08:00
|
|
|
encoding = getattr(f, "encoding", None)
|
2014-01-25 04:22:19 +08:00
|
|
|
try:
|
|
|
|
fd = f.fileno()
|
2014-03-28 18:27:02 +08:00
|
|
|
except Exception:
|
|
|
|
if "b" not in getattr(f, "mode", "") and hasattr(f, "encoding"):
|
|
|
|
# we seem to have a text stream, let's just use it
|
|
|
|
return f
|
2014-01-25 04:22:19 +08:00
|
|
|
else:
|
2014-03-28 18:27:02 +08:00
|
|
|
newfd = os.dup(fd)
|
|
|
|
if "b" not in mode:
|
|
|
|
mode += "b"
|
|
|
|
f = os.fdopen(newfd, mode, 0) # no buffering
|
|
|
|
return EncodedFile(f, encoding or default_encoding)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
class EncodedFile(object):
|
2014-08-07 17:05:42 +08:00
|
|
|
errors = "strict" # possibly needed by py3 code (issue555)
|
2014-03-28 18:27:02 +08:00
|
|
|
def __init__(self, buffer, encoding):
|
|
|
|
self.buffer = buffer
|
2014-01-25 04:22:19 +08:00
|
|
|
self.encoding = encoding
|
|
|
|
|
|
|
|
def write(self, obj):
|
|
|
|
if isinstance(obj, unicode):
|
2014-03-28 18:27:02 +08:00
|
|
|
obj = obj.encode(self.encoding, "replace")
|
|
|
|
self.buffer.write(obj)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
def writelines(self, linelist):
|
|
|
|
data = ''.join(linelist)
|
|
|
|
self.write(data)
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2014-09-06 06:55:14 +08:00
|
|
|
return getattr(object.__getattribute__(self, "buffer"), name)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
|
2014-04-01 20:19:52 +08:00
|
|
|
class MultiCapture(object):
|
2014-03-28 14:03:37 +08:00
|
|
|
out = err = in_ = None
|
|
|
|
|
|
|
|
def __init__(self, out=True, err=True, in_=True, Capture=None):
|
|
|
|
if in_:
|
|
|
|
self.in_ = Capture(0)
|
|
|
|
if out:
|
|
|
|
self.out = Capture(1)
|
|
|
|
if err:
|
|
|
|
self.err = Capture(2)
|
|
|
|
|
2014-03-14 19:49:36 +08:00
|
|
|
def start_capturing(self):
|
2014-03-28 14:03:37 +08:00
|
|
|
if self.in_:
|
2014-01-25 04:22:19 +08:00
|
|
|
self.in_.start()
|
2014-03-28 14:03:37 +08:00
|
|
|
if self.out:
|
2014-01-25 04:22:19 +08:00
|
|
|
self.out.start()
|
2014-03-28 14:03:37 +08:00
|
|
|
if self.err:
|
2014-01-25 04:22:19 +08:00
|
|
|
self.err.start()
|
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
def pop_outerr_to_orig(self):
|
|
|
|
""" pop current snapshot out/err capture and flush to orig streams. """
|
|
|
|
out, err = self.readouterr()
|
|
|
|
if out:
|
|
|
|
self.out.writeorg(out)
|
|
|
|
if err:
|
|
|
|
self.err.writeorg(err)
|
2014-04-01 20:32:12 +08:00
|
|
|
return out, err
|
2014-03-28 14:03:37 +08:00
|
|
|
|
2014-04-01 20:32:12 +08:00
|
|
|
def suspend_capturing(self, in_=False):
|
2014-04-01 20:19:58 +08:00
|
|
|
if self.out:
|
|
|
|
self.out.suspend()
|
|
|
|
if self.err:
|
|
|
|
self.err.suspend()
|
2014-04-01 20:32:12 +08:00
|
|
|
if in_ and self.in_:
|
|
|
|
self.in_.suspend()
|
|
|
|
self._in_suspended = True
|
2014-04-01 20:19:58 +08:00
|
|
|
|
|
|
|
def resume_capturing(self):
|
|
|
|
if self.out:
|
|
|
|
self.out.resume()
|
|
|
|
if self.err:
|
|
|
|
self.err.resume()
|
2014-04-01 20:32:12 +08:00
|
|
|
if hasattr(self, "_in_suspended"):
|
|
|
|
self.in_.resume()
|
|
|
|
del self._in_suspended
|
2014-04-01 20:19:58 +08:00
|
|
|
|
2014-03-28 14:11:25 +08:00
|
|
|
def stop_capturing(self):
|
|
|
|
""" stop capturing and reset capturing streams """
|
|
|
|
if hasattr(self, '_reset'):
|
|
|
|
raise ValueError("was already stopped")
|
|
|
|
self._reset = True
|
|
|
|
if self.out:
|
|
|
|
self.out.done()
|
|
|
|
if self.err:
|
|
|
|
self.err.done()
|
|
|
|
if self.in_:
|
|
|
|
self.in_.done()
|
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
def readouterr(self):
|
2014-03-28 14:03:37 +08:00
|
|
|
""" return snapshot unicode value of stdout/stderr capturings. """
|
2014-03-28 18:27:02 +08:00
|
|
|
return (self.out.snap() if self.out is not None else "",
|
|
|
|
self.err.snap() if self.err is not None else "")
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2017-01-09 12:52:42 +08:00
|
|
|
class NoCapture(object):
|
2014-04-01 20:19:58 +08:00
|
|
|
__init__ = start = done = suspend = resume = lambda *args: None
|
2014-03-28 14:03:37 +08:00
|
|
|
|
2017-01-09 12:52:42 +08:00
|
|
|
class FDCapture(object):
|
2014-03-28 14:03:37 +08:00
|
|
|
""" Capture IO to/from a given os-level filedescriptor. """
|
|
|
|
|
2015-12-26 03:51:55 +08:00
|
|
|
def __init__(self, targetfd, tmpfile=None):
|
2014-03-28 14:03:37 +08:00
|
|
|
self.targetfd = targetfd
|
|
|
|
try:
|
2014-04-01 20:19:55 +08:00
|
|
|
self.targetfd_save = os.dup(self.targetfd)
|
2014-03-28 14:03:37 +08:00
|
|
|
except OSError:
|
|
|
|
self.start = lambda: None
|
|
|
|
self.done = lambda: None
|
|
|
|
else:
|
2014-04-01 20:19:55 +08:00
|
|
|
if targetfd == 0:
|
|
|
|
assert not tmpfile, "cannot set tmpfile with stdin"
|
|
|
|
tmpfile = open(os.devnull, "r")
|
|
|
|
self.syscapture = SysCapture(targetfd)
|
|
|
|
else:
|
|
|
|
if tmpfile is None:
|
2014-03-28 14:11:25 +08:00
|
|
|
f = TemporaryFile()
|
|
|
|
with f:
|
2014-03-28 18:27:02 +08:00
|
|
|
tmpfile = safe_text_dupfile(f, mode="wb+")
|
2014-04-01 20:19:55 +08:00
|
|
|
if targetfd in patchsysdict:
|
|
|
|
self.syscapture = SysCapture(targetfd, tmpfile)
|
|
|
|
else:
|
|
|
|
self.syscapture = NoCapture()
|
2014-03-28 14:03:37 +08:00
|
|
|
self.tmpfile = tmpfile
|
2014-04-01 20:19:55 +08:00
|
|
|
self.tmpfile_fd = tmpfile.fileno()
|
2014-03-28 14:03:37 +08:00
|
|
|
|
2014-03-28 14:11:25 +08:00
|
|
|
def __repr__(self):
|
2014-04-01 20:19:55 +08:00
|
|
|
return "<FDCapture %s oldfd=%s>" % (self.targetfd, self.targetfd_save)
|
2014-03-28 14:11:25 +08:00
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
def start(self):
|
|
|
|
""" Start capturing on targetfd using memorized tmpfile. """
|
|
|
|
try:
|
2014-04-01 20:19:55 +08:00
|
|
|
os.fstat(self.targetfd_save)
|
|
|
|
except (AttributeError, OSError):
|
2014-03-28 14:03:37 +08:00
|
|
|
raise ValueError("saved filedescriptor not valid anymore")
|
2014-04-01 20:19:55 +08:00
|
|
|
os.dup2(self.tmpfile_fd, self.targetfd)
|
|
|
|
self.syscapture.start()
|
2014-03-28 14:03:37 +08:00
|
|
|
|
2014-03-28 14:11:25 +08:00
|
|
|
def snap(self):
|
|
|
|
f = self.tmpfile
|
|
|
|
f.seek(0)
|
|
|
|
res = f.read()
|
|
|
|
if res:
|
|
|
|
enc = getattr(f, "encoding", None)
|
|
|
|
if enc and isinstance(res, bytes):
|
|
|
|
res = py.builtin._totext(res, enc, "replace")
|
|
|
|
f.truncate(0)
|
|
|
|
f.seek(0)
|
2014-04-01 20:32:12 +08:00
|
|
|
return res
|
|
|
|
return ''
|
2014-03-28 14:11:25 +08:00
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
def done(self):
|
|
|
|
""" stop capturing, restore streams, return original capture file,
|
|
|
|
seeked to position zero. """
|
2014-04-01 20:19:55 +08:00
|
|
|
targetfd_save = self.__dict__.pop("targetfd_save")
|
|
|
|
os.dup2(targetfd_save, self.targetfd)
|
|
|
|
os.close(targetfd_save)
|
|
|
|
self.syscapture.done()
|
2014-03-28 14:11:25 +08:00
|
|
|
self.tmpfile.close()
|
2014-03-14 19:49:36 +08:00
|
|
|
|
2014-04-01 20:19:55 +08:00
|
|
|
def suspend(self):
|
|
|
|
self.syscapture.suspend()
|
|
|
|
os.dup2(self.targetfd_save, self.targetfd)
|
|
|
|
|
|
|
|
def resume(self):
|
|
|
|
self.syscapture.resume()
|
|
|
|
os.dup2(self.tmpfile_fd, self.targetfd)
|
|
|
|
|
2014-03-14 19:49:36 +08:00
|
|
|
def writeorg(self, data):
|
2014-03-28 14:13:08 +08:00
|
|
|
""" write to original file descriptor. """
|
2014-03-28 14:03:37 +08:00
|
|
|
if py.builtin._istext(data):
|
|
|
|
data = data.encode("utf8") # XXX use encoding of original stream
|
2014-04-01 20:19:55 +08:00
|
|
|
os.write(self.targetfd_save, data)
|
2014-03-14 19:49:36 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2017-01-09 12:52:42 +08:00
|
|
|
class SysCapture(object):
|
2014-04-01 20:19:55 +08:00
|
|
|
def __init__(self, fd, tmpfile=None):
|
2014-03-28 14:03:37 +08:00
|
|
|
name = patchsysdict[fd]
|
|
|
|
self._old = getattr(sys, name)
|
|
|
|
self.name = name
|
2014-04-01 20:19:55 +08:00
|
|
|
if tmpfile is None:
|
|
|
|
if name == "stdin":
|
|
|
|
tmpfile = DontReadFromInput()
|
|
|
|
else:
|
2017-02-20 01:16:32 +08:00
|
|
|
tmpfile = CaptureIO()
|
2014-04-01 20:19:55 +08:00
|
|
|
self.tmpfile = tmpfile
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
def start(self):
|
|
|
|
setattr(sys, self.name, self.tmpfile)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-03-28 14:11:25 +08:00
|
|
|
def snap(self):
|
|
|
|
f = self.tmpfile
|
|
|
|
res = f.getvalue()
|
|
|
|
f.truncate(0)
|
|
|
|
f.seek(0)
|
|
|
|
return res
|
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
def done(self):
|
|
|
|
setattr(sys, self.name, self._old)
|
2014-04-01 20:19:55 +08:00
|
|
|
del self._old
|
2014-03-28 14:11:25 +08:00
|
|
|
self.tmpfile.close()
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-04-01 20:19:55 +08:00
|
|
|
def suspend(self):
|
|
|
|
setattr(sys, self.name, self._old)
|
|
|
|
|
|
|
|
def resume(self):
|
|
|
|
setattr(sys, self.name, self.tmpfile)
|
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
def writeorg(self, data):
|
|
|
|
self._old.write(data)
|
|
|
|
self._old.flush()
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
|
2017-01-09 12:52:42 +08:00
|
|
|
class DontReadFromInput(object):
|
2014-01-25 04:22:19 +08:00
|
|
|
"""Temporary stub class. Ideally when stdin is accessed, the
|
|
|
|
capturing should be turned off, with possibly all data captured
|
|
|
|
so far sent to the screen. This should be configurable, though,
|
|
|
|
because in automated test runs it is better to crash than
|
|
|
|
hang indefinitely.
|
|
|
|
"""
|
2014-09-22 19:19:27 +08:00
|
|
|
|
|
|
|
encoding = None
|
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
def read(self, *args):
|
|
|
|
raise IOError("reading from stdin while output is captured")
|
|
|
|
readline = read
|
|
|
|
readlines = read
|
|
|
|
__iter__ = read
|
|
|
|
|
|
|
|
def fileno(self):
|
2016-09-26 18:59:28 +08:00
|
|
|
raise UnsupportedOperation("redirected stdin is pseudofile, "
|
|
|
|
"has no fileno()")
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
def isatty(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
pass
|
2015-12-25 05:43:34 +08:00
|
|
|
|
2016-08-25 06:44:43 +08:00
|
|
|
@property
|
|
|
|
def buffer(self):
|
|
|
|
if sys.version_info >= (3,0):
|
|
|
|
return self
|
|
|
|
else:
|
|
|
|
raise AttributeError('redirected stdin has no attribute buffer')
|
|
|
|
|
2015-12-25 05:43:34 +08:00
|
|
|
|
2015-12-26 03:46:19 +08:00
|
|
|
def _readline_workaround():
|
2015-12-25 05:43:34 +08:00
|
|
|
"""
|
|
|
|
Ensure readline is imported so that it attaches to the correct stdio
|
|
|
|
handles on Windows.
|
|
|
|
|
|
|
|
Pdb uses readline support where available--when not running from the Python
|
|
|
|
prompt, the readline module is not imported until running the pdb REPL. If
|
2016-06-21 22:16:57 +08:00
|
|
|
running pytest with the --pdb option this means the readline module is not
|
2015-12-25 05:43:34 +08:00
|
|
|
imported until after I/O capture has been started.
|
|
|
|
|
|
|
|
This is a problem for pyreadline, which is often used to implement readline
|
|
|
|
support on Windows, as it does not attach to the correct handles for stdout
|
|
|
|
and/or stdin if they have been redirected by the FDCapture mechanism. This
|
|
|
|
workaround ensures that readline is imported before I/O capture is setup so
|
|
|
|
that it can attach to the actual stdin/out for the console.
|
|
|
|
|
|
|
|
See https://github.com/pytest-dev/pytest/pull/1281
|
|
|
|
"""
|
|
|
|
|
2015-12-26 03:46:19 +08:00
|
|
|
if not sys.platform.startswith('win32'):
|
|
|
|
return
|
2015-12-25 05:43:34 +08:00
|
|
|
try:
|
|
|
|
import readline # noqa
|
|
|
|
except ImportError:
|
|
|
|
pass
|