[Python-checkins] r62090 - python/trunk/Lib/test/test_support.py

brett.cannon python-checkins at python.org
Tue Apr 1 14:37:44 CEST 2008


Author: brett.cannon
Date: Tue Apr  1 14:37:43 2008
New Revision: 62090

Modified:
   python/trunk/Lib/test/test_support.py
Log:
Generalize test.test_support.test_stdout() with a base context manager so that
it is easy to capture stderr if desired.


Modified: python/trunk/Lib/test/test_support.py
==============================================================================
--- python/trunk/Lib/test/test_support.py	(original)
+++ python/trunk/Lib/test/test_support.py	Tue Apr  1 14:37:43 2008
@@ -392,19 +392,23 @@
 
 
 @contextlib.contextmanager
-def captured_stdout():
-    """Run the with statement body using a StringIO object as sys.stdout.
-    Example use::
+def captured_output(stream_name):
+    """Run the 'with' statement body using a StringIO object in place of a
+    specific attribute on the sys module.
+    Example use (with 'stream_name=stdout')::
 
        with captured_stdout() as s:
            print "hello"
        assert s.getvalue() == "hello"
     """
     import StringIO
-    orig_stdout = sys.stdout
-    sys.stdout = StringIO.StringIO()
-    yield sys.stdout
-    sys.stdout = orig_stdout
+    orig_stdout = getattr(sys, stream_name)
+    setattr(sys, stream_name, StringIO.StringIO())
+    yield getattr(sys, stream_name)
+    setattr(sys, stream_name, orig_stdout)
+
+def captured_stdout():
+    return captured_output("stdout")
 
 
 #=======================================================================


More information about the Python-checkins mailing list