-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
gh-139452: Clarify redirect_stdout, stderr behavior #139490
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -354,6 +356,12 @@ Functions and classes provided: | |
| with redirect_stdout(sys.stderr): | ||
| help(pow) | ||
|
|
||
cmaloney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. | ||
|
|
@@ -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`. | ||
|
||
|
|
||
| This context manager is :ref:`reentrant <reentrant-cms>`. | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be left out entirely. That is already covered by the docs for
sys.stdoutandsys.__stdout__