Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-41147 Document that redirect_stdout provides the new stream as context var #21199

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Doc/library/contextlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,11 @@ Functions and classes provided:

For example, the output of :func:`help` normally is sent to *sys.stdout*.
You can capture that output in a string by redirecting the output to an
:class:`io.StringIO` object::
:class:`io.StringIO` object. The replacement stream is returned from the
``__enter__`` method and so is available as the target of the
:keyword:`with` statement::

f = io.StringIO()
with redirect_stdout(f):
with redirect_stdout(io.StringIO()) as f:
PeterJCLaw marked this conversation as resolved.
Show resolved Hide resolved
help(pow)
s = f.getvalue()

Expand Down