2007-02-01 23:20:39 +08:00
|
|
|
import os
|
2007-01-24 22:24:01 +08:00
|
|
|
import sys
|
2007-02-01 23:20:39 +08:00
|
|
|
import py
|
2007-01-24 22:24:01 +08:00
|
|
|
try: from cStringIO import StringIO
|
|
|
|
except ImportError: from StringIO import StringIO
|
|
|
|
|
2007-02-02 04:26:27 +08:00
|
|
|
|
2007-02-01 23:20:39 +08:00
|
|
|
class Capture(object):
|
|
|
|
def call(cls, func, *args, **kwargs):
|
|
|
|
""" return a (res, out, err) tuple where
|
|
|
|
out and err represent the output/error output
|
|
|
|
during function execution.
|
|
|
|
call the given function with args/kwargs
|
|
|
|
and capture output/error during its execution.
|
|
|
|
"""
|
|
|
|
so = cls()
|
|
|
|
try:
|
|
|
|
res = func(*args, **kwargs)
|
|
|
|
finally:
|
|
|
|
out, err = so.reset()
|
|
|
|
return res, out, err
|
|
|
|
call = classmethod(call)
|
|
|
|
|
2007-02-02 05:52:42 +08:00
|
|
|
def reset(self):
|
|
|
|
""" reset sys.stdout and sys.stderr
|
|
|
|
|
|
|
|
returns a tuple of file objects (out, err) for the captured
|
|
|
|
data
|
|
|
|
"""
|
|
|
|
outfile, errfile = self.done()
|
|
|
|
return outfile.read(), errfile.read()
|
|
|
|
|
|
|
|
|
2007-02-01 23:20:39 +08:00
|
|
|
class StdCaptureFD(Capture):
|
2007-02-03 21:57:25 +08:00
|
|
|
""" This class allows to capture writes to FD1 and FD2
|
|
|
|
and may connect a NULL file to FD0 (and prevent
|
|
|
|
reads from sys.stdin)
|
2007-02-01 23:20:39 +08:00
|
|
|
"""
|
2007-02-03 21:57:25 +08:00
|
|
|
def __init__(self, out=True, err=True, mixed=False, in_=True, patchsys=True):
|
|
|
|
if in_:
|
|
|
|
self._oldin = (sys.stdin, os.dup(0))
|
|
|
|
sys.stdin = DontReadFromInput()
|
|
|
|
fd = os.open(devnullpath, os.O_RDONLY)
|
|
|
|
os.dup2(fd, 0)
|
|
|
|
os.close(fd)
|
2007-02-01 23:20:39 +08:00
|
|
|
if out:
|
|
|
|
self.out = py.io.FDCapture(1)
|
|
|
|
if patchsys:
|
|
|
|
self.out.setasfile('stdout')
|
|
|
|
if err:
|
2007-02-02 04:26:27 +08:00
|
|
|
if mixed and out:
|
|
|
|
tmpfile = self.out.tmpfile
|
|
|
|
else:
|
|
|
|
tmpfile = None
|
|
|
|
self.err = py.io.FDCapture(2, tmpfile=tmpfile)
|
2007-02-01 23:20:39 +08:00
|
|
|
if patchsys:
|
|
|
|
self.err.setasfile('stderr')
|
|
|
|
|
2007-02-02 05:52:42 +08:00
|
|
|
def done(self):
|
|
|
|
""" return (outfile, errfile) and stop capturing. """
|
2007-02-02 04:26:27 +08:00
|
|
|
outfile = errfile = emptyfile
|
2007-02-01 23:20:39 +08:00
|
|
|
if hasattr(self, 'out'):
|
|
|
|
outfile = self.out.done()
|
|
|
|
if hasattr(self, 'err'):
|
|
|
|
errfile = self.err.done()
|
2007-02-03 21:57:25 +08:00
|
|
|
if hasattr(self, '_oldin'):
|
|
|
|
oldsys, oldfd = self._oldin
|
|
|
|
os.dup2(oldfd, 0)
|
|
|
|
os.close(oldfd)
|
|
|
|
sys.stdin = oldsys
|
2007-02-02 05:52:42 +08:00
|
|
|
return outfile, errfile
|
2007-02-01 23:20:39 +08:00
|
|
|
|
|
|
|
class StdCapture(Capture):
|
2007-02-03 21:57:25 +08:00
|
|
|
""" This class allows to capture writes to sys.stdout|stderr "in-memory"
|
|
|
|
and will raise errors on tries to read from sys.stdin. It only
|
|
|
|
modifies sys.stdout|stderr|stdin attributes and does not
|
|
|
|
touch underlying File Descriptors (use StdCaptureFD for that).
|
2007-01-24 22:24:01 +08:00
|
|
|
"""
|
2007-02-03 21:57:25 +08:00
|
|
|
def __init__(self, out=True, err=True, in_=True, mixed=False):
|
2007-02-02 04:26:27 +08:00
|
|
|
self._out = out
|
|
|
|
self._err = err
|
2007-02-03 21:57:25 +08:00
|
|
|
self._in = in_
|
2007-02-02 04:26:27 +08:00
|
|
|
if out:
|
|
|
|
self.oldout = sys.stdout
|
|
|
|
sys.stdout = self.newout = StringIO()
|
|
|
|
if err:
|
|
|
|
self.olderr = sys.stderr
|
|
|
|
if out and mixed:
|
|
|
|
newerr = self.newout
|
|
|
|
else:
|
|
|
|
newerr = StringIO()
|
|
|
|
sys.stderr = self.newerr = newerr
|
2007-02-03 21:57:25 +08:00
|
|
|
if in_:
|
|
|
|
self.oldin = sys.stdin
|
|
|
|
sys.stdin = self.newin = DontReadFromInput()
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def reset(self):
|
2007-02-02 05:52:42 +08:00
|
|
|
""" return captured output as strings and restore sys.stdout/err."""
|
2007-01-24 22:24:01 +08:00
|
|
|
x, y = self.done()
|
|
|
|
return x.read(), y.read()
|
|
|
|
|
|
|
|
def done(self):
|
2007-02-02 05:52:42 +08:00
|
|
|
""" return (outfile, errfile) and stop capturing. """
|
2007-01-24 22:24:01 +08:00
|
|
|
o,e = sys.stdout, sys.stderr
|
2007-02-02 04:26:27 +08:00
|
|
|
outfile = errfile = emptyfile
|
|
|
|
if self._out:
|
|
|
|
try:
|
|
|
|
sys.stdout = self.oldout
|
|
|
|
except AttributeError:
|
|
|
|
raise IOError("stdout capturing already reset")
|
|
|
|
del self.oldout
|
|
|
|
outfile = self.newout
|
|
|
|
outfile.seek(0)
|
|
|
|
if self._err:
|
|
|
|
try:
|
|
|
|
sys.stderr = self.olderr
|
|
|
|
except AttributeError:
|
|
|
|
raise IOError("stderr capturing already reset")
|
|
|
|
del self.olderr
|
|
|
|
errfile = self.newerr
|
|
|
|
errfile.seek(0)
|
2007-02-03 21:57:25 +08:00
|
|
|
if self._in:
|
|
|
|
sys.stdin = self.oldin
|
2007-02-02 04:26:27 +08:00
|
|
|
return outfile, errfile
|
2007-01-24 22:24:01 +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
|
2007-02-03 21:57:25 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
devnullpath = os.devnull
|
|
|
|
except AttributeError:
|
|
|
|
if os.name == 'nt':
|
|
|
|
devnullpath = 'NUL'
|
|
|
|
else:
|
|
|
|
devnullpath = '/dev/null'
|
|
|
|
|
|
|
|
emptyfile = StringIO()
|
|
|
|
|