11"""Utilities to get a password and/or the current user name.
22
3- getpass(prompt[, stream[, mask ]]) - Prompt for a password, with echo
4- turned off.
3+ getpass(prompt[, stream[, echochar ]]) - Prompt for a password, with echo
4+ turned off and optional keyboard feedback .
55getuser() - Get the user name from the environment or password database.
66
77GetPassWarning - This UserWarning is issued when getpass() cannot prevent
2626class GetPassWarning (UserWarning ): pass
2727
2828
29- def unix_getpass (prompt = 'Password: ' , stream = None , mask = None ):
29+ def unix_getpass (prompt = 'Password: ' , stream = None , * , echochar = None ):
3030 """Prompt for a password, with echo turned off.
3131
3232 Args:
3333 prompt: Written on stream to ask for the input. Default: 'Password: '
3434 stream: A writable file object to display the prompt. Defaults to
3535 the tty. If no tty is available defaults to sys.stderr.
36- mask: A string used to mask input (e.g., '*'). If None, input is hidden.
36+ echochar: A string used to mask input (e.g., '*'). If None, input is
37+ hidden.
3738 Returns:
3839 The seKr3t input.
3940 Raises:
@@ -70,14 +71,14 @@ def unix_getpass(prompt='Password: ', stream=None, mask=None):
7071 old = termios .tcgetattr (fd ) # a copy to save
7172 new = old [:]
7273 new [3 ] &= ~ termios .ECHO # 3 == 'lflags'
73- if mask :
74+ if echochar :
7475 new [3 ] &= ~ termios .ICANON
7576 tcsetattr_flags = termios .TCSAFLUSH
7677 if hasattr (termios , 'TCSASOFT' ):
7778 tcsetattr_flags |= termios .TCSASOFT
7879 try :
7980 termios .tcsetattr (fd , tcsetattr_flags , new )
80- if not mask :
81+ if not echochar :
8182 passwd = _raw_input (prompt , stream , input = input )
8283 stream .write ('\n ' )
8384 return passwd
@@ -92,14 +93,14 @@ def unix_getpass(prompt='Password: ', stream=None, mask=None):
9293 if char == '\x03 ' :
9394 raise KeyboardInterrupt
9495 if char == '\x7f ' or char == '\b ' :
95- if mask and passwd :
96- stream .write ("\b \b " * len (mask ))
96+ if echochar and passwd :
97+ stream .write ("\b \b " * len (echochar ))
9798 stream .flush ()
9899 passwd = passwd [:- 1 ]
99100 else :
100101 passwd += char
101- if mask :
102- stream .write (mask )
102+ if echochar :
103+ stream .write (echochar )
103104 stream .flush ()
104105 finally :
105106 termios .tcsetattr (fd , tcsetattr_flags , old )
@@ -120,7 +121,7 @@ def unix_getpass(prompt='Password: ', stream=None, mask=None):
120121 return passwd
121122
122123
123- def win_getpass (prompt = 'Password: ' , stream = None , mask = None ):
124+ def win_getpass (prompt = 'Password: ' , stream = None , * , echochar = None ):
124125 """Prompt for password with echo off, using Windows getwch()."""
125126 if sys .stdin is not sys .__stdin__ :
126127 return fallback_getpass (prompt , stream )
@@ -135,16 +136,16 @@ def win_getpass(prompt='Password: ', stream=None, mask=None):
135136 if c == '\003 ' :
136137 raise KeyboardInterrupt
137138 if c == '\b ' :
138- if mask and pw :
139- for _ in mask :
139+ if echochar and pw :
140+ for _ in echochar :
140141 msvcrt .putwch ('\b ' )
141142 msvcrt .putwch (' ' )
142143 msvcrt .putwch ('\b ' )
143144 pw = pw [:- 1 ]
144145 else :
145146 pw = pw + c
146- if mask :
147- msvcrt .putwch (mask )
147+ if echochar :
148+ msvcrt .putwch (echochar )
148149 msvcrt .putwch ('\r ' )
149150 msvcrt .putwch ('\n ' )
150151 return pw
0 commit comments