contextlib.redirect_stdio function

Previously raised here [1], contextlib.redirect_stdio would be a utility function to help redirect standard input, output and error. The idea of a function called "stdio" that does the same thing was also raised. The function would combine the functionality of contextlib.redirect_stdout and contextlib.redirect_stderr which work as described in the documentation [2]. It would also add "contextlib.redirect_stdin" functionality which currently appears to be missing. The usage described in the original python-ideas post looks like this:
I'm interested to hear what people think about this. Kind regards, Sam Ezeh [1]: https://mail.python.org/pipermail/python-ideas/2017-September/047236.html [2]: https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout

On Mon, May 02, 2022 at 05:29:56PM -0000, sam.z.ezeh@gmail.com wrote:
Seems kinda useful, but I have two concerns. (1) Perhaps this would be better as a recipe using ExitStack (plus a new redirect_stdin)? https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack (2) I don't see `redirect_stdio(stout=None ...)` as meaning "leave stdout alone". I see it as equivalent to some variation of unsetting stdout, say setting it to /dev/null. I don't know how I would *not* redirect stdout, except to just not redirect it. Overall: Add redirect_stdin: +1 (regardless of what we do with redirect_stdio) Add an ExitStack recipe: +1 Add redirect_stdio: -0 Use None to mean "don't change": -1 Use None to mean "redirect to nowhere": +1 -- Steve

This makes sense, perhaps None could be an alias for a stream created from os.devnull [1]. Then the default arguments for stdin, stdout, stderr would be sys.stdin, sys.stdout and sys.stderr respectively. I've now created a GitHub issue [2] and a draft PR [3] for contextlib.redirect_stdin. [1]: https://docs.python.org/3/library/os.html#os.devnull [2]: https://github.com/python/cpython/issues/92178 [3]: https://github.com/python/cpython/pull/92180

sam.z.ezeh@gmail.com wrote:
I've now created a GitHub issue [2] and a draft PR [3] for contextlib.redirect_stdin.
To provide an update, contextlib.redirect_stdin isn't happening [1] [1]: https://github.com/python/cpython/issues/92178#issuecomment-1115486367

On Mon, May 02, 2022 at 05:29:56PM -0000, sam.z.ezeh@gmail.com wrote:
Seems kinda useful, but I have two concerns. (1) Perhaps this would be better as a recipe using ExitStack (plus a new redirect_stdin)? https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack (2) I don't see `redirect_stdio(stout=None ...)` as meaning "leave stdout alone". I see it as equivalent to some variation of unsetting stdout, say setting it to /dev/null. I don't know how I would *not* redirect stdout, except to just not redirect it. Overall: Add redirect_stdin: +1 (regardless of what we do with redirect_stdio) Add an ExitStack recipe: +1 Add redirect_stdio: -0 Use None to mean "don't change": -1 Use None to mean "redirect to nowhere": +1 -- Steve

This makes sense, perhaps None could be an alias for a stream created from os.devnull [1]. Then the default arguments for stdin, stdout, stderr would be sys.stdin, sys.stdout and sys.stderr respectively. I've now created a GitHub issue [2] and a draft PR [3] for contextlib.redirect_stdin. [1]: https://docs.python.org/3/library/os.html#os.devnull [2]: https://github.com/python/cpython/issues/92178 [3]: https://github.com/python/cpython/pull/92180

sam.z.ezeh@gmail.com wrote:
I've now created a GitHub issue [2] and a draft PR [3] for contextlib.redirect_stdin.
To provide an update, contextlib.redirect_stdin isn't happening [1] [1]: https://github.com/python/cpython/issues/92178#issuecomment-1115486367
participants (2)
-
sam.z.ezeh@gmail.com
-
Steven D'Aprano