This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Document that redirect_std{out,err} yield the new stream as the context variable
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: PeterJCLaw, docs@python, iritkatriel, miss-islington
Priority: normal Keywords: patch

Created on 2020-06-28 12:27 by PeterJCLaw, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21199 merged PeterJCLaw, 2020-06-28 12:43
PR 26379 merged miss-islington, 2021-05-26 14:13
PR 26380 merged miss-islington, 2021-05-26 14:13
Messages (5)
msg372513 - (view) Author: Peter Law (PeterJCLaw) * Date: 2020-06-28 12:27
In `contextlib`, `_RedirectStream` (the class behind `redirect_stdout` and `redirect_stderr`) returns the current stream target as its context variable, which allows code like this:

``` python
with redirect_stdout(io.StringIO()) as buffer:
    do_stuff()

use(buffer.getvalue())
```

where you capture the redirected stream without a separate line to declare the variable.

This isn't documented (See https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout), yet is potentially useful.

Unless there's a reason that this isn't documented, I propose that the documentation be modified to include it.

Aside: After initially reporting this against the typeshed (https://github.com/python/typeshed/issues/4283) I'm also working on a PR to the typeshed to include this there.
msg394435 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-26 14:13
New changeset 46db39d7bd67fb9fea133cd4f18cdf7eacb0f6d9 by Peter Law in branch 'main':
bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199)
https://github.com/python/cpython/commit/46db39d7bd67fb9fea133cd4f18cdf7eacb0f6d9
msg394437 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-26 14:34
New changeset 1261941e02cd04829592b1b1360b4ec21bfcdb9a by Miss Islington (bot) in branch '3.10':
bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199) (GH-26379)
https://github.com/python/cpython/commit/1261941e02cd04829592b1b1360b4ec21bfcdb9a
msg394439 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-26 14:43
New changeset bee66d3cb98e740f9d8057eb7f503122052ca5d8 by Miss Islington (bot) in branch '3.9':
bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199) (GH-26380)
https://github.com/python/cpython/commit/bee66d3cb98e740f9d8057eb7f503122052ca5d8
msg394440 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-26 14:44
Thank you Peter!
History
Date User Action Args
2022-04-11 14:59:33adminsetgithub: 85319
2021-05-26 14:44:48iritkatrielsetstatus: open -> closed
resolution: fixed
messages: + msg394440

stage: patch review -> resolved
2021-05-26 14:43:46iritkatrielsetmessages: + msg394439
2021-05-26 14:34:30iritkatrielsetmessages: + msg394437
2021-05-26 14:25:57iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11
2021-05-26 14:13:28miss-islingtonsetpull_requests: + pull_request24972
2021-05-26 14:13:23miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24971
2021-05-26 14:13:18iritkatrielsetnosy: + iritkatriel
messages: + msg394435
2020-06-28 12:43:09PeterJCLawsetkeywords: + patch
stage: patch review
pull_requests: + pull_request20352
2020-06-28 12:27:34PeterJCLawcreate