Could getpass use streams other than stdin?

On posix, it would be useful if I could have getpass.getpass() write the prompt to stderr or to an arbitrary stream (file-like object), rather than always writing to stdin. In my use case, I need to send the results of a command-line script to stdout after the user authenticates, and the user may be redirecting stdout, so I don't want the password prompt to pollute the output. If I could have the "Password: " show up in stderr, that would be ideal. I don't think this would be terribly difficult to implement an API like def getpass(prompt='', stream=None) if stream is None: stream = sys.stdin ... but I'm unsure of the nuances of termios and arbitrary streams. Would it be better to just make the choice be between stdin and stderr? -- in which case the API would be something like def getpass(prompt='', use_stderr=False) Or is the whole exercise a waste of time? Thanks

Mike Brown wrote:
In my use case, I need to send the results of a command-line script to stdout after the user authenticates, and the user may be redirecting stdout, so I don't want the password prompt to pollute the output. If I could have the "Password: " show up in stderr, that would be ideal.
you can redirect sys.stdin/sys.stdout before you call getpass: try: old_stdout = sys.stdout sys.stdout = sys.stderr password = getpass.getpass(prompt) finally: sys.stdout = old_stdout </F>
participants (2)
-
Fredrik Lundh
-
Mike Brown