summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_support.py
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2008-04-01 12:37:43 (GMT)
committerBrett Cannon <bcannon@gmail.com>2008-04-01 12:37:43 (GMT)
commit8d2a90af2dd04877e5df4c12fd71e1ae86a3b7b9 (patch)
tree400fa0e7b239d923c1014dd645c7c687e0edf1ae /Lib/test/test_support.py
parentd48a2f77f01641ed806379358bd88eaf9e7644c8 (diff)
downloadcpython-8d2a90af2dd04877e5df4c12fd71e1ae86a3b7b9.zip
cpython-8d2a90af2dd04877e5df4c12fd71e1ae86a3b7b9.tar.gz
cpython-8d2a90af2dd04877e5df4c12fd71e1ae86a3b7b9.tar.bz2
Generalize test.test_support.test_stdout() with a base context manager so that
it is easy to capture stderr if desired.
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r--Lib/test/test_support.py18
1 files changed, 11 insertions, 7 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 1c89d5b..7a5f30a 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -392,19 +392,23 @@ def transient_internet():
@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")
#=======================================================================