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
|
|
|
"""
|
2014-03-14 22:58:16 +08:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
import sys
|
2009-09-05 22:54:52 +08:00
|
|
|
import os
|
2014-03-28 14:11:25 +08:00
|
|
|
from tempfile import TemporaryFile
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
import py
|
|
|
|
import pytest
|
|
|
|
|
2014-03-28 16:46:38 +08:00
|
|
|
from py.io import TextIO
|
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
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
@pytest.mark.tryfirst
|
2013-09-30 19:14:16 +08:00
|
|
|
def pytest_load_initial_conftests(early_config, parser, args, __multicall__):
|
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
|
2014-04-01 20:19:58 +08:00
|
|
|
pluginmanager.add_shutdown(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
|
2014-03-14 19:49:36 +08:00
|
|
|
pluginmanager.add_shutdown(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()
|
2013-09-30 19:14:16 +08:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return __multicall__.execute()
|
|
|
|
finally:
|
|
|
|
out, err = capman.suspendcapture()
|
|
|
|
except:
|
|
|
|
sys.stdout.write(out)
|
|
|
|
sys.stderr.write(err)
|
|
|
|
raise
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
class CaptureManager:
|
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:
|
|
|
|
outerr = cap.readouterr()
|
|
|
|
cap.suspend_capturing(in_=in_)
|
|
|
|
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
|
|
|
|
2014-04-01 20:32:12 +08:00
|
|
|
@pytest.mark.tryfirst
|
2009-08-12 01:00:41 +08:00
|
|
|
def pytest_make_collect_report(self, __multicall__, collector):
|
2014-04-01 20:32:12 +08:00
|
|
|
if not isinstance(collector, pytest.File):
|
2014-01-25 04:22:19 +08:00
|
|
|
return
|
2014-04-01 20:32:12 +08:00
|
|
|
self.resumecapture()
|
|
|
|
try:
|
|
|
|
rep = __multicall__.execute()
|
|
|
|
finally:
|
|
|
|
out, err = self.suspendcapture()
|
|
|
|
if out:
|
|
|
|
rep.sections.append(("Captured stdout", out))
|
|
|
|
if err:
|
|
|
|
rep.sections.append(("Captured stderr", err))
|
|
|
|
return rep
|
2014-03-14 19:49:36 +08:00
|
|
|
|
|
|
|
@pytest.mark.hookwrapper
|
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
|
|
|
|
2014-03-14 19:49:36 +08:00
|
|
|
@pytest.mark.hookwrapper
|
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
|
|
|
|
2014-03-14 19:49:36 +08:00
|
|
|
@pytest.mark.hookwrapper
|
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
|
|
|
|
2014-03-14 19:49:36 +08:00
|
|
|
@pytest.mark.tryfirst
|
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
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
@pytest.mark.tryfirst
|
2014-03-14 19:49:36 +08:00
|
|
|
def pytest_internalerror(self, excinfo):
|
|
|
|
self.reset_capturings()
|
|
|
|
|
2014-04-01 21:03:17 +08:00
|
|
|
def suspendcapture_item(self, item, when):
|
2014-04-01 20:32:12 +08:00
|
|
|
out, err = self.suspendcapture()
|
2014-03-14 19:49:36 +08:00
|
|
|
item.add_report_section(when, "out", out)
|
|
|
|
item.add_report_section(when, "err", err)
|
2009-07-26 00:09:01 +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):
|
2011-03-04 06:40:38 +08:00
|
|
|
"""enables capturing of writes to sys.stdout/sys.stderr and makes
|
|
|
|
captured output available via ``capsys.readouterr()`` method calls
|
|
|
|
which return a ``(out, err)`` tuple.
|
2010-07-27 03:15:15 +08:00
|
|
|
"""
|
2012-07-19 01:49:14 +08:00
|
|
|
if "capfd" in request._funcargs:
|
2012-07-19 15:20:14 +08:00
|
|
|
raise request.raiseerror(error_capsysfderror)
|
2014-03-28 14:13:08 +08:00
|
|
|
request.node._capfuncarg = c = CaptureFixture(SysCapture)
|
|
|
|
return c
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2014-03-28 16:46:38 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def capfd(request):
|
2011-03-04 06:40:38 +08:00
|
|
|
"""enables capturing of writes to file descriptors 1 and 2 and makes
|
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
|
|
|
"""
|
2012-07-19 01:49:14 +08:00
|
|
|
if "capsys" in request._funcargs:
|
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")
|
2014-03-28 14:13:08 +08:00
|
|
|
request.node._capfuncarg = c = CaptureFixture(FDCapture)
|
|
|
|
return c
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
|
2012-10-05 20:24:44 +08:00
|
|
|
class CaptureFixture:
|
2010-10-05 23:56:37 +08:00
|
|
|
def __init__(self, captureclass):
|
2014-03-28 14:11:25 +08:00
|
|
|
self.captureclass = captureclass
|
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,
|
2014-03-28 14:11:25 +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
|
|
|
|
|
|
|
|
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-03-28 18:27:02 +08:00
|
|
|
return getattr(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
|
|
|
|
2014-04-01 20:19:55 +08:00
|
|
|
class NoCapture:
|
2014-04-01 20:19:58 +08:00
|
|
|
__init__ = start = done = suspend = resume = lambda *args: None
|
2014-03-28 14:03:37 +08:00
|
|
|
|
|
|
|
class FDCapture:
|
|
|
|
""" Capture IO to/from a given os-level filedescriptor. """
|
|
|
|
|
|
|
|
def __init__(self, targetfd, tmpfile=None):
|
|
|
|
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
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
class SysCapture:
|
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:
|
|
|
|
tmpfile = TextIO()
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class DontReadFromInput:
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
def read(self, *args):
|
|
|
|
raise IOError("reading from stdin while output is captured")
|
|
|
|
readline = read
|
|
|
|
readlines = read
|
|
|
|
__iter__ = read
|
|
|
|
|
|
|
|
def fileno(self):
|
|
|
|
raise ValueError("redirected Stdin is pseudofile, has no fileno()")
|
|
|
|
|
|
|
|
def isatty(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
pass
|