[Python-checkins] cpython (merge 3.3 -> default): Merge #17987: properly document support.captured_xxx.
r.david.murray
python-checkins at python.org
Thu Jul 11 18:30:12 CEST 2013
http://hg.python.org/cpython/rev/d0f7f1996001
changeset: 84549:d0f7f1996001
parent: 84547:19ed630d8d75
parent: 84548:af2416c2e27c
user: R David Murray <rdmurray at bitdance.com>
date: Thu Jul 11 12:29:31 2013 -0400
summary:
Merge #17987: properly document support.captured_xxx.
files:
Doc/library/test.rst | 26 +++++++++++++++++++-------
Lib/test/support.py | 19 +++++++++++++++++--
Lib/test/test_support.py | 17 ++++++++++-------
Misc/ACKS | 1 +
4 files changed, 47 insertions(+), 16 deletions(-)
diff --git a/Doc/library/test.rst b/Doc/library/test.rst
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -362,17 +362,29 @@
New optional arguments *filters* and *quiet*.
-.. function:: captured_stdout()
+.. function:: captured_stdin()
+ captured_stdout()
+ captured_stderr()
- A context manager that runs the :keyword:`with` statement body using a
- :class:`io.StringIO` object as sys.stdout. That object can be retrieved
- using the ``as`` clause of the :keyword:`with` statement.
+ A context managers that temporarily replaces the named stream with
+ :class:`io.StringIO` object.
- Example use::
+ Example use with output streams::
- with captured_stdout() as s:
+ with captured_stdout() as stdout, captured_stderr() as stderr:
print("hello")
- assert s.getvalue() == "hello\n"
+ print("error", file=sys.stderr)
+ assert stdout.getvalue() == "hello\n"
+ assert stderr.getvalue() == "error\n"
+
+ Example use with input stream::
+
+ with captured_stdin() as stdin:
+ stdin.write('hello\n')
+ stdin.seek(0)
+ # call test code that consumes from sys.stdin
+ captured = input()
+ self.assertEqual(captured, "hello")
.. function:: temp_cwd(name='tempcwd', quiet=False, path=None)
diff --git a/Lib/test/support.py b/Lib/test/support.py
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -1186,16 +1186,31 @@
def captured_stdout():
"""Capture the output of sys.stdout:
- with captured_stdout() as s:
+ with captured_stdout() as stdout:
print("hello")
- self.assertEqual(s.getvalue(), "hello")
+ self.assertEqual(stdout.getvalue(), "hello\n")
"""
return captured_output("stdout")
def captured_stderr():
+ """Capture the output of sys.stderr:
+
+ with captured_stderr() as stderr:
+ print("hello", file=sys.stderr)
+ self.assertEqual(stderr.getvalue(), "hello\n")
+ """
return captured_output("stderr")
def captured_stdin():
+ """Capture the input to sys.stdin:
+
+ with captured_stdin() as stdin:
+ stdin.write('hello\n')
+ stdin.seek(0)
+ # call test code that consumes from sys.stdin
+ captured = input()
+ self.assertEqual(captured, "hello")
+ """
return captured_output("stdin")
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -130,19 +130,22 @@
self.assertNotIn("bar", sys.path)
def test_captured_stdout(self):
- with support.captured_stdout() as s:
+ with support.captured_stdout() as stdout:
print("hello")
- self.assertEqual(s.getvalue(), "hello\n")
+ self.assertEqual(stdout.getvalue(), "hello\n")
def test_captured_stderr(self):
- with support.captured_stderr() as s:
+ with support.captured_stderr() as stderr:
print("hello", file=sys.stderr)
- self.assertEqual(s.getvalue(), "hello\n")
+ self.assertEqual(stderr.getvalue(), "hello\n")
def test_captured_stdin(self):
- with support.captured_stdin() as s:
- print("hello", file=sys.stdin)
- self.assertEqual(s.getvalue(), "hello\n")
+ with support.captured_stdin() as stdin:
+ stdin.write('hello\n')
+ stdin.seek(0)
+ # call test code that consumes from sys.stdin
+ captured = input()
+ self.assertEqual(captured, "hello")
def test_gc_collect(self):
support.gc_collect()
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -66,6 +66,7 @@
Jeff Balogh
Manuel Balsera
Matt Bandy
+Dmi Baranov
Michael J. Barber
Daniel Barclay
Nicolas Bareil
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list