summaryrefslogtreecommitdiffstats
path: root/Lib/getpass.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2006-03-31 18:42:16 (GMT)
committerGeorg Brandl <georg@python.org>2006-03-31 18:42:16 (GMT)
commit338ef7d2bd3c2ef507d7ef1edce42492dae28db0 (patch)
tree04479e4ab621b0ad192b1436cc3f7a8b94584c93 /Lib/getpass.py
parent22ec80bc4f3e66990981ca57b66b38873e2711df (diff)
downloadcpython-338ef7d2bd3c2ef507d7ef1edce42492dae28db0.zip
cpython-338ef7d2bd3c2ef507d7ef1edce42492dae28db0.tar.gz
cpython-338ef7d2bd3c2ef507d7ef1edce42492dae28db0.tar.bz2
Bug #1445068: getpass.getpass() can now be given an explicit stream
argument to specify where to write the prompt.
Diffstat (limited to 'Lib/getpass.py')
-rw-r--r--Lib/getpass.py25
1 files changed, 15 insertions, 10 deletions
diff --git a/Lib/getpass.py b/Lib/getpass.py
index e96491f..6b78612 100644
--- a/Lib/getpass.py
+++ b/Lib/getpass.py
@@ -15,11 +15,14 @@ import sys
__all__ = ["getpass","getuser"]
-def unix_getpass(prompt='Password: '):
+def unix_getpass(prompt='Password: ', stream=None):
"""Prompt for a password, with echo turned off.
+ The prompt is written on stream, by default stdout.
Restore terminal settings at end.
"""
+ if stream is None:
+ stream = sys.stdout
try:
fd = sys.stdin.fileno()
@@ -32,18 +35,18 @@ def unix_getpass(prompt='Password: '):
new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
try:
termios.tcsetattr(fd, termios.TCSADRAIN, new)
- passwd = _raw_input(prompt)
+ passwd = _raw_input(prompt, stream)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
- sys.stdout.write('\n')
+ stream.write('\n')
return passwd
-def win_getpass(prompt='Password: '):
+def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
if sys.stdin is not sys.__stdin__:
- return default_getpass(prompt)
+ return default_getpass(prompt, stream)
import msvcrt
for c in prompt:
msvcrt.putch(c)
@@ -63,17 +66,19 @@ def win_getpass(prompt='Password: '):
return pw
-def default_getpass(prompt='Password: '):
- print "Warning: Problem with getpass. Passwords may be echoed."
- return _raw_input(prompt)
+def default_getpass(prompt='Password: ', stream=None):
+ print >>sys.stderr, "Warning: Problem with getpass. Passwords may be echoed."
+ return _raw_input(prompt, stream)
-def _raw_input(prompt=""):
+def _raw_input(prompt="", stream=None):
# A raw_input() replacement that doesn't save the string in the
# GNU readline history.
+ if stream is None:
+ stream = sys.stdout
prompt = str(prompt)
if prompt:
- sys.stdout.write(prompt)
+ stream.write(prompt)
line = sys.stdin.readline()
if not line:
raise EOFError