Issue 31603: Please add argument to override stdin/out/err in the input builtin
Created on 2017-09-27 06:34 by wt, last changed 2022-04-11 14:58 by admin.
Messages (9)
msg303103 - (view)
Author: Wren Turkal (wt)
Date: 2017-09-27 06:34
Date: 2017-09-27 12:52
Date: 2017-09-27 13:06
Date: 2019-03-18 12:04
Date: 2019-05-06 18:27
Date: 2022-01-17 18:23
I would really love to add a few params to input so that it's signature looked more like so:
def input(prompt, /, *, fin=sys.stdin, fout=sys.stdout, ferr=sys.stderr):
...
This would certainly make overriding the the files in specific calls to input easier. A reasonable use case is collecting a piece over /dev/tty explicitly so that a redirected stdin could be used to pipe data into a program while overriding using /dev/tty to collect some piece of data. Here's some code illustrating what I'd like:
import sys
sys.stdin.close()
sys.stdin = open('/dev/null', 'r')
with open('/dev/tty', 'r') as f:
x = input('Name? ', fin=f)
print(x)
FWIW, I have actually already implemented this fully (including tests). I am just trying to see what I need to do to allow release since I am work on my employer's time.
In the interest of getting feedback, does the signature provided above look sane?
msg303105 - (view)
Author: Wren Turkal (wt)
Date: 2017-09-27 06:41
This might also allow simplification of the getpass.unix_getpass() logic. I am not 100% sure about that yet though.msg303132 - (view) Author: R. David Murray (r.david.murray) *
Date: 2017-09-27 12:52
This is probably a topic for the python-ideas mailing list. For example, rather than complicating input, it might be better to propose adding a redirect_stdin to contextlib to parallel the two existing redirect context managers. I don't remember why that was not added when the others were, but there was probably a reason, so a discussion about what the best solution is for the use case is in order, and python-ideas is the place for that.msg303135 - (view) Author: Serhiy Storchaka (serhiy.storchaka) *
Date: 2017-09-27 13:06
input() is a convenient function for simple user interaction. If you need to read a data from different file object just use its readline() method.msg303297 - (view) Author: Wren Turkal (wt) Date: 2017-09-29 05:31
Added a PR to facilitate discussion on python-ideas.msg338208 - (view) Author: Cheryl Sabella (cheryl.sabella) *
Date: 2019-03-18 12:04
For reference, here's the link to the Python ideas thread: https://mail.python.org/pipermail/python-ideas/2017-September/047230.htmlmsg341587 - (view) Author: anthony shaw (anthonypjshaw) *
Date: 2019-05-06 18:27
The discussion on python-ideas has some unresolved questions. Wren, did you get a definitive answer on this idea?msg410816 - (view) Author: Irit Katriel (iritkatriel) *
Date: 2022-01-17 18:23
See also Issue1927.msg411041 - (view) Author: Wren Turkal (wt) Date: 2022-01-20 18:29
I'm not sure that I ever got a definitive issue, but I didn't have time to push it forward. I still think the idea has merit. I'd love to see it implemented, but I don't have the time to pursue it right now. Thanks, wt On Mon, Jan 17, 2022 at 10:23 AM Irit Katriel <report@bugs.python.org> wrote: > > Irit Katriel <iritkatriel@gmail.com> added the comment: > > See also Issue1927. > > ---------- > nosy: +iritkatriel > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue31603> > _______________________________________ >
History
Date
User
Action
Args
2022-04-11 14:58:52adminsetgithub: 75784
2022-01-20 18:29:21wtsetmessages:
+ msg411041
2022-01-17 18:23:46iritkatrielsetnosy:
+ iritkatriel
messages: + msg410816
2019-05-06 18:27:06anthonypjshawsetnosy: + anthonypjshaw
messages: + msg341587
2019-03-18 12:04:57cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg338208
2017-09-29 05:31:58wtsetkeywords: + patch
messages: + msg303135
2017-09-27 12:52:55r.david.murraysetnosy: + r.david.murray
messages: + msg303132
2017-09-27 06:41:52wtsetmessages: + msg303105 2017-09-27 06:34:07wtcreate
messages: + msg410816
2019-05-06 18:27:06anthonypjshawsetnosy: + anthonypjshaw
messages: + msg341587
2019-03-18 12:04:57cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg338208
2017-09-29 05:31:58wtsetkeywords: + patch
stage: patch review
messages:
+ msg303297
pull_requests:
+ pull_request3798
messages: + msg303135
2017-09-27 12:52:55r.david.murraysetnosy: + r.david.murray
messages: + msg303132
2017-09-27 06:41:52wtsetmessages: + msg303105 2017-09-27 06:34:07wtcreate