Title: Document that redirect_std{out,err} yield the new stream as the context variable
Type: behavior Stage: patch review
Components: Documentation Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: PeterJCLaw, docs@python
Priority: normal Keywords: patch

Created on 2020-06-28 12:27 by PeterJCLaw, last changed 2020-06-28 12:43 by PeterJCLaw.

Pull Requests
URL Status Linked Edit
PR 21199 open PeterJCLaw, 2020-06-28 12:43
Messages (1)
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:


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

This isn't documented (See, 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 ( I'm also working on a PR to the typeshed to include this there.
Date User Action Args
2020-06-28 12:43:09PeterJCLawsetkeywords: + patch
stage: patch review
pull_requests: + pull_request20352
2020-06-28 12:27:34PeterJCLawcreate