[svn r37772] have both capturings have the same done/reset semantics

(should also fix a buildcmodule related problem, e.g. for
greenlets)

--HG--
branch : trunk
This commit is contained in:
hpk 2007-02-01 22:52:42 +01:00
parent 31f4c194e8
commit 7fd122a4c1
2 changed files with 24 additions and 9 deletions

View File

@ -22,6 +22,16 @@ class Capture(object):
return res, out, err return res, out, err
call = classmethod(call) call = classmethod(call)
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()
class StdCaptureFD(Capture): class StdCaptureFD(Capture):
""" capture Stdout and Stderr both on filedescriptor """ capture Stdout and Stderr both on filedescriptor
and sys.stdout/stderr level. and sys.stdout/stderr level.
@ -40,18 +50,14 @@ class StdCaptureFD(Capture):
if patchsys: if patchsys:
self.err.setasfile('stderr') self.err.setasfile('stderr')
def reset(self): def done(self):
""" reset sys.stdout and sys.stderr """ return (outfile, errfile) and stop capturing. """
returns a tuple of file objects (out, err) for the captured
data
"""
outfile = errfile = emptyfile outfile = errfile = emptyfile
if hasattr(self, 'out'): if hasattr(self, 'out'):
outfile = self.out.done() outfile = self.out.done()
if hasattr(self, 'err'): if hasattr(self, 'err'):
errfile = self.err.done() errfile = self.err.done()
return outfile.read(), errfile.read() return outfile, errfile
class StdCapture(Capture): class StdCapture(Capture):
""" capture sys.stdout/sys.stderr (but not system level fd 1 and 2). """ capture sys.stdout/sys.stderr (but not system level fd 1 and 2).
@ -76,11 +82,12 @@ class StdCapture(Capture):
sys.stdin = self.newin = DontReadFromInput() sys.stdin = self.newin = DontReadFromInput()
def reset(self): def reset(self):
""" return captured output and restore sys.stdout/err.""" """ return captured output as strings and restore sys.stdout/err."""
x, y = self.done() x, y = self.done()
return x.read(), y.read() return x.read(), y.read()
def done(self): def done(self):
""" return (outfile, errfile) and stop capturing. """
o,e = sys.stdout, sys.stderr o,e = sys.stdout, sys.stderr
outfile = errfile = emptyfile outfile = errfile = emptyfile
if self._out: if self._out:

View File

@ -61,7 +61,15 @@ class TestStdCapture:
def getcapture(self, **kw): def getcapture(self, **kw):
return py.io.StdCapture(**kw) return py.io.StdCapture(**kw)
def test_capturing_simple(self): def test_capturing_done_simple(self):
cap = self.getcapture()
print "hello world"
print >>sys.stderr, "hello error"
outfile, errfile = cap.done()
assert outfile.read() == "hello world\n"
assert errfile.read() == "hello error\n"
def test_capturing_reset_simple(self):
cap = self.getcapture() cap = self.getcapture()
print "hello world" print "hello world"
print >>sys.stderr, "hello error" print >>sys.stderr, "hello error"