[New-bugs-announce] [issue41147] Document that redirect_std{out, err} yield the new stream as the context variable

Peter Law report at bugs.python.org
Sun Jun 28 08:27:34 EDT 2020


New submission from Peter Law <PeterJCLaw+py at gmail.com>:

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.

----------
assignee: docs at python
components: Documentation
messages: 372513
nosy: PeterJCLaw, docs at python
priority: normal
severity: normal
status: open
title: Document that redirect_std{out,err} yield the new stream as the context variable
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41147>
_______________________________________


More information about the New-bugs-announce mailing list