On 29 September 2017 at 11:25, Steven D'Aprano firstname.lastname@example.org wrote:
I like it very much.
But as an alternative, perhaps all we really need is a context manager to set the std* files:
with open('/dev/tty', 'r+') as f: with stdio(stdin=f, stdout=f): name = input('Name? ')
That's nearly as nice, and is possibly useful in more situations. Or maybe we should have both?
Agreed - a general way of redirecting stdio would be more generally useful - I've often replaced stdio, but as far as I can recall never for input().
There's already contextlib.redirect_stdout() and contextlib.redirect_stderr(). Adding contextlib.redirect_stdin() would be logical, but I think a more flexible
contextlib.redirect_stdio(stdin=None, stdout=None, stderr=None)
would be better - where None (the default) means "leave this alone".
Would love to see if anyone else is interested in this. I think it's pretty cool that the core logic really didn't need to be changed other than plumbing in the new args.
I'm interested in the general context manager. I don't use input() enough to be particularly interested in a solution specific to that function.