[Python-checkins] bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199) (GH-26380)

iritkatriel webhook-mailer at python.org
Wed May 26 10:43:46 EDT 2021


https://github.com/python/cpython/commit/bee66d3cb98e740f9d8057eb7f503122052ca5d8
commit: bee66d3cb98e740f9d8057eb7f503122052ca5d8
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: iritkatriel <iritkatriel at yahoo.com>
date: 2021-05-26T15:43:37+01:00
summary:

bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199) (GH-26380)

(cherry picked from commit 46db39d7bd67fb9fea133cd4f18cdf7eacb0f6d9)

Co-authored-by: Peter Law <PeterJCLaw at gmail.com>

files:
M Doc/library/contextlib.rst

diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index aa848df8c4290..3dcedf72a390e 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -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:
             help(pow)
         s = f.getvalue()
 



More information about the Python-checkins mailing list