Skip to content
Open
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
16 changes: 12 additions & 4 deletions Doc/library/contextlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,12 @@ Functions and classes provided:
.. function:: redirect_stdout(new_target)

Context manager for temporarily redirecting :data:`sys.stdout` to
another file or file-like object.
another :term:`file object`.

This tool adds flexibility to existing functions or classes whose output
is hardwired to stdout.
is hardwired to :data:`sys.stdout`. This does not modify underlying file
objects or file descriptors. It sets the global :data:`sys.stdout` to the
provided value and at context exit sets it to the previous value.

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
Expand All @@ -354,6 +356,12 @@ Functions and classes provided:
with redirect_stdout(sys.stderr):
help(pow)

To discard or suppress the output of :func:`help` without collecting the data::

with open(os.devnull, 'w') as devnull:
with redirect_stdout(devnull):
help(pow)

Note that the global side effect on :data:`sys.stdout` means that this
context manager is not suitable for use in library code and most threaded
applications. It also has no effect on the output of subprocesses.
Expand All @@ -366,8 +374,8 @@ Functions and classes provided:

.. function:: redirect_stderr(new_target)

Similar to :func:`~contextlib.redirect_stdout` but redirecting
:data:`sys.stderr` to another file or file-like object.
Similar to :func:`~contextlib.redirect_stdout` but redirecting the global
:data:`sys.stderr` to another value, typically a :term:`file object`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "another target" would be better here than "another value"?


This context manager is :ref:`reentrant <reentrant-cms>`.

Expand Down
Loading