Skip to content

Commit 485415e

Browse files
committed
gh-139452: Clarify redirect_stdout, stderr behavior
These don't "redirect" like a terminal where typically the file pointed to by fd 1 and 2 are made the "same" by the preparing process (ex. by `dup2` the given fd into that specific fd number). Rather, these context managers set the respective globals to the provided value. In most cases that should be a "file object".
1 parent 75b1afe commit 485415e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Doc/library/contextlib.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,12 @@ Functions and classes provided:
327327
.. function:: redirect_stdout(new_target)
328328

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

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

335337
For example, the output of :func:`help` normally is sent to *sys.stdout*.
336338
You can capture that output in a string by redirecting the output to an
@@ -366,8 +368,9 @@ Functions and classes provided:
366368

367369
.. function:: redirect_stderr(new_target)
368370

369-
Similar to :func:`~contextlib.redirect_stdout` but redirecting
370-
:data:`sys.stderr` to another file or file-like object.
371+
Similar to :func:`~contextlib.redirect_stdout` but redirecting the global
372+
:data:`sys.stderr` to another value, typically a :term:`file object`
373+
returned from :func:`open`.
371374

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

0 commit comments

Comments
 (0)