cpython: d0f7f1996001 (original) (raw)
Mercurial > cpython
changeset 84549:d0f7f1996001
Merge #17987: properly document support.captured_xxx. [#17987]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Thu, 11 Jul 2013 12:29:31 -0400 |
parents | 19ed630d8d75(current diff)af2416c2e27c(diff) |
children | e6384b8b2325 |
files | Lib/test/support.py Misc/ACKS |
diffstat | 4 files changed, 48 insertions(+), 17 deletions(-)[+] [-] Doc/library/test.rst 28 Lib/test/support.py 19 Lib/test/test_support.py 17 Misc/ACKS 1 |
line wrap: on
line diff
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -362,17 +362,29 @@ The :mod:test.support
module defines t
New optional arguments filters and quiet.
-.. function:: captured_stdout()
+.. function:: captured_stdin()
captured_stdout()[](#l1.9)
captured_stderr()[](#l1.10)
- 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 with output streams::
with captured_stdout() as stdout, captured_stderr() as stderr:[](#l1.21)
print("hello")[](#l1.22)
print("error", file=sys.stderr)[](#l1.23)
assert stdout.getvalue() == "hello\n"[](#l1.24)
assert stderr.getvalue() == "error\n"[](#l1.25)
with captured_stdout() as s:[](#l1.27)
print("hello")[](#l1.28)
assert s.getvalue() == "hello\n"[](#l1.29)
- Example use with input stream:: +
with captured_stdin() as stdin:[](#l1.32)
stdin.write('hello\n')[](#l1.33)
stdin.seek(0)[](#l1.34)
# call test code that consumes from sys.stdin[](#l1.35)
captured = input()[](#l1.36)
self.assertEqual(captured, "hello")[](#l1.37)
.. function:: temp_cwd(name='tempcwd', quiet=False, path=None)
--- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -1186,16 +1186,31 @@ def captured_output(stream_name): def captured_stdout(): """Capture the output of sys.stdout:
with captured_stdout() as s:[](#l2.7)
with captured_stdout() as stdout:[](#l2.8) print("hello")[](#l2.9)
self.assertEqual(s.getvalue(), "hello")[](#l2.10)
""" return captured_output("stdout") def captured_stderr():self.assertEqual(stdout.getvalue(), "hello\n")[](#l2.11)
- """Capture the output of sys.stderr:
with captured_stderr() as stderr:[](#l2.18)
print("hello", file=sys.stderr)[](#l2.19)
self.assertEqual(stderr.getvalue(), "hello\n")[](#l2.20)
- """ return captured_output("stderr") def captured_stdin():
- """Capture the input to sys.stdin:
with captured_stdin() as stdin:[](#l2.27)
stdin.write('hello\n')[](#l2.28)
stdin.seek(0)[](#l2.29)
# call test code that consumes from sys.stdin[](#l2.30)
captured = input()[](#l2.31)
self.assertEqual(captured, "hello")[](#l2.32)
- """ return captured_output("stdin")
--- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -130,19 +130,22 @@ class TestSupport(unittest.TestCase): self.assertNotIn("bar", sys.path) def test_captured_stdout(self):
with support.captured_stdout() as s:[](#l3.7)
with support.captured_stdout() as stdout:[](#l3.8) print("hello")[](#l3.9)
self.assertEqual(s.getvalue(), "hello\n")[](#l3.10)
self.assertEqual(stdout.getvalue(), "hello\n")[](#l3.11)
def test_captured_stderr(self):
with support.captured_stderr() as s:[](#l3.14)
with support.captured_stderr() as stderr:[](#l3.15) print("hello", file=sys.stderr)[](#l3.16)
self.assertEqual(s.getvalue(), "hello\n")[](#l3.17)
self.assertEqual(stderr.getvalue(), "hello\n")[](#l3.18)
def test_captured_stdin(self):
with support.captured_stdin() as s:[](#l3.21)
print("hello", file=sys.stdin)[](#l3.22)
self.assertEqual(s.getvalue(), "hello\n")[](#l3.23)
with support.captured_stdin() as stdin:[](#l3.24)
stdin.write('hello\n')[](#l3.25)
stdin.seek(0)[](#l3.26)
# call test code that consumes from sys.stdin[](#l3.27)
captured = input()[](#l3.28)
self.assertEqual(captured, "hello")[](#l3.29)
def test_gc_collect(self): support.gc_collect()
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -66,6 +66,7 @@ Luigi Ballabio Jeff Balogh Manuel Balsera Matt Bandy +Dmi Baranov Michael J. Barber Daniel Barclay Nicolas Bareil