mirror of
https://github.com/python/cpython.git
synced 2025-10-15 11:22:18 +00:00
#7960: fix docstrings for captured_output and captured_stdout.
This commit is contained in:
parent
3c0d8a1cc7
commit
fc778fd067
1 changed files with 8 additions and 8 deletions
|
@ -712,14 +712,8 @@ def transient_internet(resource_name, *, timeout=30.0, errnos=()):
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def captured_output(stream_name):
|
def captured_output(stream_name):
|
||||||
"""Run the 'with' statement body using a StringIO object in place of a
|
"""Return a context manager used by captured_stdout and captured_stdin
|
||||||
specific attribute on the sys module.
|
that temporarily replaces the sys stream *stream_name* with a StringIO."""
|
||||||
Example use (with 'stream_name=stdout')::
|
|
||||||
|
|
||||||
with captured_stdout() as s:
|
|
||||||
print("hello")
|
|
||||||
assert s.getvalue() == "hello"
|
|
||||||
"""
|
|
||||||
import io
|
import io
|
||||||
orig_stdout = getattr(sys, stream_name)
|
orig_stdout = getattr(sys, stream_name)
|
||||||
setattr(sys, stream_name, io.StringIO())
|
setattr(sys, stream_name, io.StringIO())
|
||||||
|
@ -729,6 +723,12 @@ def captured_output(stream_name):
|
||||||
setattr(sys, stream_name, orig_stdout)
|
setattr(sys, stream_name, orig_stdout)
|
||||||
|
|
||||||
def captured_stdout():
|
def captured_stdout():
|
||||||
|
"""Capture the output of sys.stdout:
|
||||||
|
|
||||||
|
with captured_stdout() as s:
|
||||||
|
print("hello")
|
||||||
|
self.assertEqual(s.getvalue(), "hello")
|
||||||
|
"""
|
||||||
return captured_output("stdout")
|
return captured_output("stdout")
|
||||||
|
|
||||||
def gc_collect():
|
def gc_collect():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue