From 7fd122a4c1b8612268be71dd5876b97916fde206 Mon Sep 17 00:00:00 2001 From: hpk Date: Thu, 1 Feb 2007 22:52:42 +0100 Subject: [PATCH] [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 --- py/io/stdcapture.py | 23 +++++++++++++++-------- py/io/test/test_capture.py | 10 +++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/py/io/stdcapture.py b/py/io/stdcapture.py index 4812cdad1..deabb7133 100644 --- a/py/io/stdcapture.py +++ b/py/io/stdcapture.py @@ -22,6 +22,16 @@ class Capture(object): return res, out, err 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): """ capture Stdout and Stderr both on filedescriptor and sys.stdout/stderr level. @@ -40,18 +50,14 @@ class StdCaptureFD(Capture): if patchsys: self.err.setasfile('stderr') - def reset(self): - """ reset sys.stdout and sys.stderr - - returns a tuple of file objects (out, err) for the captured - data - """ + def done(self): + """ return (outfile, errfile) and stop capturing. """ outfile = errfile = emptyfile if hasattr(self, 'out'): outfile = self.out.done() if hasattr(self, 'err'): errfile = self.err.done() - return outfile.read(), errfile.read() + return outfile, errfile class StdCapture(Capture): """ 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() 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() return x.read(), y.read() def done(self): + """ return (outfile, errfile) and stop capturing. """ o,e = sys.stdout, sys.stderr outfile = errfile = emptyfile if self._out: diff --git a/py/io/test/test_capture.py b/py/io/test/test_capture.py index d2bd70f31..565c6561c 100644 --- a/py/io/test/test_capture.py +++ b/py/io/test/test_capture.py @@ -61,7 +61,15 @@ class TestStdCapture: def getcapture(self, **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() print "hello world" print >>sys.stderr, "hello error"